<?php
// Start the session
session_start();
error_reporting(0);

// Check if user is logged in
if (strlen($_SESSION['user_id']) == 0) {
    header('location:index.php');
    exit;
}
   function convertToBanglaNumbers($input)
            {
                $englishToBanglaDigits = [
                    '0' => '০',
                    '1' => '১',
                    '2' => '২',
                    '3' => '৩',
                    '4' => '৪',
                    '5' => '৫',
                    '6' => '৬',
                    '7' => '৭',
                    '8' => '৮',
                    '9' => '৯'
                ];
                return strtr($input, $englishToBanglaDigits);
            }
// Check if 'nid' and 'id' are set in the URL
if (isset($_GET["nid"]) && isset($_GET["id"])) {
    $nid = $_GET["nid"];
    $id = $_GET["id"];

    // Establish database connection
    include_once("includes/configuration.php");

    // Prepare SQL query to fetch data based on nid and id
    $sql = "SELECT * FROM server_copy WHERE nationalId = ? AND id = ?";
    $stmt = $conn->prepare($sql);

    // Check if prepare was successful
    if ($stmt === false) {
        die("Prepare failed: " . htmlspecialchars($conn->error));
    }

    // Bind parameters (assuming nid is a string and id is an integer)
    $stmt->bind_param("si", $nid, $id); // "s" for string, "i" for integer

    // Execute the statement
    $stmt->execute();

    // Get the result
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        // Fetch data
        while ($data = $result->fetch_assoc()) {
            // Access the fetched data
            $nameBn = $data['nameBn'];
            $nameEn = $data['nameEn'];
            $nid = $data['nationalId'];
            $pin = $data['pin'];
            $sl_no = $data['sl_no'];
            $voter_aria_code = $data['voter_aria_code'];
            $voter_no = $data['voter_no'];
            $voter_area = $data['vuter_area'];
            $dobs = $data['dateOfBirth'];
            $father = $data['father'];
            $mother = $data['mother'];
            $spouse = $data['spouse'];
            $gender = $data['gender'];
            $photoPath = $data['photo_path'];
            $permanentAddress = $data['permanent_address'];
            $presentAddress = $data['present_address'];
            $bloodGroup = $data['bloodGroup'];
            $permanentaddress = convertToBanglaNumbers($data['permanentAddressLine']);
            $presentaddress = convertToBanglaNumbers($data['presentAddressLine']);
            $occupation = $data['occupation'];
            $fatherNID = $data['fatherNID'];
            $motherNID = $data['motherNID'];
            $birthPlace = $data['birthPlace'];
            $religion = $data['religion'];
            $phone = $data['mobile'];
            $signPath = $data['sign_path'];
            $nidAddress = $data['nidAddress'];
            $education = $data['education'];


         

            $nidAddress = convertToBanglaNumbers($nidAddress);

            // dob foramt
            if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $dobs)) {
                $dob = $dobs;
            } else {
                $date = DateTime::createFromFormat('d M Y', $dobs);
                $dob = $date ? $date->format('Y-m-d') : null;
            }

               
         
          
  
          
          if(!empty($education)){
            $filter_religion = "";
          }else{
             $filter_religion = $religion;
          } 


           

        }
    } else {
        echo "No records found for the given NID and ID.";
    }

    // Close the statement and connection
    $stmt->close();
    $conn->close();
} else {
    echo "NID or ID not set.";
}


function renderRow($label, $value)
{
    if (!empty($value) && $value !== "N/A") {
        echo "<tr><td><strong>$label</strong></td><td>$value</td></tr>";
    }
}

$data = [
    "national_id" => $nid,
    "pin_number" => $pin,
    "fatherNID" => $fatherNID,
    "motherNID" => $motherNID,
    "voter_area" => $voter_area,
    "name_bangla" => $nameBn,
    "name_english" => $nameEn,
    "dob" => $dob,
    "father_name" => $father,
    "mother_name" => $mother,
    "spouse_name" => $spouse,
    "gender" => $gender,
    "religion" => $filter_religion,
    "birthplace" => $birthPlace,
    "current_address" => $presentaddress,
    "permanent_address" => $permanentaddress,
    "occupation" => $occupation,
    "phone" => $phone,
    "bloodGroup" => $bloodGroup,
    "sl_no" => $sl_no,
    "voter_aria_code" => $voter_aria_code,
    "vuter_no" => $voter_no,
    "education" => $education
];