<div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0 text-dark">Employees Pay

            </h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="<?= base_url(); ?>/dashboard">Dashboard</a></li>
              <li class="breadcrumb-item active">Employees Pay
              </li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- Main content -->
    <section class="content">
    <div class="container-fluid">
      <form method="post" id="incomeexpense_report" class="card basicvalidation" action="<?php echo base_url().'Paychecks/index/'?>">
         <div class="card-body">
            <div class="row">
               <div class="col-md-3">
                  <div class="form-group row">
                     <label for="incomeexpense_from" class="col-sm-5 col-form-label">Report From</label>
                     <div class="col-sm-6 form-group">
                        <input type="date" required="true" class="form-control form-control-sm " value="<?php echo isset($_POST['incomeexpense_from']) ? $_POST['incomeexpense_from'] : ''; ?>" id="incomeexpense_from" name="incomeexpense_from" placeholder="Date From">
                     </div>
                  </div>
               </div>
               <div class="col-md-3">
                  <div class="form-group row">
                     <label for="incomeexpense_to" class="col-sm-5 col-form-label">Report To</label>
                     <div class="col-sm-6 form-group">
                        <input type="date" required="true" class="form-control form-control-sm " value="<?php echo isset($_POST['incomeexpense_to']) ? $_POST['incomeexpense_to'] : ''; ?>" id="incomeexpense_to" name="incomeexpense_to" placeholder="Date To">
                     </div>
                  </div>
               </div>
               <div class="col-md-4">
                  <div class="form-group row">
                     <label for="booking_to" class="col-sm-3 col-form-label">Driver</label>
                     <div class="col-sm-8 form-group">
                        <select id="incomeexpense_vechicle" class="form-control"  name="driverId">
                             <option value=""> ALL </option>
                           <?php foreach ($drivers as $id => $driver) {
                             ?>
                             <option <?php if($driverId==$id){echo 'selected';} ?> value="<?= $id ?>"> <?= $driver["userName"] ?> </option>
                           <?php
                           } ?>
                        </select>
                     </div>
                  </div>
               </div>
               <input type="hidden" id="incomeexpensereport" name="incomeexpensereport" value="1">
               <div class="col-md-2">
                  <button type="submit" class="btn btn-block btn-outline-info btn-sm"> Generate Report</button>
               </div>
            </div>
         </div>
   </div>

   </form>


      <div class="container-fluid">

      <div class="card">

        <div class="card-body p-0">
<?php 
$test=array();
      ?>  
          

         <div class="table-responsive">
                    <table id="custtbl" class="table card-table table-vcenter ">
                      <thead>
                        <tr>
                          <th class="w-1">S.No</th>
                          <th>Driver</th>
                          
                          <th>Completion Time</th>
                          <th>Type</th>
                          <th>Amount</th>
                          <!--
                          <th>Status</th>
                          -->
                         
                          <th>Action</th>
                          
                        </tr>
                      </thead>
                      <tbody>
                      <?php




                      $finalamout=0;
                      if(!empty($paychecks)){ 
                        
                        
                        
                        
                        $count=1;
                           foreach($paychecks as $x=> $paycheck){
                              // echo "<pre>";
                              //  print_r($paycheck);
                              //  echo "</pre>"; 

                            if($paycheck['status']=="requested"){
                             
                            $x1=getusernamewithcartype($paycheck['driverId']);
                           	// echo "<pre>";
                           	// print_r($x1);
                           	// echo "</pre>";

                           ?>
                        <tr>
                           <td> <?php echo output($count); $count++; ?></td>
                           <td> 

                            <?php echo ($x1['name']); ?></td>
                            <td><?php echo date("Y-m-d h:i",$paycheck['completionTimeStamp']/1000); ?></td>
                           <td><?php echo ($x1['carType']); ?></td>
                           <td> <?php echo "$".output($paycheck['amount']); ?></td>
                          
                           <td>

                              <?php if ($paycheck['status'] == "requested") {
                                $finalamout=$finalamout+$paycheck['amount'];
                               array_push($test,$x);

                                ?>
                                <a class="icon" onclick='releasePayment("<?= $x; ?>")'>
                              <button class="btn btn-info btn-small">Release Payment</button>
                            </a>
                                <?php
                              } elseif($paycheck['status'] == "pending"){
                                ?>
                              <button class="btn btn-warning btn-small">Pending ...</button>
                              <?php
                              } else {
                                ?>
                                <button class="btn btn-success btn-small" disabled="true">Paid</button>
                                <?php
                              }?>   
                          
                          </td>
                        </tr>
                        <?php } } } ?>
                        <tr>
                              <td colspan="4"><b>Final Amout</b></td>
                              <td><?php echo "$".$finalamout ; ?></td>
                            
                                <td>
<?php 

if(count($test)){
  
  $mynewvalue=implode(",",$test);
  ?>


<form >
<input type="hidden" id="checkid" name="checkid"  value="<?php echo $mynewvalue; ?>"/>
<button type="button" onclick='releaseallPayment()' Class ="btn btn-success btn-small">Release All Payment</button>

</form>

<?php
}
?>
</td>



                        </tr>
                      </tbody>
                    </table>
                   
        </div>         
        </div>
        <!-- /.card-body -->
      </div>
      
             </div>
    </section>
    <!-- /.content -->


<script>
    function releasePayment(id){
    	$.ajax({
          url: "<?php echo base_url('paychecks/releasePaycheck'); ?>",
          method: 'post',
          data: {
            paymentId: id
          },
          dataType: 'json',
          cache: false,
          success: function(result) {
            console.log(result);
            if (result.success == 1) {
              console.log('success');              
            } else {
              console.log('Error occured');
            }
            location.reload();
          },
          error: function(err) {
            alert("Something went wrong. Check console");
            console.log(err["responseText"]);
          }
        })

	}
  function releaseallPayment(){
    var checkid=$("#checkid").val();
   
    $.ajax({
        
       

          url: "<?php echo base_url('paychecks/releasePaycheckall'); ?>",
          method: 'post',
          data: {
            checkid: checkid
          },
          dataType: 'json',
          cache: false,
          success: function(result) {
          
            if (result.success == 1) {
              console.log('success');              
            } else {
              console.log('Error occured');
            }
            location.reload();
          },
          error: function(err) {
        //    alert("Something went wrong. Check console");
            console.log(err["responseText"]);
          }
        })

	}
	
</script>