<?php
  session_start();
  if(empty($_SESSION['branch']) || $_SESSION['branch'] == ''){
    header("Location: index.php");
    die();
  }
    require_once 'system/conn.php';
?>
<?php 

  if(isset($_POST["submit"]))
  {
    $rev_from= $_POST['rev_from'];
    $emp_id= $_POST['emp_id'];
    $return_date= $_POST['return_date'];
    $reason= $_POST['reason'];
    $branch= $_SESSION['branch'];
    
    $timestamp = strtotime ("-1 month",strtotime ($return_date));
    $nextMonth  =  date("Y-m-d",$timestamp);
    $date = DateTime::createFromFormat("Y-m-d", $nextMonth);
    
    $month1= $date->format("m");
    $year1= $date->format("Y");
    
    $updated_date=date("Y-m-d");
    $data = [
      'rev_from' => $rev_from,
      'emp_id' => $emp_id,
      'return_date' => $return_date,
      'reason' => $reason,
      'branch' => $branch,
      'month' => $month1,
      'year' => $year1,
      'updated_date' => $updated_date,
    ];
    //print_r($data);exit;
    $sql = "INSERT INTO `rev_payment_tbl` (rev_from, emp_id, return_date, reason, branch, month, year, updated_date) VALUES (:rev_from, :emp_id, :return_date, :reason, :branch, :month, :year, :updated_date)";
    $stmt= $conn->prepare($sql);
    $statement = $stmt->execute($data);
    
    if($statement){
      $date=date('m-d-Y', strtotime(date('Y-m')." -1 month"));
      $date=explode('-',$date);
      $month=$date[0];
      $year=$date[2];
      $query1 = " UPDATE `emp_salary` SET  `payment_status`='0' WHERE month='$month'  and year='$year' and `branch`='".$_SESSION['branch']."' and `emp_id`= '".$emp_id."' ";
      //print_r($query1);exit;
      $update1 = $conn->prepare($query1)->execute($data2);
      if($update1){  
        echo "<script>alert('Reverse Payment Entry Successfully.')</script>";
        echo "<script>window.location.href = 'reverse-payment-data.php'</script>";
      }else{
        echo "not inserted";
      }
    }
  } 
?>
<!doctype html>
<html lang="en" class="semi-dark">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- loader-->
    <link href="assets/css/pace.min.css" rel="stylesheet" />
    <script src="assets/js/pace.min.js"></script>
    <!--plugins-->
    <link href="assets/plugins/perfect-scrollbar/css/perfect-scrollbar.css" rel="stylesheet" />
    <link href="assets/plugins/simplebar/css/simplebar.css" rel="stylesheet" />
    <link href="assets/plugins/metismenu/css/metisMenu.min.css" rel="stylesheet" />
    <!-- CSS Files -->
    <link href="assets/css/bootstrap.min.css" rel="stylesheet">
    <link href="assets/css/bootstrap-extended.css" rel="stylesheet">
    <link href="assets/css/style.css" rel="stylesheet">
    <link href="assets/css/icons.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&display=swap" rel="stylesheet">
    <!--Theme Styles-->
    <link href="assets/css/dark-theme.css" rel="stylesheet" />
    <link href="assets/css/semi-dark.css" rel="stylesheet" />
    <link href="assets/css/header-colors.css" rel="stylesheet" />
    <script src="assets/js/jquery.min.js"></script>
    <title>Reverse Payment Entry</title>
  </head>
  <body>
    
   <!--start wrapper-->
   <div class="wrapper">
    <?php include'system/header.php'?>
    <!--end top header-->

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

            <div class="row">
              <div class="col-xl-12 mx-auto">
                <h6 class="mb-0 text-uppercase">Reverse Payment Entry</h6>
                <hr/>
                
                <div class="card">
                  <div class="card-body">
                    <div class="p-4 border rounded">
                      <form class="row g-3 needs-validation" novalidate method="post" enctype="multipart/form-data" action="">
                        <div class="col-md-4">
                          <label for="validationCustom01" class="form-label">Return From :</label>
                          <select class="form-select" id="revfrom-dropdown" name="rev_from">
                            <option value="">---Select---</option>
                            <option value="General Bank Sheet">General Bank Sheet</option>
                            <option value="General Cheque Sheet">General Cheque Sheet</option>
                            <option value="OT Bank Sheet">OT Bank Sheet</option>
                            <option value="OT Cheque Sheet">OT Cheque Sheet</option>
                          </select>
                        </div>
                        <div class="col-md-4">
                          <label for="validationCustom01" class="form-label">Employee :</label> 
                          <select class="form-select" id="emp-dropdown" name="emp_id"></select>
                          <!--<input type="text" class="form-control" id="input-5" name="emp_id" required>-->
                        </div>
                        <div class="col-md-4">
                          <label for="validationCustom01" class="form-label">Return Date :</label>
                          <input type="date" class="form-control" id="input-5" name="return_date" required>
                        </div>
                        <div class="col-md-12">
                          <label for="validationCustom01" class="form-label">Reason :</label>
                          <textarea class="form-control" id="input-5" name="reason" ></textarea>
                        </div>
                        <div class="col-12">
                          <button class="btn btn-primary" type="submit" name="submit" value="submit" >Submit form</button>
                        </div>
                      </form>
                    </div>
                  </div>
                </div>
                
                
              </div>
            </div>
            <!--end row-->

          </div>
          <!-- end page content-->
         </div>

        <script>
        $(document).ready(function() {
            $('#revfrom-dropdown').on('change', function() {
                var empp_id = this.value;
                $.ajax({
                    url: "rev-emp.php",
                    type: "POST",
                    data: {
                        empp_id: empp_id
                    },
                    cache: false,
                    success: function(result){
                      $("#emp-dropdown").html(result);
                    }
                });
            });
        });
        </script>
<?php include'system/footer.php'?>