<?php
session_start();
include 'include/config.php';

if(!isset($_SESSION['id'])){
 echo "<script> window.location.href = 'index.php' </script>";
  }
?>

<?php
$select_spa = mysqli_query($conn,"select * from `vender`");
?>

<?php  include 'include/head.php'; ?>

<div class="container-fluid">
                        
                        <!-- start page title -->
                        <div class="row">
                            <div class="col-12">
                                <div class="page-title-box">
                                    <div class="page-title-right">
                                        <ol class="breadcrumb m-0">
                                            <li class="breadcrumb-item"><a href="">Home</a></li>
                                            <li class="breadcrumb-item active">Collection</li>
                                        </ol>
                                    </div>
                                    <h4 class="page-title">Collection</h4>
                                </div>
                            </div>
                        </div>
                        <!-- end page title --> 
                        
                        
                        <div class="row">
        <div class="card">
            <div class="card-body">
                <!-- Form start -->
                <form method="post" action="">
                    <!-- Dropdown and Input field -->
                    <div class="row">
                        <div class="col-lg-4">
                            <div class="form-group">
                                <label for="exampleFormControlSelect1">Select Spa</label>
                                <select name="spa" class="form-control" id="exampleFormControlSelect1">
                                    <option>Select..</option>
                                    <?php while($data = mysqli_fetch_assoc($select_spa)){?>
                                    <option value="<?php echo $data['unique_id'];?>"><?php echo $data['unique_id'] . ' = ' .$data['name'];?></option>
                                    <?php }?>
                                    
                                </select>
                            </div>
                        </div>
                        <div class="col-lg-4">
                           <div class="form-group">
                               <label for="schedule">To Date</label>
                               <input type="date" name="to_date" class="form-control" id="schedule">
                           </div>
                       </div>
                       
                       <div class="col-lg-4">
                           <div class="form-group">
                               <label for="schedule">From Date</label>
                               <input type="date" name="from_date" class="form-control" id="schedule">
                           </div>
                       </div>
                        <div class="col-lg-4" style="margin-top:20px;">
                         <button type="submit" name="submittt" class="btn btn-primary">Submit</button>
                         </div>
                    </div>
                    <!-- End of Dropdown and Input field -->
                    <!-- Submit button -->
                   
                </form>
                
               <?php 
if(isset($_POST['submittt'])){
    $spa_id = $_POST['spa'];
    $to_date = $_POST['to_date'];
    $from_date = $_POST['from_date'];
    
    // Format the dates to match the database format
    $to_date = date('d-m-Y', strtotime($to_date));
    $from_date = date('d-m-Y', strtotime($from_date));
    
    // Include proper escaping for the variables to prevent SQL injection
    $spa_id = mysqli_real_escape_string($conn, $spa_id);
    $to_date = mysqli_real_escape_string($conn, $to_date);
    $from_date = mysqli_real_escape_string($conn, $from_date);
    
    // Construct the SQL query with proper date format and single quotes around date values
    $selllll = mysqli_query($conn,"SELECT * FROM `booking` WHERE `vender_id`='$spa_id' AND `booking_status`='complete' AND STR_TO_DATE(booking_date, '%d-%m-%Y') BETWEEN STR_TO_DATE('$to_date', '%d-%m-%Y') AND STR_TO_DATE('$from_date', '%d-%m-%Y')");
    // echo $selllll; // For testing, remove this line in production
}
?>
                <!-- Form end -->
            </div>
        </div>
</div>

                          <!--<div>-->
                          <!--    <a href="service_cat.php"  class="btn btn-success"> Add Category</a>-->
                          <!--</div>-->

                        <div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <div class="table-responsive">
                    <table id="datatable" class="table table-striped dt-responsive nowrap w-100">
                        <thead>
                            <tr>
                                <th>Sr. No.</th>
                                <th>User Name</th>
                                <th>Phone No</th>
                                <th>Booking Date</th>
                                <th>Booking Time</th>
                                <th>Booking Status</th>
                                <th>Spa Name</th>
                                <th>Booking Code</th>
                                <th>Employee Charges</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                              $totalCollection = 0; // Initialize total collection variable
                              $i = 1;
                              while ($data = mysqli_fetch_assoc($selllll)) { 
                                  // Increment total collection with each employee's charge
                                  $totalCollection += $data['employee_chargs'];
                                  ?>
                                  <tr>
                                      <td><?= $i; ?></td>
                                      <td><?= $data['user_name']; ?></td>
                                      <td><?= $data['user_phone']; ?></td>
                                      <td><?= $data['booking_date']; ?></td>
                                      <td><?= $data['booking_time']; ?></td>
                                      <td><?= $data['booking_status']; ?></td>
                                      <?php $select_spaaa = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM `vender` WHERE `unique_id`='{$data['vender_id']}'")); ?>
                                      <td><?= $select_spaaa['name']; ?></td>
                                      <td><?= $data['unique_id']; ?></td>
                                      <td><?= $data['employee_chargs']; ?></td>
                                      <td></td> <!-- Placeholder for action column -->
                                  </tr>
                              <?php
                                  $i++;
                              }; 
                              ?>
                              <tr>
                                  <td style="margin-to:30px; color:red;">Total Collection:</td>
                                  <td colspan="2"></td> <!-- Span other columns -->
                                  <td style="color:black"><?= $totalCollection; ?></td> <!-- Display total collection -->
                              </tr>
                                                  </table>
                                              </div>
                                          </div>
                                      </div>
                                  </div>
                              </div>


<?php  
include 'include/footer.php'; 
?>