<?php
// Start session for CSRF protection
session_start();

// Verify CSRF token
if (!isset($_POST['csrf_token']) || $_POST['csrf_token'] !== $_SESSION['csrf_token']) {
    header('Content-Type: application/json');
    http_response_code(403);
    echo json_encode(['success' => false, 'message' => 'Invalid CSRF token']);
    exit();
}

// Database connection
require 'connect.php';

// Begin transaction
$conn->begin_transaction();

// Function to sanitize input
function sanitizeInput($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

// Function to validate required fields
function validateRequiredFields($fields) {
    foreach ($fields as $field) {
        if (empty($_POST[$field])) {
            return false;
        }
    }
    return true;
}

try {
    // Validate all required fields from step 1
    $requiredFieldsStep1 = [
        'personalno', 'tename', 'tefname', 'tege', 'tebirthday',
        'tecnic', 'temobno', 'district', 'tehsil', 'unionCouncil',
        'tecudesig', 'tecups', 'dojcp', 'current_time_scale',
        'tetimes', 'iaa', 'doapp', 'workin'
    ];
    
    if (!validateRequiredFields($requiredFieldsStep1)) {
        throw new Exception('All required fields in Step 1 must be filled');
    }

    // Process form submission
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // Step 1 Data
        $personalno = sanitizeInput($_POST["personalno"]);
        $tename = sanitizeInput($_POST["tename"]);
        $tefname = sanitizeInput($_POST["tefname"]);
        $tege = sanitizeInput($_POST["tege"]);
        $tebirthday = sanitizeInput($_POST["tebirthday"]);
        $tebplace = sanitizeInput($_POST["tebplace"]);
        $tecnic = sanitizeInput($_POST["tecnic"]);
        $teemq = sanitizeInput($_POST["teemq"]);
        $temarst = sanitizeInput($_POST["temarst"]);
        $temobno = sanitizeInput($_POST["temobno"]);
        $tewano = sanitizeInput($_POST["tewano"]);
        $tegpfno = sanitizeInput($_POST["tegpfno"]);
        $teddono = sanitizeInput($_POST["teddono"]);
        $tenok = sanitizeInput($_POST["tenok"]);
        $tenokre = sanitizeInput($_POST["tenokre"]);
        $tevillage = sanitizeInput($_POST["tevillage"]);
        $tepo = sanitizeInput($_POST["tepo"]);
        $district = sanitizeInput($_POST["district"]);
        $tehsil = sanitizeInput($_POST["tehsil"]);
        $unionCouncil = sanitizeInput($_POST["unionCouncil"]);
        $tconstituency = sanitizeInput($_POST["tconstituency"]);
        $tecudesig = sanitizeInput($_POST["tecudesig"]);
        $tecups = sanitizeInput($_POST["tecups"]);
        $dojcp = sanitizeInput($_POST["dojcp"]);
        $tetimes = sanitizeInput($_POST["tetimes"]);
        $iaa = sanitizeInput($_POST["iaa"]);
        $doapp = sanitizeInput($_POST["doapp"]);
        $workin = sanitizeInput($_POST["workin"]);
        $wtis = sanitizeInput($_POST["wtis"]);

        // Step 2 Data
        $tesscpass = sanitizeInput($_POST["tesscpass"] ?? '');
        $tesscsub = sanitizeInput($_POST["tesscsub"] ?? '');
        $tesscuni = sanitizeInput($_POST["tesscuni"] ?? '');
        $tesscom = sanitizeInput($_POST["tesscom"] ?? '');
        $tesscdiv = sanitizeInput($_POST["tesscdiv"] ?? '');

        // Step 3 Data
        $teptpass = sanitizeInput($_POST["teptpass"] ?? '');
        $teptis = sanitizeInput($_POST["teptis"] ?? '');
        $teptom = sanitizeInput($_POST["teptom"] ?? '');
        $teptdiv = sanitizeInput($_POST["teptdiv"] ?? '');
        $tebedpass = sanitizeInput($_POST["tebedpass"] ?? '');
        $tebedis = sanitizeInput($_POST["tebedis"] ?? '');
        $tebedom = sanitizeInput($_POST["tebedom"] ?? '');
        $tebeddiv = sanitizeInput($_POST["tebeddiv"] ?? '');
        $temedpass = sanitizeInput($_POST["temedpass"] ?? '');
        $temedis = sanitizeInput($_POST["temedis"] ?? '');
        $temedom = sanitizeInput($_POST["temedom"] ?? '');
        $temeddiv = sanitizeInput($_POST["temeddiv"] ?? '');
        $teo1name = sanitizeInput($_POST["teo1name"] ?? '');
        $teo1pass = sanitizeInput($_POST["teo1pass"] ?? '');
        $teo1is = sanitizeInput($_POST["teo1is"] ?? '');
        $teo1om = sanitizeInput($_POST["teo1om"] ?? '');
        $teo1div = sanitizeInput($_POST["teo1div"] ?? '');
        $teo2name = sanitizeInput($_POST["teo2name"] ?? '');
        $teo2pass = sanitizeInput($_POST["teo2pass"] ?? '');
        $teo2is = sanitizeInput($_POST["teo2is"] ?? '');
        $teo2om = sanitizeInput($_POST["teo2om"] ?? '');
        $teo2div = sanitizeInput($_POST["teo2div"] ?? '');
        $teo3name = sanitizeInput($_POST["teo3name"] ?? '');
        $teo3pass = sanitizeInput($_POST["teo3pass"] ?? '');
        $teo3is = sanitizeInput($_POST["teo3is"] ?? '');
        $teo3om = sanitizeInput($_POST["teo3om"] ?? '');
        $teo3div = sanitizeInput($_POST["teo3div"] ?? '');

        // Step 4 Data
        $tr1name = sanitizeInput($_POST["tr1name"] ?? '');
        $tr1year = sanitizeInput($_POST["tr1year"] ?? '');
        $tr1institute = sanitizeInput($_POST["tr1institute"] ?? '');
        $tr1place = sanitizeInput($_POST["tr1place"] ?? '');
        $tr1duration = sanitizeInput($_POST["tr1duration"] ?? '');
        $tr2name = sanitizeInput($_POST["tr2name"] ?? '');
        $tr2year = sanitizeInput($_POST["tr2year"] ?? '');
        $tr2institute = sanitizeInput($_POST["tr2institute"] ?? '');
        $tr2place = sanitizeInput($_POST["tr2place"] ?? '');
        $tr2duration = sanitizeInput($_POST["tr2duration"] ?? '');
        $tr3name = sanitizeInput($_POST["tr3name"] ?? '');
        $tr3year = sanitizeInput($_POST["tr3year"] ?? '');
        $tr3institute = sanitizeInput($_POST["tr3institute"] ?? '');
        $tr3place = sanitizeInput($_POST["tr3place"] ?? '');
        $tr3duration = sanitizeInput($_POST["tr3duration"] ?? '');
        $tr4name = sanitizeInput($_POST["tr4name"] ?? '');
        $tr4year = sanitizeInput($_POST["tr4year"] ?? '');
        $tr4institute = sanitizeInput($_POST["tr4institute"] ?? '');
        $tr4place = sanitizeInput($_POST["tr4place"] ?? '');
        $tr4duration = sanitizeInput($_POST["tr4duration"] ?? '');
        $tr5name = sanitizeInput($_POST["tr5name"] ?? '');
        $tr5year = sanitizeInput($_POST["tr5year"] ?? '');
        $tr5institute = sanitizeInput($_POST["tr5institute"] ?? '');
        $tr5place = sanitizeInput($_POST["tr5place"] ?? '');
        $tr5duration = sanitizeInput($_POST["tr5duration"] ?? '');

        // Step 5 Data
        $tesubject1 = sanitizeInput($_POST["tesubject1"] ?? '');
        $teclass1 = sanitizeInput($_POST["teclass1"] ?? '');
        $teperiod1 = sanitizeInput($_POST["teperiod1"] ?? '');
        $tesubject2 = sanitizeInput($_POST["tesubject2"] ?? '');
        $teclass2 = sanitizeInput($_POST["teclass2"] ?? '');
        $teperiod2 = sanitizeInput($_POST["teperiod2"] ?? '');
        $tesubject3 = sanitizeInput($_POST["tesubject3"] ?? '');
        $teclass3 = sanitizeInput($_POST["teclass3"] ?? '');
        $teperiod3 = sanitizeInput($_POST["teperiod3"] ?? '');
        $tesubject4 = sanitizeInput($_POST["tesubject4"] ?? '');
        $teclass4 = sanitizeInput($_POST["teclass4"] ?? '');
        $teperiod4 = sanitizeInput($_POST["teperiod4"] ?? '');
        $tesubject5 = sanitizeInput($_POST["tesubject5"] ?? '');
        $teclass5 = sanitizeInput($_POST["teclass5"] ?? '');
        $teperiod5 = sanitizeInput($_POST["teperiod5"] ?? '');
        $tesubject6 = sanitizeInput($_POST["tesubject6"] ?? '');
        $teclass6 = sanitizeInput($_POST["teclass6"] ?? '');
        $teperiod6 = sanitizeInput($_POST["teperiod6"] ?? '');
        $tesubject7 = sanitizeInput($_POST["tesubject7"] ?? '');
        $teclass7 = sanitizeInput($_POST["teclass7"] ?? '');
        $teperiod7 = sanitizeInput($_POST["teperiod7"] ?? '');
        $tesubject8 = sanitizeInput($_POST["tesubject8"] ?? '');
        $teclass8 = sanitizeInput($_POST["teclass8"] ?? '');
        $teperiod8 = sanitizeInput($_POST["teperiod8"] ?? '');
        $tesubject9 = sanitizeInput($_POST["tesubject9"] ?? '');
        $teclass9 = sanitizeInput($_POST["teclass9"] ?? '');
        $teperiod9 = sanitizeInput($_POST["teperiod9"] ?? '');

        // Step 6 Data
        $tepredesig1 = sanitizeInput($_POST["tepredesig1"] ?? '');
        $teprescale1 = sanitizeInput($_POST["teprescale1"] ?? '');
        $tenewdesig1 = sanitizeInput($_POST["tenewdesig1"] ?? '');
        $tenewscale1 = sanitizeInput($_POST["tenewscale1"] ?? '');
        $teprodate1 = sanitizeInput($_POST["teprodate1"] ?? '');
        $tepredesig2 = sanitizeInput($_POST["tepredesig2"] ?? '');
        $teprescale2 = sanitizeInput($_POST["teprescale2"] ?? '');
        $tenewdesig2 = sanitizeInput($_POST["tenewdesig2"] ?? '');
        $tenewscale2 = sanitizeInput($_POST["tenewscale2"] ?? '');
        $teprodate2 = sanitizeInput($_POST["teprodate2"] ?? '');
        $tepredesig3 = sanitizeInput($_POST["tepredesig3"] ?? '');
        $teprescale3 = sanitizeInput($_POST["teprescale3"] ?? '');
        $tenewdesig3 = sanitizeInput($_POST["tenewdesig3"] ?? '');
        $tenewscale3 = sanitizeInput($_POST["tenewscale3"] ?? '');
        $teprodate3 = sanitizeInput($_POST["teprodate3"] ?? '');
        $tepredesig4 = sanitizeInput($_POST["tepredesig4"] ?? '');
        $teprescale4 = sanitizeInput($_POST["teprescale4"] ?? '');
        $tenewdesig4 = sanitizeInput($_POST["tenewdesig4"] ?? '');
        $tenewscale4 = sanitizeInput($_POST["tenewscale4"] ?? '');
        $teprodate4 = sanitizeInput($_POST["teprodate4"] ?? '');
        $tepredesig5 = sanitizeInput($_POST["tepredesig5"] ?? '');
        $teprescale5 = sanitizeInput($_POST["teprescale5"] ?? '');
        $tenewdesig5 = sanitizeInput($_POST["tenewdesig5"] ?? '');
        $tenewscale5 = sanitizeInput($_POST["tenewscale5"] ?? '');
        $teprodate5 = sanitizeInput($_POST["teprodate5"] ?? '');

        // Step 7 Data
        $tetfrom1 = sanitizeInput($_POST["tetfrom1"] ?? '');
        $tetto1 = sanitizeInput($_POST["tetto1"] ?? '');
        $tetreason1 = sanitizeInput($_POST["tetreason1"] ?? '');
        $tetdate1 = sanitizeInput($_POST["tetdate1"] ?? '');
        $tetfrom2 = sanitizeInput($_POST["tetfrom2"] ?? '');
        $tetto2 = sanitizeInput($_POST["tetto2"] ?? '');
        $tetreason2 = sanitizeInput($_POST["tetreason2"] ?? '');
        $tetdate2 = sanitizeInput($_POST["tetdate2"] ?? '');
        $tetfrom3 = sanitizeInput($_POST["tetfrom3"] ?? '');
        $tetto3 = sanitizeInput($_POST["tetto3"] ?? '');
        $tetreason3 = sanitizeInput($_POST["tetreason3"] ?? '');
        $tetdate3 = sanitizeInput($_POST["tetdate3"] ?? '');
        $tetfrom4 = sanitizeInput($_POST["tetfrom4"] ?? '');
        $tetto4 = sanitizeInput($_POST["tetto4"] ?? '');
        $tetreason4 = sanitizeInput($_POST["tetreason4"] ?? '');
        $tetdate4 = sanitizeInput($_POST["tetdate4"] ?? '');
        $tetfrom5 = sanitizeInput($_POST["tetfrom5"] ?? '');
        $tetto5 = sanitizeInput($_POST["tetto5"] ?? '');
        $tetreason5 = sanitizeInput($_POST["tetreason5"] ?? '');
        $tetdate5 = sanitizeInput($_POST["tetdate5"] ?? '');

        // Handle file upload
        $photoPath = null;
        if (isset($_FILES['tchpic']) && $_FILES['tchpic']['error'] === UPLOAD_ERR_OK) {
            $allowedTypes = ['image/jpeg', 'image/png'];
            $fileType = $_FILES['tchpic']['type'];
            
            if (in_array($fileType, $allowedTypes)) {
                $uploadDir = 'uploads/employee_photos/';
                if (!file_exists($uploadDir)) {
                    mkdir($uploadDir, 0777, true);
                }
                
                $fileExt = pathinfo($_FILES['tchpic']['name'], PATHINFO_EXTENSION);
                $fileName = "emp_{$personalno}_photo." . $fileExt;
                $filePath = $uploadDir . $fileName;
                
                if (move_uploaded_file($_FILES['tchpic']['tmp_name'], $filePath)) {
                    $photoPath = $filePath;
                }
            }
        }

        // Insert data into the first step table
        $sqlStep1 = "INSERT INTO employees (
            personalno, tename, tefname, tege, tebirthday, tebplace, tecnic, teemq, temarst, 
            temobno, tewano, tegpfno, teddono, tenok, tenokre, tevillage, tepo, district, 
            tehsil, unionCouncil, tconstituency, tecudesig, tecups, dojcp, tetimes, iaa, doapp, 
            workin, wtis, photo_path
        ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        $stmt = $conn->prepare($sqlStep1);
        $stmt->bind_param(
            "ssssssssssssssssssssssssssssss",
            $personalno, $tename, $tefname, $tege, $tebirthday, $tebplace, $tecnic, $teemq, $temarst,
            $temobno, $tewano, $tegpfno, $teddono, $tenok, $tenokre, $tevillage, $tepo, $district,
            $tehsil, $unionCouncil, $tconstituency, $tecudesig, $tecups, $dojcp, $tetimes, $iaa, $doapp,
            $workin, $wtis, $photoPath
        );

        if (!$stmt->execute()) {
            throw new Exception("Error saving personal information: " . $stmt->error);
        }
        $stmt->close();

        // Insert data into the second step table (if any data exists)
        if ($tesscpass || $tesscsub || $tesscuni || $tesscom || $tesscdiv) {
            $sqlStep2 = "INSERT INTO teacher_qualification (
                personalno, tesscpass, tesscsub, tesscuni, tesscom, tesscdiv
            ) VALUES (?, ?, ?, ?, ?, ?)";

            $stmt = $conn->prepare($sqlStep2);
            $stmt->bind_param(
                "ssssss",
                $personalno, $tesscpass, $tesscsub, $tesscuni, $tesscom, $tesscdiv
            );

            if (!$stmt->execute()) {
                throw new Exception("Error saving qualifications: " . $stmt->error);
            }
            $stmt->close();
        }

        // Insert data into the third step table (if any data exists)
        if ($teptpass || $tebedpass || $temedpass || $teo1name || $teo2name || $teo3name) {
            $sqlStep3 = "INSERT INTO teacher_pro_qualification (
                personalno, teptpass, teptis, teptom, teptdiv, 
                tebedpass, tebedis, tebedom, tebeddiv, 
                temedpass, temedis, temedom, temeddiv, 
                teo1name, teo1pass, teo1is, teo1om, teo1div, 
                teo2name, teo2pass, teo2is, teo2om, teo2div, 
                teo3name, teo3pass, teo3is, teo3om, teo3div
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

            $stmt = $conn->prepare($sqlStep3);
            $stmt->bind_param(
                "ssssssssssssssssssssssssssss",
                $personalno, $teptpass, $teptis, $teptom, $teptdiv, 
                $tebedpass, $tebedis, $tebedom, $tebeddiv, 
                $temedpass, $temedis, $temedom, $temeddiv, 
                $teo1name, $teo1pass, $teo1is, $teo1om, $teo1div, 
                $teo2name, $teo2pass, $teo2is, $teo2om, $teo2div, 
                $teo3name, $teo3pass, $teo3is, $teo3om, $teo3div
            );

            if (!$stmt->execute()) {
                throw new Exception("Error saving professional qualifications: " . $stmt->error);
            }
            $stmt->close();
        }

        // Insert data into the fourth step table (if any data exists)
        if ($tr1name || $tr2name || $tr3name || $tr4name || $tr5name) {
            $sqlStep4 = "INSERT INTO teacher_training (
                personalno,
                tr1name, tr1year, tr1institute, tr1place, tr1duration,
                tr2name, tr2year, tr2institute, tr2place, tr2duration,
                tr3name, tr3year, tr3institute, tr3place, tr3duration,
                tr4name, tr4year, tr4institute, tr4place, tr4duration,
                tr5name, tr5year, tr5institute, tr5place, tr5duration
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

            $stmt = $conn->prepare($sqlStep4);
            $stmt->bind_param(
                "ssssssssssssssssssssssssss",
                $personalno,
                $tr1name, $tr1year, $tr1institute, $tr1place, $tr1duration,
                $tr2name, $tr2year, $tr2institute, $tr2place, $tr2duration,
                $tr3name, $tr3year, $tr3institute, $tr3place, $tr3duration,
                $tr4name, $tr4year, $tr4institute, $tr4place, $tr4duration,
                $tr5name, $tr5year, $tr5institute, $tr5place, $tr5duration
            );

            if (!$stmt->execute()) {
                throw new Exception("Error saving training information: " . $stmt->error);
            }
            $stmt->close();
        }

        // Insert data into the fifth step table (if any data exists)
        if ($tesubject1 || $tesubject2 || $tesubject3 || $tesubject4 || $tesubject5) {
            $sqlStep5 = "INSERT INTO teacher_subject (
                personalno,
                tesubject1, teclass1, teperiod1,
                tesubject2, teclass2, teperiod2,
                tesubject3, teclass3, teperiod3,
                tesubject4, teclass4, teperiod4,
                tesubject5, teclass5, teperiod5,
                tesubject6, teclass6, teperiod6,
                tesubject7, teclass7, teperiod7,
                tesubject8, teclass8, teperiod8,
                tesubject9, teclass9, teperiod9
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

            $stmt = $conn->prepare($sqlStep5);
            $stmt->bind_param(
                "sssssssssssssssssssssssssss",
                $personalno,
                $tesubject1, $teclass1, $teperiod1,
                $tesubject2, $teclass2, $teperiod2,
                $tesubject3, $teclass3, $teperiod3,
                $tesubject4, $teclass4, $teperiod4,
                $tesubject5, $teclass5, $teperiod5,
                $tesubject6, $teclass6, $teperiod6,
                $tesubject7, $teclass7, $teperiod7,
                $tesubject8, $teclass8, $teperiod8,
                $tesubject9, $teclass9, $teperiod9
            );

            if (!$stmt->execute()) {
                throw new Exception("Error saving subject information: " . $stmt->error);
            }
            $stmt->close();
        }

        // Insert data into the sixth step table (if any data exists)
        if ($tepredesig1 || $tepredesig2 || $tepredesig3 || $tepredesig4 || $tepredesig5) {
            $sqlStep6 = "INSERT INTO teacher_promotion (
                personalno,
                tepredesig1, teprescale1, tenewdesig1, tenewscale1, teprodate1,
                tepredesig2, teprescale2, tenewdesig2, tenewscale2, teprodate2,
                tepredesig3, teprescale3, tenewdesig3, tenewscale3, teprodate3,
                tepredesig4, teprescale4, tenewdesig4, tenewscale4, teprodate4,
                tepredesig5, teprescale5, tenewdesig5, tenewscale5, teprodate5
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

            $stmt = $conn->prepare($sqlStep6);
            $stmt->bind_param(
                "ssssssssssssssssssssssssss",
                $personalno,
                $tepredesig1, $teprescale1, $tenewdesig1, $tenewscale1, $teprodate1,
                $tepredesig2, $teprescale2, $tenewdesig2, $tenewscale2, $teprodate2,
                $tepredesig3, $teprescale3, $tenewdesig3, $tenewscale3, $teprodate3,
                $tepredesig4, $teprescale4, $tenewdesig4, $tenewscale4, $teprodate4,
                $tepredesig5, $teprescale5, $tenewdesig5, $tenewscale5, $teprodate5
            );

            if (!$stmt->execute()) {
                throw new Exception("Error saving promotion information: " . $stmt->error);
            }
            $stmt->close();
        }

        // Insert data into the seventh step table (if any data exists)
        if ($tetfrom1 || $tetfrom2 || $tetfrom3 || $tetfrom4 || $tetfrom5) {
            $sqlStep7 = "INSERT INTO teacher_transfer (
                personalno,
                tetfrom1, toto1, tetreason1, tetdate1,
                tetfrom2, toto2, tetreason2, tetdate2,
                tetfrom3, toto3, tetreason3, tetdate3,
                tetfrom4, toto4, tetreason4, tetdate4,
                tetfrom5, toto5, tetreason5, tetdate5
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

            $stmt = $conn->prepare($sqlStep7);
            $stmt->bind_param(
                "ssssssssssssssssssss",
                $personalno,
                $tetfrom1, $tetto1, $tetreason1, $tetdate1,
                $tetfrom2, $tetto2, $tetreason2, $tetdate2,
                $tetfrom3, $tetto3, $tetreason3, $tetdate3,
                $tetfrom4, $tetto4, $tetreason4, $tetdate4,
                $tetfrom5, $tetto5, $tetreason5, $tetdate5
            );

            if (!$stmt->execute()) {
                throw new Exception("Error saving transfer information: " . $stmt->error);
            }
            $stmt->close();
        }

        // Commit transaction if all queries succeeded
        $conn->commit();

        // Return success response
        header('Content-Type: application/json');
        echo json_encode([
            'success' => true,
            'message' => 'Employee data saved successfully',
            'employee_id' => $personalno
        ]);
    }
} catch (Exception $e) {
    // Rollback transaction on error
    $conn->rollback();

    // Return error response
    header('Content-Type: application/json');
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage()
    ]);
} finally {
    // Close the connection
    $conn->close();
}
?>