<?php 
    session_start();
    if(!$_SESSION['id']){
        header('location:index.php');
    }
      require_once '../system/conn.php';
?>
<?php 
if(isset($_POST["update"])) {
    $candidate_id = $_GET['id'];
    $full_name = $_POST['full_name'];
    $father_name = $_POST['father_name'];
    $mother_name = $_POST['mother_name'];
    $spouse_name = $_POST['spouse_name'];
    $date_of_application = $_POST['date_of_application'];
    $desired_position = $_POST['desired_position'];
    $nationality = $_POST['nationality'];
    $religion = $_POST['religion'];
    $gender = $_POST['gender'];
    $height = $_POST['height'];
    $weight = $_POST['weight'];
    $marital_status = $_POST['marital_status'];
    $education = $_POST['education'];
    $language = $_POST['language'];
    $experience = $_POST['experience'];
    $blood_group = $_POST['blood-group'];
    $aadhar_number = $_POST['aadhar_number'];
    $phone_number = $_POST['phone_number'];
    $alternate_phone = $_POST['alternate_phone'];
    $date_of_birth = $_POST['date_of_birth'];
    $address = $_POST['address'];
    $updated_date = date('Y-m-d');
    $doi = $_POST['doi'];
    $doj = $_POST['doj'];
    $probation = $_POST['period'];
    $salary = $_POST['salary'];
    $duty = $_POST['duty'];
    $site = $_POST['site'];
    $query = "UPDATE `candidate_details` SET 
              `full_name`=?, `father_name`=?, `mother_name`=?, `spouse_name`=?, `date_of_application`=?,
              `desired_position`=?, `nationality`=?, `religion`=?, `gender`=?, `height`=?, `weight`=?, 
              `maritial-status`=?, `education`=?, `language`=?, `experience`=?, `blood-group`=?, 
              `aadhar_number`=?, `phone_number`=?, `alternate_phone`=?, `date_of_birth`=?, `address`=?,`doi`=?,`doj`=?,`probation`=?,`salary`=?,`duty`=?,`site`=?, 
              `updated_date`=? 
              WHERE `id`=?";
    
    $statement = $conn->prepare($query);
    $success = $statement->execute([
        $full_name, $father_name, $mother_name, $spouse_name, $date_of_application, $desired_position, 
        $nationality, $religion, $gender, $height, $weight, $marital_status, $education, $language, 
        $experience, $blood_group, $aadhar_number, $phone_number, $alternate_phone, $date_of_birth, 
        $address,$doi,$doj,$probation,$salary,$duty,$site, $updated_date, $candidate_id
    ]);
    
    if ($success) {
        echo "<script>alert('Candidate details updated successfully.')</script>";
        echo "<script>window.location.href = 'all-candidates.php'</script>";
    } else {
        echo "<script>alert('Update failed!');</script>";
    }
} 
?>


<!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">Edit Candidate</div>
            </div>
            <!--end breadcrumb-->


            <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="">
    <h4 class="mb-0 text-primary">Edit Candidate Details:</h4>
    <hr/>
    <?php

                          $stmt = $conn->prepare("SELECT * FROM `candidate_details` WHERE `id`=?");
                          $stmt->execute([$_GET['id']]);
                          $users = $stmt->fetchAll();
                          foreach($users as $row) 
                          {  
                         ?>
    <div class="col-md-6">
        <label class="form-label">Full Name:</label>
        <input type="text" class="form-control" placeholder="Enter Full Name" name="full_name" value="<?php echo $row['full_name']; ?>" required>
    </div>
    <div class="col-md-6">
        <label class="form-label">Father's Name:</label>
        <input type="text" class="form-control" placeholder="Enter Father's Name" name="father_name" value="<?php echo $row['father_name']; ?>" required>
    </div>
    <div class="col-md-6">
        <label class="form-label">Mother's Name:</label>
        <input type="text" class="form-control" placeholder="Enter Mother's Name" name="mother_name" value="<?php echo $row['mother_name']; ?>" required>
    </div>
    <div class="col-md-6">
        <label class="form-label">Spouse Name (if applicable):</label>
        <input type="text" class="form-control" placeholder="Enter Spouse Name" name="spouse_name" value="<?php echo $row['spouse_name']; ?>">
    </div>
    
    <div class="col-md-6">
        <label class="form-label">Date of Application:</label>
        <input type="date" class="form-control" name="date_of_application" required value="<?php echo $row['date_of_application']; ?>">
    </div>
    <div class="col-md-6">
        <label class="form-label">Desired Position:</label>
        <input type="text" class="form-control" placeholder="Enter Desired Position" name="desired_position" value="<?php echo $row['desired_position']; ?>" required>
    </div>
    
    <div class="col-md-6">
        <label class="form-label">Nationality:</label>
        <input type="text" class="form-control" placeholder="Enter Nationality" name="nationality" value="<?php echo $row['nationality']; ?>" required>
    </div>
    <div class="col-md-6">
        <label class="form-label">Religion:</label>
        <input type="text" class="form-control" placeholder="Enter Religion" name="religion" value="<?php echo $row['religion']; ?>">
    </div>
    
    <div class="col-md-6">
        <label class="form-label">Gender:</label>
        <select class="form-control" name="gender" required>
            <option value="">Select Gender</option>
            <option value="Male" <?php echo ($row['gender'] == 'Male') ? 'selected' : ''; ?>>Male</option>
            <option value="Female" <?php echo ($row['gender'] == 'Female') ? 'selected' : ''; ?>>Female</option>
            <option value="Other" <?php echo ($row['gender'] == 'Other') ? 'selected' : ''; ?>>Other</option>
        </select>
    </div>
    <div class="col-md-6">
        <label class="form-label">Marital Status:</label>
        <select class="form-control" name="marital_status" required>
            <option value="">Select Status</option>
            <option value="Single" <?php echo ($row['marital_status'] == 'Single') ? 'selected' : ''; ?>>Single</option>
            <option value="Married" <?php echo ($row['marital_status'] == 'Married') ? 'selected' : ''; ?>>Married</option>
        </select>
    </div>
    <div class="col-md-6">
        <label class="form-label">Height (cm):</label>
        <input type="number" class="form-control" placeholder="Enter Height" name="height" value="<?php echo $row['height']; ?>">
    </div>
    <div class="col-md-6">
        <label class="form-label">Weight (kg):</label>
        <input type="number" class="form-control" placeholder="Enter Weight" name="weight" value="<?php echo $row['weight']; ?>">
    </div>
    
    <div class="col-md-6">
        <label class="form-label">Education:</label>
        <input type="text" class="form-control" placeholder="Enter Highest Qualification" name="education" required value="<?php echo $row['education']; ?>">
    </div>
    <div class="col-md-6">
        <label class="form-label">Languages Known:</label>
        <input type="text" class="form-control" placeholder="Enter Languages" name="language" value="<?php echo $row['language']; ?>">
    </div>
    
    <div class="col-md-6">
        <label class="form-label">Experience (Years):</label>
        <input type="number" class="form-control" placeholder="Enter Experience" name="experience" value="<?php echo $row['experience']; ?>">
    </div>
    <div class="col-md-6">
        <label class="form-label">Blood Group:</label>
        <input type="text" class="form-control" placeholder="Enter Blood Group" name="blood-group" value="<?php echo $row['blood-group']; ?>">
    </div>
    





    <div class="col-md-12">
        <label class="form-label">Aadhar Number:</label>
        <input type="text" class="form-control" placeholder="Enter Aadhar Number" name="aadhar_number" value="<?php echo $row['aadhar_number']; ?>">
    </div>
    <div class="col-md-4">
        <label class="form-label">Phone Number:</label>
        <input type="tel" class="form-control" placeholder="Enter Phone Number" name="phone_number" value="<?php echo $row['phone_number']; ?>">
    </div>
    
    <div class="col-md-4">
        <label class="form-label">Alternate Phone:</label>
        <input type="tel" class="form-control" placeholder="Enter Alternate Number" name="alternate_phone" value="<?php echo $row['alternate_phone']; ?>">
    </div>
    <div class="col-md-4">
        <label class="form-label">Date of Birth:</label>
        <input type="date" class="form-control" name="date_of_birth" value="<?php echo $row['date_of_birth']; ?>">
    </div>
    <div class="col-md-4">
        <label class="form-label">Probation period(in months):</label>
        <input type="number" class="form-control" name="period" value="<?php echo $row['probation']; ?>">
    </div>
    <div class="col-md-4">
        <label class="form-label">Date of Interview:</label>
        <input type="date" class="form-control" name="doi" value="<?php echo $row['doi']; ?>">
    </div>
    <div class="col-md-4">
        <label class="form-label">Date of Joining:</label>
        <input type="date" class="form-control" name="doj" value="<?php echo $row['doj']; ?>">
    </div>
    <div class="col-md-4">
        <label class="form-label">Salary:</label>
        <input type="number" class="form-control" name="salary" value="<?php echo $row['salary']; ?>">
    </div>
    <div class="col-md-4">
        <label class="form-label">Duty Hours:</label>
        <input type="number" class="form-control" name="duty" value="<?php echo $row['duty']; ?>">
    </div>
    <div class="col-md-4">
        <label class="form-label">Working Site:</label>
        <select class="form-select" name="site">
            <option value="" disabled selected>-please choose-</option>
            <?php
                $sites = $conn->prepare("SELECT * FROM `site`");
                $sites->execute();
                foreach($sites->fetchAll(PDO::FETCH_ASSOC) as $site){
            ?>
            <option value="<?php echo $site['id']; ?>" 
                <?php echo ($site['id'] == $row['site']) ? 'selected' : ''; ?>>
                <?php echo $site['name']; ?>
            </option>
            <?php } ?>
        </select>
    </div>

    <div class="col-md-12">
        <label class="form-label">Address:</label>
        <textarea class="form-control" placeholder="Enter Address" name="address" rows="3" required><?php echo $row['address']; ?></textarea>
    </div>
    <?php } ?>
    <div class="col-12">
        <button class="btn btn-success" type="submit" name="update" value="submit">Save Changes</button>
    </div>
</form>

                    </div>
                  </div>
                </div>
                
                
              </div>
            </div>
            <!--end row-->


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