<?php
include("db/cn.php");
include("common/tx.php");
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'];
}
//$cr_id = $_GET["cr_id"];
@$supplier_name = $_GET["supplier_name"];
//echo $cr_nickname;
$rec =  mysqli_query($connection,"select * from purchase where supplier_name = '$supplier_name' && paid_by = 'credit' ");
$row1 = mysqli_fetch_array($rec);
//echo $row1["order_id"];
//@$order_id = $row1["order_id"];


$date_time1 = date("Y-m-d");
$rec10 = mysqli_query($connection, "SELECT customer_name, discount_amount  FROM purchase WHERE  customer_name = '$supplier_name'  AND paid_by = 'credit' GROUP BY inv_id ");
//&& user_name = '$userName' AND user_role = '$userRole'
if ($rec10) {
    $total_sales = 0;
    while ($row3 = mysqli_fetch_array($rec10)) {
        $total_sales += $row3['discount_amount'];
    }
    echo "";
    if (mysqli_num_rows($rec10) == 0) {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}


$date_time21 = date("Y-m-d");
$cash_out = mysqli_query($connection, "SELECT SUM(supplier_amount) AS total_amount FROM supplier_paid where user_name = '$userName' && user_role = '$userRole' && supplier_name = '$supplier_name'   ");
if ($cash_out) {
    $cash_outdata = mysqli_fetch_array($cash_out);
    if ($cash_outdata && isset($cash_outdata['total_amount'])) {
        $totalAmount = $cash_outdata['total_amount'];
        echo "";
    } else {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}


$cash_purchase = mysqli_query($connection, "SELECT SUM(discount_amount) AS total_amounts FROM purchase
where login_user = '$userName' && user_role = '$userRole' && customer_name = '$supplier_name' && paid_by = 'cash' GROUP BY inv_id   ");
if ($cash_purchase) {
    $cash_purchase_data = mysqli_fetch_array($cash_purchase);
    if ($cash_purchase_data && isset($cash_purchase_data['total_amounts'])) {
        $totalAmount_cash = $cash_purchase_data['total_amounts'];
        echo "";
    } else {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}

//-------------------------------------------------------------


$cash_purchases_r = mysqli_query($connection, "SELECT customer_name, discount_amount  FROM purchase WHERE  customer_name = '$supplier_name'  AND paid_by = 'credit' AND status = 'return' GROUP BY customer_name ");
//&& user_name = '$userName' AND user_role = '$userRole'
if ($cash_purchases_r) {
     $totalAmount_cash_r  = 0;
    while ($row32 = mysqli_fetch_array($cash_purchases_r)) {
        $totalAmount_cash_r  += $row32['discount_amount'];
    }
    echo "";
    if (mysqli_num_rows($rec10) == 0) {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}



//--------------------------------------------------------------

$supplier_out = mysqli_query($connection, "SELECT SUM(open_amount) AS total_amount FROM supplier_opening_amount where user_name = '$userName' && user_role = '$userRole' && supplier_name = '$supplier_name'   ");
if ($supplier_out) {
    $supplier_outdata = mysqli_fetch_array($supplier_out);
    if ($supplier_outdata && isset($supplier_outdata['total_amount'])) {
        $totalAmount_supplier = $supplier_outdata['total_amount'];
        echo "";
    } else {
        echo "";
    }
} else {
    echo "Error executing query: " . mysqli_error($connection);
}



if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["supplier_paid"])){
$date_time = $_POST["date_time"];
	$supplier = $_POST["supplier_name"];
	$amount = $_POST["supplier_amount"];
	$status = $_POST["status"];
	
	
	 mysqli_query($connection,"insert into supplier_paid(supplier_name,supplier_amount,status,date_time,user_name,user_role,outlet_name,outlet_address)
values('".$supplier."','".$amount."','".$status."','".$date_time."','".$userName."','".$userRole."','".$outlet_name."','".$outlet_address."')");

 echo "<script type='text/javascript'>window.location.href = 'supplier_detail.php?supplier_name=$supplier_name';</script>";

}

if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["supplier_open"])){
$date_time32 = date("Y-m-d");
			$supplier_name = $_POST["supplier_name"];
			$open_amount = $_POST["open_amount"];
			 
			 mysqli_query($connection,"insert into supplier_opening_amount(supplier_name,open_amount,date_time,user_name,user_role,outlet_name,outlet_address)
			 values('".$supplier_name."','".$open_amount."','".$date_time32."','".$userName."','".$userRole."','".$outlet_name."','".$outlet_address."');");
			 
			 
			  echo "<script type='text/javascript'>window.location.href = 'supplier_detail.php?supplier_name=$supplier_name';</script>";
}

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["supplier_close"])) {

    // Check connection
    if ($connection->connect_error) {
        die("Connection failed: " . $connection->connect_error);
    }

    // Retrieve values from $_POST
    $supplier_name = $_POST['supplier_name'];
    $opening_cash = $_POST['opening_cash'];
    $cash_purchase = $_POST['cash_purchase'];
    $purchase_return = $_POST['cash_purchases_r'];
    $cash_paid = $_POST["cash_paid"];
    $credit_purchase = $_POST['credit_purchase'];
    $closing_balance = $_POST["closing_balance"];
    $date_time211 = date("Y-m-d");

    // Check if the supplier already has an entry in the database
    $check_sql = "SELECT * FROM supplier_closing WHERE supplier_name = '$supplier_name'";
    $result = $connection->query($check_sql);

    if ($result->num_rows > 0) {
        // Update existing record
        $sql = "UPDATE supplier_closing SET 
                    opening_cash = '$opening_cash', 
                    cash_purchase = '$cash_purchase', 
                    credit_purchase = '$credit_purchase',  
                    cash_paid = '$cash_paid', 
                    closing_balance = '$closing_balance', 
                    user_name = '$userName', 
                    user_role = '$userRole', 
                    outlet_name = '$outlet_name', 
                    outlet_address = '$outlet_address', 
                    date_time = '$date_time211' 
                WHERE supplier_name = '$supplier_name'";
    } else {
        // Insert new record
        $sql = "INSERT INTO supplier_closing(
                    supplier_name, opening_cash, cash_purchase, credit_purchase, cash_paid, closing_balance, 
                    user_name, user_role, outlet_name, outlet_address, date_time
                ) VALUES (
                    '$supplier_name', '$opening_cash', '$cash_purchase', '$credit_purchase', '$cash_paid', '$closing_balance', 
                    '$userName', '$userRole', '$outlet_name', '$outlet_address', '$date_time211'
                )";
    }

    // Execute SQL query
    if ($connection->query($sql) === TRUE) {
        echo "";
    } else {
        echo "Error: " . $sql . "<br>" . $connection->error;
    }
}

?>
<!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><?php echo $tx; ?></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">
    <!--<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.3/xlsx.full.min.js"></script>-->
        <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;
		}
		
		/* Basic styling */
    .tab {
        display: none;
        padding: 20px;
    }
    .active {
        display: block;
    }
    .tab-buttons {
        display: flex;
    }
    .tab-button {
        cursor: pointer;
        padding: 10px 10px;
        background-color: #ffff;
        border: 1px solid #0A5064;
        border-radius: 5px 5px 0 0;
        margin-right: 5px;
		color:#212529;
    }
    .tab-button.active {
        background-color: #212529;
		color:#fff;
    }

    /* Background colors for tab content */
    #tab1 {
        background-color: #fff;
		color:#212529;
		padding:20px;
    }
    #tab2 {
        color:#212529;
		background-color: ;
    }
    #tab3 {
		color:#212529;
        background-color: ;
    }
    tr{
        font-weight:700;
    }
    h3{
        color:#000;
        font-weight:700;
    }
    h4{
        color:#000;
        font-weight:700;
    }
    .btn, .form-control{
        font-weight:700;
    }
  </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>
                <!-- End of Topbar -->

                <!-- Begin Page Content -->
                
             <div class="">
<br />
<div class="container-fluid">
<div class="row">
<div class="col-sm-9">
<h3 style="color:#212529;font-weight:700;">Supplier Detail</h3>
</div>
<div class="col-sm-3">
<a href="supplier_view.php"><button type="button" class="btn btn-dark btn-lg"><i class="fa fa-arrow-left"></i></button></a>
<!--<button type="button" class="btn btn-dark " data-toggle="modal" data-target="#addPurchaseModal">-->
<!--               Paid-->
<!--            </button>-->
<!--             <button type="button" class="btn btn-dark" data-toggle="modal" data-target="#addopenPurchaseModal">-->
<!--              Add Open-->
<!--            </button>-->
</div>
</div>
<br />
<div class="container">
<div class="row">
<div class="col-sm-7  text-dark text-capitalize" >
<span style="background:#0A4657;color:#fff;padding:10px; border-radius:7px; font-size:18px; border:1px solid #0A5064;"><?php echo $supplier_name; ?></span></div>
<div class="col-sm-5"></div>

</div>
</div>
<br /><br />
<div class="container-fluid">
<div class="tab-buttons" style="font-weight:700;">
    <div class="tab-button active" onclick="openTab(event, 'tab1')">Overview</div>
    <div class="tab-button" onclick="openTab(event, 'tab2')">Purchase</div>
    <div class="tab-button" onclick="openTab(event, 'tab4')">Purchase Return</div>
    <div class="tab-button" onclick="openTab(event, 'tab3')">Cash Paid</div>
</div>
<?php
$close_balance1 =  $total_sales + @$totalAmount_supplier;
$close = $close_balance1 - @$totalAmount - @$totalAmount_cash_r;

?>
<div class="tab-container">
    <div id="tab1" class="tab active">
    <h4>Overview</h4>
    <form method="post" action="supplier_detail.php?supplier_name=<?php echo $supplier_name; ?>">
    <input type="hidden" value="<?php echo $supplier_name; ?>" name="supplier_name"  />
    <div class="container ">
        <div class="row" style="padding:20px;background:#fff; box-shadow: 0px 0px 5px 2px #CCC; border-radius:10px;">
            <div class="col-sm-12" style="padding:20px;">
            <table class="table table-hover"  >
            <tr style="color:#8A1218;padding:20px;">
            <td style="font-weight:600;">Opening Cash</td>
            <td style="text-align:right; font-size:19px; font-weight:600;">
            <?php echo isset($totalAmount_supplier) ? "Rs ".$totalAmount_supplier."" : ""; ?>
      <input type="hidden" value="<?php echo isset($totalAmount_supplier) ? " ".$totalAmount_supplier."" : ""; ?>" name="opening_cash"  />
			</td>
            
            </tr>
            <!--<tr>
            <td style="font-weight:600;">Cash Purchase</td>
            <td style="text-align:right; font-size:19px; font-weight:600;">
            <?php echo isset($totalAmount_cash) ? "Rs ".$totalAmount_cash."" : ""; ?>
       
			</td>
            </tr>-->
            <input type="hidden" value="<?php echo isset($totalAmount_cash) ? "".$totalAmount_cash."" : ""; ?>" name="cash_purchase"  />
            <tr>
            <td style="font-weight:600;">Credit Purchase</td>
            <td style="text-align:right; font-size:19px; font-weight:600;">
            <?php echo isset($total_sales) ? "Rs ".$total_sales."" : ""; ?>
     <input type="hidden" value="<?php echo isset($total_sales) ? " ".$total_sales."" : ""; ?>" name="credit_purchase"  />
			</td>
            </tr>
            <tr>
            <td style="font-weight:600;">Purchase Return</td>
            <td style="text-align:right; font-size:19px; font-weight:600;">
            <?php echo isset($totalAmount_cash_r) ? "Rs ".$totalAmount_cash_r."" : ""; ?>
       <input type="hidden" value="<?php echo isset($totalAmount_cash_r) ? " ".$totalAmount_cash_r."" : ""; ?>" name="cash_purchases_r"  />
			</td>
            </tr>
            <tr>
            <td style="font-weight:600;">Cash Paid</td>
            <td style="text-align:right; font-size:19px; font-weight:600;">
            <?php echo isset($totalAmount) ? "Rs ".$totalAmount."" : ""; ?>
        <input type="hidden" value="<?php echo isset($totalAmount) ? " ".$totalAmount."" : ""; ?>" name="cash_paid"  />
			</td>
            </tr> 

            <tr>
            <td style="font-weight:600;">Closing Balance</td>
            <td style="text-align:right; font-size:19px; font-weight:600;">
            <?php echo isset($close) ? "Rs ".$close."" : ""; ?>
      <input type="hidden" value="<?php echo isset($close) ? "".$close."" : ""; ?>" name="closing_balance"  />
			</td>
            </tr>
                     
            </table>
            <button style="submit" class="btn btn-dark btn-sm" name="supplier_close">Save</button>
            </div>
            
            
        </div>
        <br />
        
        
    </div>
     </form>
    <p></p>
</div>
    <div id="tab2" class="tab">
        <h4>Purchase</h4>
        <div class="container">
           <div class="row">
              
    
            <br />
            </div>
            
           
        
             <div class="col-sm-12" id="table-container-wrapper">
                         <div class="row">
                             <div class="col-sm-4">
            <label for="startDate">Start Date:</label>
            <input class="form-control" type="date" id="startDatePurchase">
            </div>
            <div class="col-sm-4">
            <label for="endDate">End Date:</label>
            <input class="form-control" type="date" id="endDatePurchase">
        </div>
        <div class="col-sm-4">
    <button type="button" class="btn btn-dark" id="downloadPurchase" style="float:right;margin-top:30px;" onclick="downloadTableData('purchaseTableBody', 'Purchase_Data.xlsx')">Download</button>
            </div>
</div>
        </div>
        <hr>
               <table class="table table-stripped table-sm text-center">
                <tr style="background:#212529; color:#fff;">
                <td>Date</td>
                <td>Invoice ID</td>
                <td>Amount (Rs)</td>
                <td>Method</td>
                <td> By</td>
                </tr>
                <tbody id="purchaseTableBody">
               <?php  
			   $rec3 = mysqli_query($connection,"select *  from purchase  where customer_name = '$supplier_name'  GROUP BY inv_id  ");
			   while($row3 = mysqli_fetch_array($rec3)){
			   ?>
              <tr>
              <td><?php echo $row3['date_time']; ?></td>
              <td><?php echo $row3['inv_id'];  ?></td>
              <td> <?php echo $row3['discount_amount']; ?></td>
              <td class="text-capitalize"><?php echo $row3["paid_by"];?></td>
              <td style="text-transform:capitalize;"><?php echo $row3['login_user']; ?></td>
              </tr>
              <?php }?>
              </tbody>
               </table>
             </div>
           </div>
          </div>
          <div id="tab4" class="tab">
        <h4>Purchase Return</h4>
        <div class="container">
           <div class="row">
              
    
            <br />
            </div>
            
           
        
             <div class="col-sm-12" id="table-container-wrapper">
                                  <div class="row">
                             <div class="col-sm-4">
            <label for="startDate">Start Date:</label>
            <input class="form-control" type="date" id="startDateReturn">
            </div>
            <div class="col-sm-4">
            <label for="endDate">End Date:</label>
            <input class="form-control" type="date" id="endDateReturn">
        </div>
        <div class="col-sm-4">
    <button type="button" class="btn btn-dark" id="downloadReturn" style="float:right;margin-top:30px;" onclick="downloadTableData('ReturnTableBody', 'Return_Data.xlsx')">Download</button>
</div>
        </div>
        <hr>
               <table class="table table-stripped table-sm text-center">
                <tr style="background:#212529; color:#fff;">
                <td>Date</td>
                <td>Invoice ID</td>
                <td>Amount (Rs)</td>
                <td>Method</td>
                <td> By</td>
                </tr>
                <tbody id="ReturnTableBody">
               <?php  
			   $rec4 = mysqli_query($connection,"select * from purchase  where customer_name = '$supplier_name' AND status = 'return' GROUP BY r_inv_id   ");
			   while($row4 = mysqli_fetch_array($rec4)){
			   ?>
              <tr>
              <td><?php echo $row4['date_time']; ?></td>
              <td><?php echo $row4["inv_id"]; ?></td>
              <td> <?php echo $row4['discount_amount']; ?></td>
              <td class="text-capitalize"><?php echo $row4["paid_by"];?></td>
              <td style="text-transform:capitalize;"><?php echo $row4['login_user']; ?></td>
              </tr>
              <?php }?>
               </tbody>
               </table>
               
             </div>
           </div>
          </div>
    </div>
    <div id="tab3" class="tab">
        <h4>Cash Paid</h2>
        <div class="container ">
           <div class="row">
       
            </div>
           
           <br />
             <div class="col-sm-12" id="table-container-wrapper">
            <div class="row">
               <div class="col-sm-4">
            <label for="startDate">Start Date:</label>
            <input class="form-control" type="date" id="startDatePaid">
            </div>
            <div class="col-sm-4">
            <label for="endDate">End Date:</label>
            <input class="form-control" type="date" id="endDatePaid">
        </div>
        <div class="col-sm-4">
<button type="button" class="btn btn-dark" id="downloadPaid" style="float:right;margin-top:30px;" onclick="downloadTableData('PaidTableBody', 'Paid_Data.xlsx')">Download</button>
</div>
        </div>
        <hr>
               <table class="table table-stripped table-sm text-center text-capitalize">
                <tr style="background:#212529; color:#fff;">
                
                <td>Date</td>
                
                <td>Amount (Rs)</td>
                <td>Method</td>
                <td>By</td>
                </tr>
                <tbody id="PaidTableBody">
              <?php 
			  
			  
			  $rec1 = mysqli_query($connection,"select * from supplier_paid where supplier_name = '$supplier_name' ");
			  while($row2 = mysqli_fetch_array($rec1)){
			  
			  ?>
              <tr>
              
              <td><?php echo $row2["date_time"];?></td>
              
              <td> <?php echo $row2["supplier_amount"]; ?> </td>
              <td><?php echo $row2["status"]; ?></td>
              <td> <?php echo $row2["user_name"];?></td>
              </tr>
              <?php } ?>
              </tbody>
              </table>
             </div>
           </div>
          </div>
    </div>

<div class="modal fade" id="addopenPurchaseModal" tabindex="-1" role="dialog" 
            aria-labelledby="addPurchaseopenModalLabel" aria-hidden="true" style="margin-top:80px;">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header" style="background:#0A4567; color:#fff;">
                            <h5 class="modal-title text-capitalize" id="addPurchaseopenModalLabel" style="font-weight:700;">Add Opening Amount</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="supplier_detail.php?supplier_name=<?php echo $supplier_name; ?>" enctype="multipart/form-data">
                            
                                <input type="hidden" value="<?php echo $supplier_name; ?>" name="supplier_name" placeholder="Name" class="form-control" />
                             
                                <input type="text" name="open_amount" placeholder="Amount" class="form-control" />
                               <br />
                               <!-- <input class="form-control" type="date" name="date_time"  />
                                <br />-->
                                <button type="submit" name="supplier_open"  class="btn btn-dark">Enter</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

<div class="modal fade" id="addPurchaseModal" tabindex="-1" role="dialog" 
            aria-labelledby="addPurchaseModalLabel" aria-hidden="true" style="margin-top:80px;">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header" style="background:#0A4567; color:#fff;">
                            <h5 class="modal-title text-capitalize" id="addPurchaseModalLabel" style="font-weight:700;">Supplier Paid</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="supplier_detail.php?supplier_name=<?php echo $supplier_name; ?>" enctype="multipart/form-data">
                            
                                <input type="text" name="supplier_name" placeholder="Supplier Name" 
                                value="<?php echo $supplier_name; ?>" class="form-control" style="background:#0A4567; color:#fff;" />
                                <br />
                                <input type="text" name="supplier_amount" placeholder="Amount" class="form-control" />
                               <br />
                                <select name="status" class="form-control">
                                <option value="">Select Method</option>                                
                                    <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 />
                                <input type="date" name="date_time" class="form-control" />
                                <br />
                                <button type="submit" name="supplier_paid"  class="btn btn-dark">Enter</button>
                            </form>
                        </div>
                    </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>
// Function to export table data to Excel
function downloadTableData(tableBodyId, filename) {
    const tableBody = document.getElementById(tableBodyId);
    const rows = Array.from(tableBody.querySelectorAll("tr"));
    const data = [];

    // Loop through each row and extract cell data
    rows.forEach(row => {
        const rowData = Array.from(row.querySelectorAll("td")).map(cell => cell.innerText);
        data.push(rowData);
    });

    // Define worksheet and workbook
    const ws = XLSX.utils.aoa_to_sheet(data);
    const wb = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, ws, "Sheet1");

    // Download Excel file
    XLSX.writeFile(wb, filename);
}
</script>

<script>
// Filter function for Purchase table
function filterpurchaseTableByDate() {
    const startDate = new Date(document.getElementById('startDatePurchase').value);
    const endDate = new Date(document.getElementById('endDatePurchase').value);
    const rows = document.querySelectorAll('#purchaseTableBody tr');

    rows.forEach(row => {
        const rowDate = new Date(row.cells[0].innerText);
        row.style.display = (!isNaN(startDate) && rowDate < startDate) || (!isNaN(endDate) && rowDate > endDate) ? 'none' : '';
    });
}

// Filter function for Paid table
function filterPaidTableByDate() {
    const startDate = new Date(document.getElementById('startDatePaid').value);
    const endDate = new Date(document.getElementById('endDatePaid').value);
    const rows = document.querySelectorAll('#PaidTableBody tr');

    rows.forEach(row => {
        const rowDate = new Date(row.cells[0].innerText);
        row.style.display = (!isNaN(startDate) && rowDate < startDate) || (!isNaN(endDate) && rowDate > endDate) ? 'none' : '';
    });
}

// Filter function for Return table
function filterReturnTableByDate() {
    const startDate = new Date(document.getElementById('startDateReturn').value);
    const endDate = new Date(document.getElementById('endDateReturn').value);
    const rows = document.querySelectorAll('#ReturnTableBody tr');

    rows.forEach(row => {
        const rowDate = new Date(row.cells[0].innerText);
        row.style.display = (!isNaN(startDate) && rowDate < startDate) || (!isNaN(endDate) && rowDate > endDate) ? 'none' : '';
    });
}

// Bind event listeners for each date input to corresponding filter functions
document.getElementById('startDatePurchase').addEventListener('change', filterpurchaseTableByDate);
document.getElementById('endDatePurchase').addEventListener('change', filterpurchaseTableByDate);

document.getElementById('startDatePaid').addEventListener('change', filterPaidTableByDate);
document.getElementById('endDatePaid').addEventListener('change', filterPaidTableByDate);

document.getElementById('startDateReturn').addEventListener('change', filterReturnTableByDate);
document.getElementById('endDateReturn').addEventListener('change', filterReturnTableByDate);
</script>
<script>
 // Function to switch between tabs
    function openTab(evt, tabName) {
        // Get all elements with class="tab" and hide them
        var tabs = document.getElementsByClassName("tab");
        for (var i = 0; i < tabs.length; i++) {
            tabs[i].style.display = "none";
        }

        // Get all elements with class="tab-button" and remove the class "active"
        var tabButtons = document.getElementsByClassName("tab-button");
        for (var i = 0; i < tabButtons.length; i++) {
            tabButtons[i].classList.remove("active");
        }

        // Show the current tab, and add an "active" class to the button that opened the tab
        document.getElementById(tabName).style.display = "block";
        evt.currentTarget.classList.add("active");
    }
  
	</script>
      
   <!--<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>