<?php 
    session_start();
    if(!$_SESSION['id']){
        header('location:index.php');
    }
?>
<?php 
require_once '../system/conn.php';
if(isset($_POST["submit"]))
{
  
$designation=$_POST['designation'];
$branch=$_POST['branch'];
$salary=$_POST['salary'];
$new_salary=$_POST['new_salary'];
$updated_date=date('Y-m-d');

   $a=1;
   $stmt = $conn->prepare("SELECT * FROM `emp_details` where branch='".$branch."' and designation='".$designation."' and `salary`='".$salary."' ");
   $stmt->execute();
   $users = $stmt->fetchAll();
   foreach($users as $user) 
   { 
       if($users){
          $query = "INSERT INTO `salary_update_log` (updated_date,branch,designation,prv_sal,new_sal) VALUES  ( '$updated_date', '$branch', '$designation', '$salary', '$new_salary')";
          $statement = $conn->prepare($query)->execute($data);
            if($statement){
                $emp_name= $user['e_name'];
                $emp_id= $user['e_id']; 
                
                $query1 = "INSERT INTO `salary_increment` (updated_date,branch,designation,emp_name,emp_id,prv_sal,new_sal) VALUES  ( '$updated_date', '$branch', '$designation', '$emp_name', '$emp_id',  '$salary', '$new_salary')";
                
                $statement1 = $conn->prepare($query1)->execute($data);
                
                  if($statement1){
                      $sn= $user['sn']; 
                      $query1 = "UPDATE `emp_details` SET `salary`='$new_salary' WHERE `sn`='$sn' and `branch`='".$branch."' ";
                      $statement1 = $conn->prepare($query1)->execute($data);
                          echo "<script>alert('Salary Modification Created successfull')</script>";
                          echo "<script>window.location.href = 'salary-modification.php'</script>";
                  }
            }
       }
       else{
        $success= "Not Created";
       }
   
   }
} 

?>
<!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" />

    <title>UTCPL</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">

             <!--start breadcrumb-->
            <div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
              <div class="breadcrumb-title pe-3">Salary Modification</div>
            </div>
            <!--end breadcrumb-->

            <?php
              if(isset($success)){
            ?>
                <center><h3 style="color:green"><?php echo $success ?></h3></center>
            <?php
              }
            ?> 
            <hr/>
            <div class="row">
              <div class="col-xl-12 mx-auto">
                <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-6">
                          <label for="validationCustom01" class="form-label">Designation</label>
                          <select class="form-control" id="validationCustom01" name="designation" placeholder="Employee Designation" required>
                              <option value="">---Select Designation---</option>
                              <?php
                                $stmt = $conn->prepare("SELECT * FROM `designation` ");
                                $stmt->execute();
                                $sql1 = $stmt->fetchAll();
                                foreach($sql1 as $row) 
                                { ?>
                                  <option value="<?php echo $row['name'];?>"><?php echo $row['name'];?></option>  
                              <?php } ?>
                          </select>
                          <div class="valid-feedback">Enter Designation!</div>
                        </div>
                        <div class="col-md-6">
                          <label for="validationCustom01" class="form-label">Branch Name</label>
                          <select class="form-control" id="validationCustom01" name="branch" placeholder="Branch" required>
                              <option value="">---Select Branch---</option>
                              <?php
                                $stmt = $conn->prepare("SELECT * FROM `branch` ");
                                $stmt->execute();
                                $sql1 = $stmt->fetchAll();
                                foreach($sql1 as $row) 
                                { ?>
                                  <option value="<?php echo $row['id'];?>"><?php echo $row['name'];?></option>  
                              <?php } ?>
                          </select>
                          <div class="valid-feedback">Enter Branch!</div>
                        </div>  
                        <div class="col-md-6">
                          <label for="validationCustom01" class="form-label">Existing Salary</label>
                          <input type="text" class="form-control" id="validationCustom01" placeholder="Existing Salary" name="salary" required>
                          <div class="valid-feedback">Enter Existing Salary!</div>
                        </div>
                        <div class="col-md-6">
                          <label for="validationCustom01" class="form-label">New Salary</label>
                          <input type="text" class="form-control" id="validationCustom01" placeholder="New Salary" name="new_salary" required>
                          <div class="valid-feedback">Enter New Salary!</div>
                        </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>
<?php include'system/footer.php'?>