<?php
// Start session
require_once 'session_config_emp.php';

// Check if employee is logged in
if (!isset($_SESSION['employee_id']) || !isset($_SESSION['cnic'])) {
    header('Location: login_emp.php');
    exit();
}

include 'connect.php';

// Initialize variables
$success = '';
$error = '';

// Check if training_id is provided
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['training_id'])) {
    $training_id = intval($_POST['training_id']);
    
    if ($training_id <= 0) {
        $_SESSION['error_message'] = "Invalid training ID.";
        header("Location: training_view.php");
        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'];
            
            // First, check if the training belongs to this employee
            $check_sql = "SELECT training_id FROM trainings WHERE training_id = ? AND employee_id = ?";
            $stmt_check = $conn->prepare($check_sql);
            $stmt_check->bind_param("ii", $training_id, $db_employee_id);
            $stmt_check->execute();
            $check_result = $stmt_check->get_result();
            
            if ($check_result->num_rows > 0) {
                // Delete training
                $delete_sql = "DELETE FROM trainings WHERE training_id = ? AND employee_id = ?";
                $stmt_delete = $conn->prepare($delete_sql);
                $stmt_delete->bind_param("ii", $training_id, $db_employee_id);
                
                if ($stmt_delete->execute()) {
                    $_SESSION['success_message'] = "Training record deleted successfully!";
                } else {
                    $_SESSION['error_message'] = "Failed to delete training: " . $conn->error;
                }
                $stmt_delete->close();
            } else {
                $_SESSION['error_message'] = "Training not found or you don't have permission to delete it.";
            }
            $stmt_check->close();
            $stmt_emp->close();
        } else {
            $_SESSION['error_message'] = "Employee profile not found.";
        }
    } else {
        $_SESSION['error_message'] = "Registration record not found.";
    }
    $stmt_reg->close();
} else {
    $_SESSION['error_message'] = "Invalid request.";
}

$conn->close();

// Redirect back to trainings page
header("Location: training_view.php");
exit();
?>