<?php
include("../db/cn.php");
ob_start();
if(isset($_SESSION['user_name']) && isset($_SESSION['user_role']) && isset($_SESSION['outlet_name']) && isset($_SESSION['outlet_address'])) {
    $userName = $_SESSION['user_name'];
    $userRole = $_SESSION['user_role'];
    $outlet_address = $_SESSION['outlet_address'];
    $outlet_name = $_SESSION['outlet_name'];
}



	
if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['withdraw_add'])){
	$userName1 = $_SESSION['user_name'];
     $userRole1 = $_SESSION['user_role'];
	@$name_detailnew = $_POST['desc1'];
	@$amountnew = $_POST["amount1"];
	$paid_wd = $_POST["paid_wd"];

	$date_timenew = date("Y-m-d");
	mysqli_query($connection,"insert into withdraw(name_detail,amount,paid_by,date_time,login_user,user_role,outlet_name,outlet_address)
	values('".$name_detailnew."','".$amountnew."','".$paid_wd."','".$date_timenew."','".$userName1."','".$userRole1."','".$outlet_name."','".$outlet_address."') ");
	echo '<script type="text/javascript">window.location.href="withdraw.php";</script>';
	
	}

	


ob_end_flush();
?>
<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Qudratullah Trading</title>

    <!-- Custom fonts for this template-->
    <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
   <link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@200;300;400;500;600;700&display=swap" rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="../css/sb-admin-2.min.css" rel="stylesheet">
        <link href="../css/sb-admin-2.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<style>
body {
    font-family: 'Quicksand', sans-serif;
    
}
		    .chart-container {
      position: relative;
      background: rgba(255, 255, 255, 0.9);
      border-radius: 10px;
      padding: 15px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    }

    canvas {
      max-width: 100%;
      height: auto;
    }
.bg-gradient-primary{
    
	background:#0A4657;
	color:#fff;	
	
	}

.btn-primary{
	background:#0A4657;
	color:#fff;
	 border:1px #0A4657 solid;
	
	}  
.btn-primary:hover{
  
  background:#fff;
  border:1px #0A4657 solid;
	color:#0A4657;
	
	}	 
    .chart-container {
      position: relative;
      background: rgba(255, 255, 255, 0.9);
      border-radius: 10px;
      padding: 15px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    }

    canvas {
      max-width: 100%;
      height: auto;
    }
	
	.fa-download:hover{
		color:#953E39;
		}
		
.text-primary{
	  	
		}
  </style>

<body id="page-top">

    <!-- Page Wrapper -->
    <div id="wrapper">

        <!-- Sidebar -->
        <?php
        include("common/sd.php");
		?>
        <!-- End of Sidebar -->

        <!-- Content Wrapper -->
        <div id="content-wrapper" class="d-flex flex-column">

            <!-- Main Content -->
            <div id="content">

                <!-- Topbar -->
                <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">

                    <!-- Sidebar Toggle (Topbar) -->
                    <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                        <i class="fa fa-bars"></i>
                    </button>

                    <!-- Topbar Search -->
                    
  <?php include('common/log.php');  ?>

                    <!-- Topbar Navbar -->
                    
                </nav>
   <div class="container-fluid">
       <div class="row">
        <div class="col-sm-3">
     <h3 style="font-weight:700; color:#060606;">WithDraw</h3>
        </div>
        <div class="col-sm-6"></div>
        <div class="col-sm-3">
            
        </div>
    </div>
    
</div>
<br />

<!-- Date filter form -->
<div class="container-fluid">
    <form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <div class="row">
            <div class="col-sm-3">
                <input type="date" name="start_date" class="form-control" value="<?php echo isset($_GET['start_date']) ? $_GET['start_date'] : ''; ?>" />
            </div>
            <div class="col-sm-3">
                <input type="date" name="end_date" class="form-control" value="<?php echo isset($_GET['end_date']) ? $_GET['end_date'] : ''; ?>" />
            </div>
            <div class="col-sm-2">
                <button type="submit" class="btn btn-dark">Filter</button>
            </div>
           
            <div class="col-sm-4">
                 
                 <a href="withdraw.php"><button type="button" class="btn btn-dark"  ><i class="fas fa-sync-alt fa-1x"></i></button></a>
                    <button type="button" class="btn btn-dark " data-toggle="modal" data-target="#addWithdrawModal">
                        Add Withdraw
                    </button>
                
            </div>
        </div>
    </form>
</div>


<div class="container-fluid">
    <div class="row">
        <div class="col-sm-12" style="" id="table-container-wrapper">
            <div class="row text-center">
                <!-- Search container -->
               
                

                <table class="table table-striped table-hover " style="margin-top:20px; ">
                    <tr class="bg-dark text-white" style="font-weight:700; font-size:15px;" class="text-white text-capitalize header-row">
                        <th>Date</th>
                        <th>Drawing</th>
                        <th>Amount(Rs)</th>
                        <th>Paid By</th>
                        <th> By</th>
                    </tr>

                    <?php
                    ob_start();
                    
                    // Handle start date and end date filters
                    $startDate = isset($_GET['start_date']) ? $_GET['start_date'] : null;
                    $endDate = isset($_GET['end_date']) ? $_GET['end_date'] : null;

                    // SQL query
                    $query = "SELECT * FROM withdraw WHERE 1=1";
                    
                    if ($startDate) {
                        $query .= " AND date_time >= '$startDate'";
                    }
                    if ($endDate) {
                        $query .= " AND date_time <= '$endDate'";
                    }
                    
                    $query .= " ORDER BY date_time DESC";
                    $rec = mysqli_query($connection, $query);

                    while ($row = mysqli_fetch_array($rec)) {
                        ?>
                        <tr style="font-size:14px;color:#000;">
                            <td><?php echo $row["date_time"]; ?></td>
                            <td class="text-capitalize" style="font-weight:700;"><?php echo $row["name_detail"]; ?></td>
                            <td><?php echo $row["amount"]; ?></td>
                            <td style="text-transform:uppercase;font-weight:700;font-size:12px;"><?php echo $row["paid_by"]; ?></td>
                            <td class="text-capitalize"><?php echo $row["login_user"]; ?></td>
                        </tr>
                    <?php 
                    }
                    ob_end_flush();
                    ?>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Modal for adding withdraw -->
<div class="modal fade" id="addWithdrawModal" tabindex="-1" role="dialog"
aria-labelledby="addwithdrawModalLabel" aria-hidden="true" style="margin-top:80px;">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title text-capitalize" id="addwithdrawModalLabel">Add Withdraw Cash</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <!-- Form for adding purchase -->
                <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
                    <input type="text" name="desc1" placeholder="Description / Name" class="form-control" />
                    <br />
                    <input type="text" name="amount1" placeholder="Amount" class="form-control" />
                    <br />
                    <select name="paid_wd" class="form-control">
                        <option value="cash">Cash</option>
                        <option value="bank1">Bank 1</option>
                                    <option value="bank2">Bank 2 </option>
                                    <option value="bank3">Bank 3</option>
                        <option value="other">Other</option>
                    </select>
                    <br>
                    <button type="submit" class="btn btn-dark" name="withdraw_add">Enter</button>
                </form>
            </div>
        </div>
    </div>
</div>


    
            
                <!-- /.container-fluid -->

            </div>
            <br><br>
            <!-- End of Main Content -->

            <!-- Footer -->
            <?php include("common/main_ft.php");  ?>
            <!-- End of Footer -->

        </div>
        <!-- End of Content Wrapper -->

    </div>
    <!-- End of Page Wrapper -->

    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
        <i class="fas fa-angle-up"></i>
    </a>

    <!-- Logout Modal-->
    <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
                    <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
                    <a class="btn btn-primary" href="">Logout</a>
                </div>
            </div>
        </div>
    </div>

      
   <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js"></script>
    <!-- Bootstrap core JavaScript-->
    <script src="../vendor/jquery/jquery.min.js"></script>
    <script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="../vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="../js/sb-admin-2.min.js"></script>

</body>

</html>