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

  require_once '../system/conn.php';


    $stmt = $conn->prepare("SELECT * FROM `emp_details` ORDER BY sn DESC LIMIT 1");
    $stmt->execute();
    $sql1 = $stmt->fetch(PDO::FETCH_ASSOC);
    $new_sn = ($sql1) ? $sql1['sn'] + 1 : 1;
    $e_id = "UTC/" . $new_sn;

    // Fetch Candidate Details
    $candidate_id = $_GET['id']; // Get candidate id from URL
    $stmt = $conn->prepare("SELECT * FROM `candidate_details` WHERE id = ?");
    $stmt->execute([$candidate_id]);
    $candidate = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$candidate) {
        echo "<script>alert('Candidate not found!');</script>";
        exit();
    }

    // Assign Values from Candidate Details
    $e_photo = $candidate['photo'];
    $e_name = $candidate['full_name'];
    $fname = $candidate['father_name'];
    $mname = $candidate['mother_name'];
    $husband_wife = $candidate['spouse_name'];
    $e_doj = date('Y-m-d'); // Joining Date
    $profile = $candidate['desired_position'];
    $aadhar_num = $candidate['aadhar_number'];
    $mobile_num = $candidate['phone_number'];
    $alt_mob_no = $candidate['alternate_phone'];
    $designation = $candidate['desired_position'];
    $dob = $candidate['date_of_birth'];
    $addr = $candidate['address'];
    $salary = $candidate['salary']; // Set salary manually or fetch from input
    $esic_no = ""; // ESIC number (if applicable)
    $branch = $candidate['site'];
    $status = 1;
    $updated_date = date('Y-m-d');

    // Define old and new image paths
$oldImagePath = "http://hr.utcpl.co.in/hrm/assets/images/candidates/" . $e_photo;
$newImagePath = "http://hr.utcpl.co.in/hrm/assets/images/employee/" . $e_photo;

// Check if the file exists
if (file_exists($oldImagePath)) {
    // Copy the file instead of moving
    if (copy($oldImagePath, $newImagePath)) {
        echo "Image copied successfully.";
    } else {
        echo "Failed to copy the image.";
    }
} else {
    echo "Image does not exist at the old location.";
}


    $query = "INSERT INTO `emp_details` 
        (`e_id`, `image`, `e_name`, `fname`, `mname`, `husband_wife`, `e_doj`, `profile`, 
        `aadhar_num`, `mobile_num`, `alt_mob_no`, `designation`, `dob`, `addr`, `salary`, 
        `esic_no`, `branch`, `status`, `updated_date`) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

    $statement = $conn->prepare($query)->execute([
        $e_id, $e_photo, $e_name, $fname, $mname, $husband_wife, $e_doj, $profile, 
        $aadhar_num, $mobile_num, $alt_mob_no, $designation, $dob, $addr, $salary, 
        $esic_no, $branch, $status, $updated_date
    ]);

    if ($statement) {
        $stmtCand = $conn->prepare("UPDATE `candidate_details` SET `convert_status` = 1 WHERE `id` = ?");
$stmtCand->execute([$candidate['id']]);

        echo "<script>alert('Converted to Employee added successfully.')</script>";
        echo "<script>window.location.href = 'all-employees.php'</script>";
    } else {
        echo "<script>alert('Database insertion failed!');</script>";
    }

?>
