<?php
     session_start();
      include 'database/shikanisha.kts.php';
        if(!($_SESSION['type'] == 'Manager' || $_SESSION['type'] == 'Cashier' || $_SESSION['type'] == 'Receptionist' || $_SESSION['type'] == 'Waiter')){
            header("location:logout.php");
}


?>
<?php 
include 'database/shikanisha.kts.php';
?>
<!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">
    <title>Jacyjoka Apartments</title>
    <!-- Favicon icon -->
    <link rel="icon" type="./quixlab/image/png" sizes="16x16" href="./quixlab/images/favicon.png">
    <!-- Pignose Calender -->
    <link href="./quixlab/plugins/pg-calendar/css/pignose.calendar.min.css" rel="stylesheet">
    <!-- Chartist -->
    <link rel="stylesheet" href="./quixlab/plugins/chartist/css/chartist.min.css">
    <link rel="stylesheet" href="./quixlab/plugins/chartist-plugin-tooltips/css/chartist-plugin-tooltip.css">
    <!-- Custom Stylesheet -->
    <link href="./quixlab/css/design.css" rel="stylesheet">
    <!-- DataTables with Buttons -->
    <link rel="stylesheet" href="./quixlab/plugins/tables/css/datatable/dataTables.bootstrap4.min.css">
    <link href="./quixlab/plugins/tables/css/datatable/dataTables.bootstrap4.min.css" rel="stylesheet">
    <link href="./quixlab/css/buttons.bootstrap.min.css" rel="stylesheet" />

</head>

<body>

    <!--*******************
        Preloader start
    ********************-->
    <div id="preloader">
        <div class="loader">
            <svg class="circular" viewBox="25 25 50 50">
                <circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="3" stroke-miterlimit="10" />
            </svg>
        </div>
    </div>
    <!--*******************
        Preloader end
    ********************-->

    
    <!--**********************************
        Main wrapper start
    ***********************************-->
    <div id="main-wrapper">

        <!--**********************************
            Nav header start
        ***********************************-->
        <div class="nav-header">
            <div class="brand-logo" style="background-image: url('./quixlab/images/room.png');">
                
            </div>
        </div>
        <!--**********************************
            Nav header end
        ***********************************-->

        <!--**********************************
            Header start
        ***********************************-->
        <div class="header">    
            <div class="header-content clearfix">
                
                <div class="nav-control">
                    <div class="hamburger">
                        <span class="toggle-icon"><i class="icon-menu"></i></span>
                    </div>
                </div>
                <div class="header-left">
                    <div class="input-group icons">
                        <div class="input-group-prepend">
                            <span class="input-group-text bg-transparent border-0 pr-2 pr-sm-3" id="basic-addon1"><i class="mdi mdi-magnify"></i></span>
                        </div>
                        <input type="search" class="form-control" placeholder="Search Dashboard" aria-label="Search Dashboard">
                        <div class="drop-down animated flipInX d-md-none">
                            <form action="#">
                                <input type="text" class="form-control" placeholder="Search">
                            </form>
                        </div>
                    </div>
                </div>
                <div class="header-right">
                    <ul class="clearfix">
                      
                    </ul>
                </div>
            </div>
        </div>
        <!--**********************************
            Header end ti-comment-alt
        ***********************************-->

        <!--**********************************
            Sidebar start
        ***********************************-->
          <?php


// Define arrays for each user role and the corresponding allowed links
$managerLinks = array(
    "orders.php", 
       "payments.php", 
    "reports.php", 
    "purchases.php", 
    "accomodation.php", 
    "employee.php", 
    "inventory.php"
);

$cashierLinks = array(
    "orders.php", 
   
    "payments.php"
);

$receptionistLinks = array(
    "orders.php", 
    "payments.php", 
    "accomodation.php" 
   
);

// Check if user is logged in and their role is set in the session
if(isset($_SESSION['type'])) {
    $role = $_SESSION['type'];
    
    // Assign the corresponding links based on the user role
    switch($role) {
        case "Manager":
            $links = $managerLinks;
            break;
        case "Cashier":
            $links = $cashierLinks;
            break;
        case "Receptionist":
            $links = $receptionistLinks;
            break;
        default:
            $links = array();
            break;
    }
} else {
    // Redirect user to login page if they are not logged in
    header("Location: login.php");
    exit();
}

?>

<div class="nk-sidebar">           
    <div class="nk-nav-scroll">
        <ul class="metismenu" id="menu">
            <li class="nav-label">Dashboard</li>
            <li>
                <a class="has-arrow" href="javascript:void()" aria-expanded="false">
                    <i class="icon-speedometer menu-icon"></i><span class="nav-text">Dashboard</span>
                </a>
                <ul aria-expanded="false">
                    <li><a href="index.php">Dashboard</a></li>
                    <!-- <li><a href="./index-2.html">Home 2</a></li> -->
                </ul>
            </li>
            <?php 
               foreach($links as $link) {
    $linkText = ucfirst(str_replace(".php", "", $link)); // remove .php extension and capitalize first letter
    echo '<li><a href="' . $link . '"><i class="icon-menu menu-icon"></i><span class="nav-text">' . $linkText . '</span></a></li>';
}

            ?>
            <li>
                <a href="logout.php" aria-expanded="false">
                    <i class="icon-lock menu-icon"></i><span class="nav-text">LogOut</span>
                </a>
            </li>
        </ul>
    </div>
</div>
        <!--**********************************
            Sidebar end
        ***********************************-->

        <!--**********************************
            Content body start
        ***********************************-->
        <div class="content-body">

            <div class="container-fluid mt-3">
              
                  <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="card-title">My Pending Bills</h4>
                                <div class="active-member">
                                    <div class="table-responsive">
                                        <table class="table table-xs mb-0 zero-configuration" id="bootstrap-data-table-export">

                                            <thead>
                                                <tr>
                                                    <th>ID</th>
                                                    <th>Seller Name</th>
                                                    <th>Total</th>
                                                    <th>Paid Amount</th>
                                                    <th>Status</th>
                                                    <th>Table Name</th>
                                                    <th>Date</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                             <tbody>
                                              <?php
        
                                                    $sql = "SELECT amount_received, payment_method, phone_number, seller_date, seller_name, total, trans_id,  table_name
FROM transaction_receipts
WHERE seller_name='{$_SESSION['user']}' AND amount_received < total
";
                                                    $result=mysqli_query($conn,$sql);
                                                     while($row=mysqli_fetch_array($result)){
                                                  ?>
                                                <tr>
                                                    <td><?php echo $row['trans_id']; ?></td>
                                                    <td>
                                                      <?php echo $row['seller_name']; ?>
                                                    </td>
                                                    <td>
                                                        <?php echo $row['total']; ?>
                                                    </td>
                                                     <td>
                                                     <?php echo $row['amount_received']; ?>
                                                     </td>
                                                    <td><?php if ($row['amount_received'] == $row['total']){ echo'<span class="badge badge-success">Paid</span>
                                                        ';}else if($row['amount_received'] < $row['total']){ echo'
                                                        
														<span class="badge badge-warning">Pending</span>';}else if($row['amount_received'] > $row['total']){ echo'
                                                        
														<span class="badge badge-danger">Overpayment</span>';}?>
                                                    </td>
                                                   
                                                    <td>
                                                       <?php echo $row['table_name']; ?>
                                                    </td>
                                                    <td>
                                                       <?php echo $row['seller_date']; ?>
                                                    </td>
                                                    
													 <td class="text-center">
                                                  <button class="btn btn-outline-primary btn-sm editbtn" type="button" data-id="<?php echo $row['trans_id'] ?>" id="loan" data-toggle="modal" ><i class="fa fa-edit"></i></button>
                                                 
                                                </td>
              										
                                                </tr>
                                               <?php } ?>
                                               
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>                        
                    </div>
                </div>
                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="card-title">My Bills Today</h4>
                                <div class="active-member">
                                    <div class="table-responsive">
                                        <table class="table table-xs mb-0 zero-configuration" id="bootstrap-data-table-export">

                                            <thead>
                                                <tr>
                                                    <th>ID</th>
                                                    <th>Seller Name</th>
                                                    <th>Total</th>
                                                    <th>Paid Amount</th>
                                                    <th>Status</th>
                                                    <th>Table Name</th>
                                                    <th>Date</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                         <?php
    // set the start and end datetime for the query
    $start_datetime = date('Y-m-d 06:00:00');
    $end_datetime = date('Y-m-d 05:00:00', strtotime('+1 day'));

    $sql = "SELECT amount_received, payment_method, phone_number, seller_date, seller_name, total, trans_id, table_name
            FROM transaction_receipts
            WHERE seller_name='{$_SESSION['user']}' AND seller_date BETWEEN '{$start_datetime}' AND '{$end_datetime}'";

    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) == 0) {
        echo "No results found.";
    } else {
?>
        <tbody>
        <?php
            while ($row = mysqli_fetch_array($result)) {
        ?>
            <tr>
                <td><?php echo $row['trans_id']; ?></td>
                <td><?php echo $row['seller_name']; ?></td>
                <td><?php echo $row['total']; ?></td>
                <td><?php echo $row['amount_received']; ?></td>
                <td>
                    <?php
                        if ($row['amount_received'] == $row['total']) {
                            echo '<span class="badge badge-success">Paid</span>';
                        } else if ($row['amount_received'] < $row['total']) {
                            echo '<span class="badge badge-warning">Pending</span>';
                        } else if ($row['amount_received'] > $row['total']) {
                            echo '<span class="badge badge-danger">Overpayment</span>';
                        }
                    ?>
                </td>
                <td><?php echo $row['table_name']; ?></td>
                <td><?php echo $row['seller_date']; ?></td>
                <td class="text-center">
                    <button class="btn btn-outline-primary btn-sm editbtn" type="button" data-id="<?php echo $row['trans_id'] ?>" id="loan" data-toggle="modal" ><i class="fa fa-edit"></i></button>
                </td>
            </tr>
        <?php
            }
        ?>
        </tbody>
<?php
    }
?>

                                               
                                           
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>                        
                    </div>
                </div>
              
                
                <!-- Payment Modal -->
                
                 <div class="modal fade bd-example-modal-sm-loan-plan " tabindex="-1" role="dialog" aria-hidden="true" id="editmodal">
                                        <div class="modal-dialog modal-sm">
                                            <div class="modal-content">
                                               <form id="payment_update">
                                                <div class="modal-header">
                                                    <h5 class="modal-title">Update Payment Details</h5>
                                                    <button type="button" class="close" data-dismiss="modal"><span>&times;</span>
                                                    </button>
                                                </div>
                                                <div class="modal-body">
                                                  <div class="basic-form">
                                                        <div class="basic-form">
                                                          <label>Reference ID</label>
                                                          <input type="text" id="loanid" name="loanid" value="" class="form-control input-rounded">
                                                        </div>
                                                         <div class="basic-form">
                                                          <label>Payment Amount</label>
                                                          <input type="number" min="0" id="payamount" name="payamount" value="" class="form-control input-rounded">
                                                        </div>
                                                       <div class="basic-form">
    <label>Payment Mode</label>
    <select class="custom-select browser-default input-rounded" id="paymethod" name="paymethod" required onchange="showHideBookingIdInput()">
        <option value="">Please select</option>
        <option value="cash">Cash</option>
        <option value="till">Till</option>
        <option value="merchant">Merchant</option>
        <option value="rooms">Rooms</option>
    </select>
</div>

<div class="basic-form" id="bookingIdContainer" style="display: none;">
    <label>Booking Id</label>
    <input type="text" id="booking_id" name="booking_id" value="" class="form-control input-rounded">
</div>
                                                       
                                                        
                                                    
                                                  </div>
                                                  
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                                                    <button type="submit" class="btn btn-success">Save</button>
                                                </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                
                <!-- end of payment modal -->

            </div>
            <!-- #/ container -->
        </div>
        <!--**********************************
            Content body end
        ***********************************-->
        
        
        <!--**********************************
            Footer start
        ***********************************-->
        <div class="footer">
            <div class="copyright">
                <p>Copyright &copy; Designed & Developed by <a href="https://kakashitech.com">Kakashi Tech</a> 2023</p>
            </div>
        </div>
        <!--**********************************
            Footer end
        ***********************************-->
    </div>
    <!--**********************************
        Main wrapper end
    ***********************************-->

    <!--**********************************
        Scripts
    ***********************************-->
    <script src="./quixlab/plugins/common/common.min.js"></script>
    <script src="./quixlab/js/custom.min.js"></script>
    <script src="./quixlab/js/settings.js"></script>
    <script src="./quixlab/js/gleek.js"></script>
    <script src="./quixlab/js/styleSwitcher.js"></script>

    <!-- Chartjs -->
    <script src="./quixlab/plugins/chart.js/Chart.bundle.min.js"></script>
    <!-- Circle progress -->
    <script src="./quixlab/plugins/circle-progress/circle-progress.min.js"></script>
    <!-- Datamap -->
    <script src="./quixlab/plugins/d3v3/index.js"></script>
    <script src="./quixlab/plugins/topojson/topojson.min.js"></script>
    <script src="./quixlab/plugins/datamaps/datamaps.world.min.js"></script>
    <!-- Morrisjs -->
    <script src="./quixlab/plugins/raphael/raphael.min.js"></script>
    <script src="./quixlab/plugins/morris/morris.min.js"></script>
    <!-- Pignose Calender -->
    <script src="./quixlab/plugins/moment/moment.min.js"></script>
    <script src="./quixlab/plugins/pg-calendar/js/pignose.calendar.min.js"></script>
    <!-- ChartistJS -->
    <script src="./quixlab/plugins/chartist/js/chartist.min.js"></script>
    <script src="./quixlab/plugins/chartist-plugin-tooltips/js/chartist-plugin-tooltip.min.js"></script>
    <script src="./quixlab/js/dashboard/dashboard-1.js"></script>
    <!-- Datatables -->
    <script src="./js/dashboard/dashboard-1.js"></script>
    <script src="./quixlab/plugins/tables/js/jquery.dataTables.min.js"></script>
    <script src="./quixlab/plugins/tables/js/datatable/dataTables.bootstrap4.min.js"></script>
    <script src="./quixlab/plugins/tables/js/datatable-init/datatable-basic.min.js"></script>
    
    <script src="./quixlab/js/data-table/dataTables.buttons.min.js"></script>
    <script src="./quixlab/js/data-table/buttons.flash.min.js"></script>
    <script src="./quixlab/js/data-table/jszip.min.js"></script>
    <script src="./quixlab/js/data-table/pdfmake.min.js"></script>
    <script src="./quixlab/js/data-table/vfs_fonts.js"></script>
    <script src="./quixlab/js/data-table/buttons.html5.min.js"></script>
    <script src="./quixlab/js/data-table/buttons.print.min.js"></script>
    <script src="./quixlab/js/data-table/datatables-init.js"></script>
    
    <!-- SweetAlert CDN -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>


 <script>
  $(document).ready(function() {
  $('#bootstrap-data-table-export').on('click', '.editbtn', function() {
    $('#editmodal').modal('show');

    var $tr = $(this).closest('tr');
    var data = $tr.children("td").map(function() {
      return $(this).text();
    }).get();

    console.log(data);

    $('#loanid').val(data[0]);
  });
});


   </script>
   
<script>
    function showHideBookingIdInput() {
        var paymethodSelect = document.getElementById("paymethod");
        var bookingIdContainer = document.getElementById("bookingIdContainer");
        var selectedOption = paymethodSelect.options[paymethodSelect.selectedIndex].value;

        if (selectedOption === "rooms") {
            bookingIdContainer.style.display = "block";
        } else {
            bookingIdContainer.style.display = "none";
        }
    }
</script>
  <script>
    $(document).ready(function() {
        // Add event listener to form submit button
        $('#payment_update').on('submit', function(event) {
            event.preventDefault(); // Prevent default form submission behavior

            // Serialize form data
            var formData = $(this).serialize();

            // Send AJAX request to server
            $.ajax({
                url: './database/payment.kts.php', // PHP script to handle the request
                type: 'POST',
                data: formData,
                success: function(response) {
                    // Parse the JSON response
                    var jsonResponse = JSON.parse(response);

                    // Display SweetAlert based on the response status
                    if (jsonResponse.status === 'success') {
                        Swal.fire({
                            title: 'Success!',
                            text: jsonResponse.message,
                            icon: 'success',
                            confirmButtonText: 'OK'
                        }).then(function() {
                            // Reload the current page
                            location.reload();
                        });
                    } else {
                        Swal.fire({
                            title: 'Error!',
                            text: jsonResponse.message,
                            icon: 'error',
                            confirmButtonText: 'OK'
                        });
                    }

                    // Clear the form input fields
                    $('#payment_update')[0].reset();
                },
                error: function(xhr, status, error) {
                    // Handle errors
                    console.log(xhr.responseText);
                }
            });
        });
    });
</script>

	
</body>

</html>