<?php
// delete_professional_ajax.php
require_once 'session_config_emp.php';

// Check if employee is logged in
if (!isset($_SESSION['employee_id']) || !isset($_SESSION['cnic'])) {
    header('Content-Type: application/json');
    echo json_encode(['success' => false, 'message' => 'Unauthorized access']);
    exit();
}

include 'connect.php';

// Initialize response
$response = ['success' => false, 'message' => ''];

// Check if qualification_id is provided
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['qualification_id'])) {
    $qualification_id = intval($_POST['qualification_id']);
    
    if ($qualification_id <= 0) {
        $response['message'] = "Invalid qualification ID.";
        echo json_encode($response);
        exit();
    }
    
    // Get employee's db_employee_id
    $employee_id = $_SESSION['employee_id'];
    $cnic = $_SESSION['cnic'];
    
    $sql_reg = "SELECT emp_numb FROM registrations_emp WHERE id = ? AND cnic = ?";
    $stmt_reg = $conn->prepare($sql_reg);
    $stmt_reg->bind_param("is", $employee_id, $cnic);
    $stmt_reg->execute();
    $result_reg = $stmt_reg->get_result();
    
    if ($result_reg->num_rows > 0) {
        $reg_data = $result_reg->fetch_assoc();
        $personal_no = $reg_data['emp_numb'];
        
        // Get employee_id from employees table
        $emp_sql = "SELECT employee_id FROM employees WHERE personal_no = ? LIMIT 1";
        $stmt_emp = $conn->prepare($emp_sql);
        $stmt_emp->bind_param("s", $personal_no);
        $stmt_emp->execute();
        $emp_result = $stmt_emp->get_result();
        
        if ($emp_result->num_rows > 0) {
            $employee_data = $emp_result->fetch_assoc();
            $db_employee_id = $employee_data['employee_id'];
            
            // Check if the qualification belongs to this employee
            $check_sql = "SELECT qualification_id FROM professional_qualifications WHERE qualification_id = ? AND employee_id = ?";
            $stmt_check = $conn->prepare($check_sql);
            $stmt_check->bind_param("ii", $qualification_id, $db_employee_id);
            $stmt_check->execute();
            $check_result = $stmt_check->get_result();
            
            if ($check_result->num_rows > 0) {
                // Delete qualification
                $delete_sql = "DELETE FROM professional_qualifications WHERE qualification_id = ? AND employee_id = ?";
                $stmt_delete = $conn->prepare($delete_sql);
                $stmt_delete->bind_param("ii", $qualification_id, $db_employee_id);
                
                if ($stmt_delete->execute()) {
                    $response['success'] = true;
                    $response['message'] = "Professional qualification deleted successfully!";
                } else {
                    $response['message'] = "Failed to delete qualification: " . $conn->error;
                }
                $stmt_delete->close();
            } else {
                $response['message'] = "Qualification not found or you don't have permission to delete it.";
            }
            $stmt_check->close();
            $stmt_emp->close();
        } else {
            $response['message'] = "Employee profile not found.";
        }
    } else {
        $response['message'] = "Registration record not found.";
    }
    $stmt_reg->close();
} else {
    $response['message'] = "Invalid request.";
}

$conn->close();

// Return JSON response
header('Content-Type: application/json');
echo json_encode($response);
exit();
?>