<?php 
    session_start();
    if(!$_SESSION['id']){
        header('location:index.php');
    }

    require_once '../system/conn.php';
?>
<?php 
if (isset($_POST['submit'])) {

    // Employee details
    $e_id = $_POST['e_id'];
    $e_name = $_POST['e_name'];
    $fname = $_POST['fname'];
    $mname = $_POST['mname'];
    $husband_wife = $_POST['husband_wife'];
    $e_doj = $_POST['e_doj'];
    $designation = $_POST['designation'];
    $dob = $_POST['dob'];
    $pan_card = $_POST['pan_card'];
    $addr = $_POST['addr'];
    $aadhar_num = $_POST['aadhar_num'];
    $mobile_num = $_POST['mobile_num'];
    $alt_mob_no = $_POST['alt_mob_no'];
    $bank_ac_no = $_POST['bank_ac_no'];
    $bank_ifsc = $_POST['bank_ifsc'];
    $bank_name = $_POST['bank_name'];
    $uan_no = $_POST['uan_no'];
    $esic_no = $_POST['esic_no'];
    $salary = $_POST['salary'];
    $status = 1;
    $updated_date = date('Y-m-d');

    // Checkbox normalization
    $ovx  = !empty($_POST['over']) ? 'yes' : '';
    $conx = !empty($_POST['con'])  ? 'y'   : '';
    $medx = !empty($_POST['med'])  ? 'y'   : '';
    $pfx  = !empty($_POST['pf'])   ? 'y'   : '';
    $esix = !empty($_POST['esi'])  ? 'y'   : '';


    if (!empty($_FILES['profile']['name'])) {
        $uploadDir = 'assets/images/employee/';

        $allowedExt = ['jpg', 'jpeg', 'png', 'webp'];
        $ext = strtolower(pathinfo($_FILES['profile']['name'], PATHINFO_EXTENSION));

        if (in_array($ext, $allowedExt)) {
            $profile = 'emp-' . date('Ymd-His') . '.' . $ext;
            move_uploaded_file($_FILES['profile']['tmp_name'], $uploadDir . $profile);
        } else {
            echo "<script>alert('Invalid image type. Only JPG, JPEG, PNG, WEBP allowed.');</script>";
        }
    }
    

    // Update query using placeholders
    $query = "UPDATE emp_details SET 
                 image = ?, e_name = ?, fname = ?, mname = ?, husband_wife = ?, e_doj = ?, designation = ?, dob = ?, pan_card = ?, addr = ?,
                aadhar_num = ?, mobile_num = ?, alt_mob_no = ?, bank_ac_no = ?, bank_ifsc = ?, bank_name = ?, ot = ?, 
                esi = ?, con = ?, pf = ?, med = ?, uan_no = ?, esic_no = ?, salary = ?, profile = ?, updated_date = ?
              WHERE e_id = ?";

    $stmt = $conn->prepare($query);
    $result = $stmt->execute([
       $profile, $e_name, $fname, $mname, $husband_wife, $e_doj, $designation, $dob, $pan_card, $addr,
        $aadhar_num, $mobile_num, $alt_mob_no, $bank_ac_no, $bank_ifsc, $bank_name, $ovx,
        $esix, $conx, $pfx, $medx, $uan_no, $esic_no, $salary, $profile, $updated_date,
        $e_id, $branch
    ]);

    if ($result) {
        echo "<script>alert('Employee details updated successfully.');</script>";
        echo "<script>window.location.href = 'all-employees.php';</script>";
    } else {
        echo "Failed to update employee details.";
    }
}
?>
<!doctype html>
<html lang="en" class="semi-dark">
  <head>
    
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
	<link href="assets/css/pace.min.css" rel="stylesheet" />
	<script src="assets/js/pace.min.js"></script>

    <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 -->
    <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>Edit Employee</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">
                
                <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" >
                        <h6 class="mb-0 text-uppercase">Edit Employee Details :</h6>
                        <hr/>
                        <?php
                          $sth = $conn->prepare(" SELECT * FROM `emp_details` where `e_id`=?");
                          $sth->execute([$_GET['id']]);
                          $user = $sth->fetch(PDO::FETCH_ASSOC);
                        
                        ?>
                        <!-- PASSPORT SIZE IMAGE (OPTIONAL) -->
                        <input type="hidden" class="form-control" name="e_id" value="<?php echo $user['e_id']; ?>" required placeholder="Enter Employee Name">
    <div class="col-md-4">
        
        <img id="previewImage"
             src="<?php echo !empty($user['image']) ? '../assets/images/employee/'.$user['image'] : 'https://placehold.co/80x80/000000/FFFFFF/?text=NO+IMAGE'; ?>"
             alt="Preview"
             class="text-center mx-auto img-thumbnail mb-2"
             style="max-width:120px;height:150px;object-fit:cover;">
             <div>
                 <label class="form-label text-left">Passport Size Photo<small class="text-muted">(Optional)</small></label>
        <input type="file"
               class="form-control"
               name="profile"
               accept="image/*"
               onchange="previewPassport(this)">

             </div>
             
    </div>
<div class="col-md-8 row">
    <!-- EMPLOYEE NAME -->
    <div class="col-md-12">
        <label class="form-label">Employee Name <span class="text-danger">*</span></label>
        <input type="text" class="form-control" name="e_name" required placeholder="Enter Employee Name" value="<?php echo $user['e_name']; ?>">
    </div>

    <!-- DESIGNATION -->
<div class="col-md-12">
    <label class="form-label">Employee Designation <span class="text-danger">*</span></label>
    <select class="form-control select2" name="designation" required>
        <option value="">---Select Designation---</option>
        <?php
        $stmt = $conn->prepare("SELECT * FROM `designation`");
        $stmt->execute();
        foreach ($stmt->fetchAll() as $row) { ?>
            <option value="<?= $row['name']; ?>" <?php echo ($user['designation'] == $row['name']) ? 'selected' : ''; ?>><?= $row['name']; ?></option>
        <?php } ?>
    </select>
</div>
<div class="col-md-6">
                          <label for="validationCustom01" class="form-label">Father Name</label>
                          <input type="text" class="form-control" id="validationCustom01" placeholder="Father Name" name="fname" value="<?php echo $user['fname']; ?>" required>
                          <div class="valid-feedback">Enter Father Name!</div>
                        </div>
                        <div class="col-md-6">
                          <label for="validationCustom01" class="form-label">Mother Name</label>
                          <input type="text" class="form-control" id="validationCustom01" placeholder="Mother Name" name="mname" value="<?php echo $user['mname']; ?>" required>
                          <div class="valid-feedback">Enter Mother Name!</div>
                        </div>
                        </div>
                        
                        <div class="col-md-6">
                          <label for="validationCustom01" class="form-label">Husband/ Wife Name</label>
                          <input type="text" class="form-control" id="validationCustom01" placeholder="Husband/ Wife Name" name="husband_wife" value="<?php echo $user['husband_wife']; ?>" required>
                          <div class="valid-feedback">Enter Husband/ Wife Name!</div>
                        </div>
                        <div class="col-md-6">
                          <label for="validationCustom01" class="form-label">Joining Date</label>
                          <input type="date" class="form-control" id="validationCustom01" placeholder="Joining Date" name="e_doj" value="<?php echo $user['e_doj']; ?>" required>
                          <div class="valid-feedback">Enter Valid Date!</div>
                        </div>
                        <div class="col-md-6">
                          <label for="validationCustom01" class="form-label">DOB</label>
                          <input type="date" class="form-control" id="validationCustom01" placeholder="DOB" name="dob" value="<?php echo $user['dob']; ?>" required>
                          <div class="valid-feedback">Enter Valid Date!</div>
                        </div>
                        <div class="col-md-6">
                          <label for="validationCustom01" class="form-label">Aadhar Number</label>
                          <input type="text" class="form-control" id="validationCustom01" placeholder="Enter Aadhar Number" name="aadhar_num"  value="<?php echo $user['aadhar_num']; ?>" required>
                          <div class="valid-feedback">Enter Aadhar Number!</div>
                        </div>
                        <div class="col-md-6">
                          <label for="validationCustom01" class="form-label">Mobile Number</label>
                          <input type="tel" class="form-control" id="validationCustom01" placeholder="Enter Mobile Number" name="mobile_num" value="<?php echo $user['mobile_num']; ?>" required>
                          <div class="valid-feedback">Enter Mobile Number!</div>
                        </div>
                        <div class="col-md-6">
                          <label for="validationCustom01" class="form-label">Alternate Mobile Number</label>
                          <input type="tel" class="form-control" id="validationCustom01" placeholder="Enter Alternate Mobile Number" name="alt_mob_no" value="<?php echo $user['alt_mob_no']; ?>" required>
                          <div class="valid-feedback">Enter Mobile Number!</div>
                        </div>

                        <div class="col-md-12">
                          <label for="validationCustom01" class="form-label">Address</label>
                          <textarea class="form-control" id="validationCustom01" placeholder="Address" name="addr" rows="4" required ><?php echo $user['addr']; ?></textarea>
                          <div class="valid-feedback">Enter Address!</div>
                        </div>
                        
                        <h6 class="mb-0 text-uppercase">Salary Detail :</h6>
                        <hr/>
                        
                        <div class="col-md-4">
                          <label for="validationCustom01" class="form-label">Bank A.C Number</label>
                          <input type="text" class="form-control" id="validationCustom01" placeholder="Enter Bank A.C Number" name="bank_ac_no" value="<?php echo $user['bank_ac_no']; ?>" required>
                          <div class="valid-feedback">Enter Bank A.C Number !</div>
                        </div>
                        <div class="col-md-4">
                          <label for="validationCustom01" class="form-label">Bank IFSC Code</label>
                          <input type="text" class="form-control" id="validationCustom01" placeholder="Enter Bank IFSC Code" name="bank_ifsc" value="<?php echo $user['bank_ifsc']; ?>" required>
                          <div class="valid-feedback">Enter Bank IFSC Code!</div>
                        </div>
                        <div class="col-md-4">
                          <label for="validationCustom01" class="form-label">Bank Name</label>
                          <input type="text" class="form-control" id="validationCustom01" placeholder="Bank Name" name="bank_name" value="<?php echo $user['bank_name']; ?>" required>
                          <div class="valid-feedback">Enter Bank Name!</div>
                        </div>
                        <div class="col-md-6">
                          <label for="validationCustom01" class="form-label">Pancard</label>
                          <input type="text" class="form-control" id="validationCustom01" placeholder="Enter Pancard" name="pan_card" value="<?php echo $user['pan_card']; ?>" required>
                          <div class="valid-feedback">Enter Pancard !</div>
                        </div>
                        <div class="col-md-6">
                          <label for="validationCustom01" class="form-label">Gross Salary</label>
                          <input type="text" class="form-control" id="validationCustom01" placeholder="Enter Gross Salary" name="salary" value="<?php echo $user['salary']; ?>" required>
                          <div class="valid-feedback">Enter Gross Salary!</div>
                        </div>
                        <div class="col-md-6">
                          <label for="validationCustom01" class="form-label">UAN Number </label>
                          <input type="text" class="form-control" id="validationCustom01" placeholder="UAN Number" name="uan_no" value="<?php echo $user['uan_no']; ?>" required>
                          <div class="valid-feedback">Enter UAN Number!</div>
                        </div>
                        <div class="col-md-6">
                          <label for="validationCustom01" class="form-label">ESIC Number </label>
                          <input type="text" class="form-control" id="validationCustom01" placeholder="ESIC Number" name="esic_no" value="<?php echo $user['esic_no']; ?>" required>
                          <div class="valid-feedback">Enter ESIC Number!</div>
                        </div>
                        
                        <div class="col-md-4">
                          <label class="form-check-label" for="validationFormCheck1">OT Allowed</label>
                          <input type="checkbox" class="form-check-input" id="validationFormCheck1" name="over"  value="<?php echo $user['ot']; ?>" <?php if($user['ot']=='yes'){ echo "checked";} else { }  ?> required="">
                          <div class="invalid-feedback">OT Allowed</div>
                        </div>
                        <div class="col-md-4">
                          <label class="form-check-label" for="validationFormCheck1">Convyance allowance</label>
                          <input type="checkbox" class="form-check-input" id="validationFormCheck1" name="con" value="<?php echo $user['con']; ?>" <?php if($user['con']=='y'){ echo "checked";} else { } ?> required="">
                          <div class="invalid-feedback">Convyance allowance</div>
                        </div>
                        <div class="col-md-4">
                          <label class="form-check-label" for="validationFormCheck1">Medical allowance</label>
                          <input type="checkbox" class="form-check-input" id="validationFormCheck1" name="med" value="<?php echo $user['med']; ?>" <?php if($user['med']=='y'){ echo "checked";} else { } ?> required="">
                          <div class="invalid-feedback">Medical allowance</div>
                        </div>
                        <div class="col-md-6">
                          <label class="form-check-label" for="validationFormCheck1">PF Allowed</label>
                          <input type="checkbox" class="form-check-input" id="validationFormCheck1" name="pf" value="<?php echo $user['pf']; ?>" <?php if($user['pf']=='y'){ echo "checked";} else { } ?> required="">
                          <div class="invalid-feedback">PF Allowed</div>
                        </div>
                        <div class="col-md-6">
                          <label class="form-check-label" for="validationFormCheck1">ESI Allowed</label>
                          <input type="checkbox" class="form-check-input" id="validationFormCheck1" name="esi" value="<?php echo $user['esi']; ?>" <?php if($user['esi']=='y'){ echo "checked";} else { } ?> required="">
                          <div class="invalid-feedback">ESI Allowed</div>
                        </div>
                        
                        <div class="col-12"><center><br>
                          <button class="btn btn-primary" type="submit" name="submit" value="submit" >Save Changes</button></center>
                        </div>
                      </form>
                    </div>
                  </div>
                </div>
                
                
              </div>
            </div>
            <!--end row-->


          </div>
          <!-- end page content-->
         </div>
<?php include'system/footer.php'?>