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

try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
}
?>
<?php include('include/sidebar.php'); ?>

<?php include('include/header.php'); ?>

<!--  connect author at mdkhairnar92@gmail.com  -->

<div class="page-content-wrapper ">

  <div class="container-fluid">

    <div class="row">
      <div class="col-sm-12">
        <div class="page-title-box">
          <div class="btn-group float-right">


          </div>
          <h4 class="page-title">Edit payroll</h4>
        </div>
      </div>
      <div class="clearfix"></div>
    </div>
    <!-- end page title end breadcrumb -->
    <div class="row">
      <div class="col-lg-12">
        <div class="card">
          <div class="card-body">

            <div class="tab-content">

              <div class="tab-pane active p-3" id="home" role="tabpanel">
                <form name="myform" action="app/payroll_crud.php" class="row" method="POST" enctype="multipart/form-data" onsubmit="return validation()" id="add_service">

            <?php
                  $stmt = $conn->prepare("SELECT * FROM `payroll` WHERE id=? ");
                  $stmt->execute([$_POST['id']]);
                  $record = $stmt->fetchAll();

                  foreach ($record as $key) { ?>


            <input class="form-control" type="hidden" name="id" value="<?php echo $key['id']?>"> 
    <div class="form-group col-md-6">
                              <label for="id_label_multiple">Branch<span class="text-danger">*</span> </label>
                              <select class="form-control postName" id="role" aria-label="Default select example" type="text" name="branch" >
                              <option value="">Select branch</option>
                               <?php    
                                $sql1=$conn->prepare("select * from `branch` where delete_status='0' ");
                                $sql1->execute();
                                $row1=$sql1->fetchAll();
                                //print_r($row);
                                //exit;
                                foreach ($row1 as $key1) {?>
                                <option value="<?php echo $key1['branch_name'];?> ">
                                       <?php echo $key1['branch_name'];?>
                                        </option>
                                    <?php } ?>
                              </select>
                           </div>
                    <div class="form-group col-md-6">
                              <label for="id_label_multiple">Staff<span class="text-danger">*</span> </label>
                              <select class="form-control postName" id="user" aria-label="Default select example" type="text" name="user_name" >
                              <option value="">Select </option>
                               <?php    
                                $sql1=$conn->prepare("select * from `user` where delete_status='0' ");
                                $sql1->execute();
                                $row1=$sql1->fetchAll();
                                //print_r($row);
                                //exit;
                                foreach ($row1 as $key1) {?>
                                <option value="<?php echo $key1['name'];?> ">
                                       <?php echo $key1['name'];?>
                                        </option>
                                    <?php } ?>
                              </select>
                           </div>


                    <div class="form-group col-md-6">
                    <label class="control-label">employee Name</label>
                    <input class="form-control" type="text" name="employee_name" value="<?php echo $key['employee_name']?>">
                  </div>

                   <div class="form-group col-md-6">
                    <label class="control-label">Payroll Date</label>
                    <input class="form-control" type="date" name="payroll_date" value="<?php echo $key['payroll_date']?>">
                  </div> 



                  <div class="form-group col-md-12">
                    <div class="mydiv">
                      <div class="form-group row control-group after-add-more subdiv">

                        

                        <div class="col-sm-4">
                             <label class="control-label">admin</label>
                          <input type="text" name=admin[] id="imageInput"value="<?php echo $key['admin']?>">
                         </div>



                         
                        <div class="col-sm-3">
                          <button class="btn btn-success add-more" type="button"><i class="fa fa-plus"></i></button>
                        </div>
                        
                      </div>
                    </div>
                    <div class="copy hide" style="display:none;">
                      <div class="form-group control-group row subdiv">
                        <div class="col-sm-1 sr_no"></div>
                        <div class="col-sm-4">
                             <label class="control-label">admin</label>
                          <input type="text" name=admin[] id="imageInput">
                        </div>
                        <div class="col-sm-3">
                          <button class="btn btn-danger remove" type="button"><i class="fa fa-minus"></i></button>
                        </div>
                        
                      </div>
                    </div>
                    <div class="col-sm-4">
                      <img id="imagePreview" src="#" alt="Selected Image" style="display: none; max-width: 100%;">
                    </div>
                     

                  </div>



<div class="container">
    <table id="dataTable1" class="table table-bordered">
        <thead>
            <tr>
               
                <th>descrption</th>
                <th>Amount</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <tr>

                <td>
                    <label class="control-label">extra time</label>
                    <input type="number" name="extra_time[]" class="form-control" step="0.01"value="<?php echo $key['extra_time']?>">
                </td>
                <td>
                    <label class="control-label">Time in late</label>
                    <input type="number" name="Timein_late[]" class="form-control" step="0.01"value="<?php echo $key['Timein_late']?>">
                </td>
                <td><button class="btn btn-success addRow" type="button">Add Row</button></td>
               
            </tr>
        </tbody>
    </table>
</div>





<div class="form-group col-md-6">
                    <label class="control-label">Total Pay</label>
                    <input class="form-control" type="text" name="total_pay"value="<?php echo $key['total_pay']?>">
                  </div>

<div class="form-group col-md-6">
                    <label class="control-label">Total Deductions</label>
                    <input class="form-control" type="text" name="total_deduction"value="<?php echo $key['total_deduction']?>">
                  </div>

<div class="form-group col-md-6">
                    <label class="control-label">Net Pay</label>
                    <input class="form-control" type="text" name="net_pay"value="<?php echo $key['net_pay']?>">
                  </div>



 <div class="col-md-12">
                    <h6 class="p-2 seo-info" style="background-color: lightgrey;">Net Pay Distribution  
                  </div>


<div class="form-group col-md-6">
                    <label class="control-label">Payment Method</label>
                    <input class="form-control" type="text" name="payment_method" value="<?php echo $key['payment_method']?>">
                  </div>

                  <div class="form-group col-md-6">
                    <label class="control-label">Bank Name</label>
                    <input class="form-control" type="text" name="bank_name" value="<?php echo $key['bank_name']?>">
                  </div>
                  <div class="form-group col-md-6">
                    <label class="control-label">Account Number</label>
                    <input class="form-control" type="text" name="acc_no" value="<?php echo $key['acc_no']?>">
                  </div>
                  <div class="form-group col-md-6">
                    <label class="control-label">Description</label>
                    <input class="form-control" type="text" name="decription" value="<?php echo $key['decription']?>">
                  </div>
                  <div class="form-group col-md-6">
                    <label class="control-label">Paid Amount</label>
                    <input class="form-control" type="text" name="paid_amount"  value="<?php echo $key['paid_amount']?>">
                  </div>



                            <div class="form-group col-md-6">
                             <label class="control-label">Comment</label>
                            <textarea class="form-control" type="text" name="comment"><?php echo $key['comment']?></textarea>
                            </div>


                 <div class="form-group col-sm-6">
                         <label class="control-label">Recurring<span class="text-danger">*</span></label>
                          <select class="form-control" name="recurring" id="recurringSelect">
                        <option value="">Select </option>
                        <option <?php  if($key['recurring']=="0"){ echo "selected";}?>  value="0">Yes</option>
                        <option <?php  if($key['recurring']=="1"){ echo "selected";}?> value="1">No</option>status
                       
                        </select>
                        </div>

<div class="form-group col-md-6" id="recurringDetails" >
    <div class="form-group col-md-6">
    <label class="control-label">Recur Type</label>
    <select class="form-control" name="recur_type">
        <option value="">Select Recur Type</option>
        <option <?php if($key['recurring'] == "0"){ echo "selected";}?> value="Daily">Daily</option>
        <option <?php if($key['recurring'] == "1"){ echo "selected";}?> value="Weekly">Weekly</option>
        <option <?php if($key['recurring'] == "2"){ echo "selected";}?> value="Monthly">Monthly</option>
        <!-- Add more options as needed -->
    </select>
</div>

<div class="form-group col-md-6">
    <label class="control-label">Recur Frequency</label>
    <input class="form-control" type="number" name="recur_frequency" value="<?php echo $key['recur_frequency'] ?? ''; ?>">
</div>

<div class="form-group col-md-6">
    <label class="control-label">Recur Starts</label>
    <input class="form-control" type="date" name="startdate" value="<?php echo $key['startdate'] ?? ''; ?>">
</div>

<div class="form-group col-md-6">
    <label class="control-label">Recur Ends</label>
    <input class="form-control" type="date" name="enddate" value="<?php echo $key['enddate'] ?? ''; ?>">
</div>
</div>


                    <div class="form-group col-md-12">

                    <button class="btn btn-primary" type="submit" name="update" onclick="addService()">Save</button>

                  </div>
                <?php } ?>
                </form>
              </div>


            </div>

          </div>
        </div><!--end card-->
      </div><!--end col-->
    </div><!--end row-->
  </div> <!-- Page content Wrapper -->
</div> <!-- content -->

<?php include('include/footer.php'); ?>
<!--  Author Name: Mayuri K. 
 for any PHP, Codeignitor or Laravel website or software development contact me at mayuri.infospace@gmail.com  -->

<script>
function addService() {
        jQuery.validator.addMethod("alphanumeric", function(value, element) {
            return this.optional(element) || /^(?=.[a-zA-Z])[a-zA-Z0-9\s!@#$%^&()_-]+$/.test(value);
        }, "Please enter alphanumeric characters only");

        jQuery.validator.addMethod("lettersonly", function(value, element) {
            return this.optional(element) || /^[a-zA-Z\s]*$/.test(value);
        }, "Please enter alphabet characters only");
  jQuery.validator.addMethod("numbersonly", function(value, element) {
    return this.optional(element) || /^[0-9]*$/.test(value);
}, "Please enter numeric characters only");
    $('#add_service').validate({
      rules: {
        branch: {
          required: true,
          alphanumeric: true
        },
       user_name: {
          required: true,
          alphanumeric: true
        },
        employee_name: {
          required: true,
          alphanumeric: true
        },
        payroll_date: {
          required: true,
          alphanumeric: true
        },
        total_pay: {
          required: true,
          alphanumeric: true
        },
        total_deduction: {
          required: true,
          alphanumeric: true
        },
        net_pay: {
          required: true,
          alphanumeric: true
        },
        payment_method: {
          required: true,
          alphanumeric: true
        },
        bank_name: {
          required: true,
          alphanumeric: true
        },
        acc_no: {
          required: true,
          alphanumeric: true
        },
         paid_amount: {
          required: true,
          numbersonly: true
        },
        comment: {
          required: true
        }
      },
      messages: {
        branch: {
          required: "Please select a branch",
          alphanumeric: "Only alphanumeric characters are allowed"
        },
        comment: {
          required: "Please enter the description"
        }
      }
    });
}
</script>