<?php
include($_SERVER['DOCUMENT_ROOT'] . "/API-Others/connection/conn.php");
error_reporting(E_ALL);
ini_set('display_errors', '1');
header('content-type:application/json');
if (isset($_GET['reasonForTermination'])) {
    $terminated_by = $_GET["terminated_by"];
    $unique_user_id = $_GET['unique_user_id'];
    $reason = $_GET['reasonForTermination'];
    $sql = "INSERT INTO terminatedappointments 
    (unique_user_id, profile_photo, title, first_name, middle_name, last_name, gender, dob, nationality, address, phone, whatsapp_no, email, role, qualification, department, employment_status, salary_wage_information, means_of_remuneration, account_name, account_number, ssnit_number, emergency_phone, safety_training_completion, medical_conditions_allergies, status, reasonfortermination, terminated_by, date_of_application, date_of_termination)
     SELECT unique_user_id, profile_photo, title, first_name, middle_name, last_name, gender, dob, nationality, address, phone, whatsapp_no, email, role, qualification, department, employment_status, salary_wage_information, means_of_remuneration, account_name, account_number, ssnit_number, emergency_phone, safety_training_completion, medical_conditions_allergies, 'In-active', '$reason', '$terminated_by', date_of_application,'" . DATE("Y-m-d") . "' FROM staff WHERE unique_user_id = '$unique_user_id'";
    if (mysqli_query($conn, $sql)) {
        $statusCode = 200;
        $statusMessage = "Staff appointment terminated successfully";
    } else {
        $statusCode = 1042;
        $statusMessage = mysqli_error($conn);
    }
    $result = mysqli_query($conn, "DELETE FROM staff WHERE unique_user_id='$unique_user_id'");
    $result = mysqli_query($conn, "DELETE FROM users WHERE unique_user_id='$unique_user_id'");
}


if (isset($_GET['deletingID'])) {
    $unique_user_id = $_GET['deletingID'];
    $sql = "SHOW TABLES";
    $result = mysqli_query($conn, $sql);
    while ($row = mysqli_fetch_row($result)) {
        $query1 = mysqli_query($conn, "SHOW COLUMNS FROM " . $row[0]);
        while ($spec1 = mysqli_fetch_row($query1)) {
            if ($spec1[0] == "unique_user_id") {
                $sql = ("DELETE FROM $row[0] WHERE unique_user_id='$unique_user_id'");
                if (mysqli_query($conn, $sql)) {
                    $statusCode = 200;
                    $statusMessage = "Staff appointment deletd successfully";
                } else {
                    $statusCode = 1042;
                    $statusMessage = mysqli_error($conn);
                }
            }
        }
    }
}


// $sql = ("DELETE FROM users WHERE unique_user_id = '$unique_user_id'");
// if (mysqli_query($conn, $sql)) {
//     $statusCode = 200;
//     $statusMessage = "User Deleted Successfully";
// } else {
//     $statusCode = 1042;
//     $statusMessage = mysqli_error($conn);
// }



$resultData = array('code' => $statusCode, 'message' => $statusMessage);
echo json_encode($resultData);
