<?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">Add 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="#" class="row" method="POST" enctype="multipart/form-data" onsubmit="return validation()" id="add_service">

                  
                       <div class="form-group col-md-6">
                              <label for="id_label_multiple">Branch<span class="text-danger">*</span> </label>
                              <select class="form-control postName select2" 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 select2" 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['username'];?> ">
                                       <?php echo $key1['username'];?>
                                        </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">
                  </div>

                    <div class="form-group col-md-6">
                    <label class="control-label">Payroll Date</label>
                    <input class="form-control" type="date" name="payroll_date">
                  </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 id="rows">
    <tr id="firstRow">
        <td>
            <label class="control-label">Extra Time</label>
            <input type="number" name="extra_time[]" class="form-control" step="0.01">
        </td>
        <td>
            <label class="control-label">Time in Late</label>
            <input type="number" name="Timein_late[]" class="form-control" step="0.01">
        </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">
                  </div>

<div class="form-group col-md-6">
                    <label class="control-label">Total Deductions</label>
                    <input class="form-control" type="text" name="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">
                  </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">
                  </div> -->


                  <div class="form-group col-sm-6">
                         <label class="control-label">payment method<span class="text-danger">*</span></label>
                          <select class="form-control" name="payment_method">
                        <option value="">Select </option>
                        <option value="cash">cash</option>
                        <option value="online payment"> online payment</option>
                       
                        </select>
                        </div>

                  <div class="form-group col-md-6">
                    <label class="control-label">Bank Name</label>
                    <input class="form-control" type="text" name="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">
                  </div>
                  <div class="form-group col-md-6">
                    <label class="control-label">Description</label>
                    <input class="form-control" type="text" name="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">
                  </div>



                            <div class="form-group col-md-6">
                                        <label class="control-label">Comment</label>
                                        <textarea class="form-control" type="text" name="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 value="0">Yes</option>
                        <option value="1">No</option>
                       
                        </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 Frequency</option>
       <option value="0">Daily</option>
       <option value="1">Weekly</option>
       <option value="2">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" >
</div>

<div class="form-group col-md-6">
    <label class="control-label">Recur Starts</label>
    <input class="form-control" type="date" name="startdate" >
</div>

<div class="form-group col-md-6">
    <label class="control-label">Recur Ends</label>
    <input class="form-control" type="date" name="enddate">
</div>
</div>


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

                    <!-- <button class="btn btn-primary" type="submit" name="submit" onclick="addService()">Save</button> -->
                    <button type="button" class="btn btn-primary" id="rzp-button1" onclick="pay_now()">Pay</button>

             </div>
                </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
        
        },
        total_pay: {
          required: true
          
        },
        total_deduction: {
          required: true
         
        },
        net_pay: {
          required: true
          
        },
        payment_method: {
          required: true
          
        },
        bank_name: {
          required: true
         
        },
        acc_no: {
          required: true
          
        },
         paid_amount: {
          required: true
          
        },
        comment: {
          required: true
        }
      },
      messages: {
        branch: {
          required: "Please select a branch",
          alphanumeric: "Only alphanumeric characters are allowed"
        },
        comment: {
          required: "Please enter the description"
        }
      }
    });
}
</script>



<script>
function pay_now() {
    var branch = $("select[name='branch']").val();
    var user = $("select[name='user_name']").val();
    var amount = $("input[name='paid_amount']").val();
    var actual_amount = amount * 100; // Convert to paise
    var description = $("input[name='decription']").val();

    var options = {
        "key": "rzp_test_D2r1vGHsLrVCXK", // Your Razorpay API Key
        "amount": actual_amount,
        "currency": "INR",
        "name": user,
        "description": description,
        "image": "", // You can add your logo URL if needed
        "handler": function (response) {
            console.log(response);
            $.ajax({
                url: 'process_payment.php',
                method: 'POST',
                data: {
                    'payment_id': response.razorpay_payment_id,
                    'amount': amount,
                    'name': user
                },
                success: function (data) {
                    alert('Thanks!!');
                    console.log(data);
                    // location.reload();
                    // Handle success message or redirection to success page
                }
            });
        },
        "prefill": {
            "name": user,
            "email": "", // Add email if available
            "contact": "" // Add contact number if available
        },
        "theme": {
            "color": "#3399cc"
        }
    };

    var rzp1 = new Razorpay(options);
    rzp1.open();
}
</script>