<?php

date_default_timezone_set('Asia/Kolkata');

$cutoffDate = new DateTime('2025-07-24 11:00:00', new DateTimeZone('Asia/Kolkata'));
$today      = new DateTime('now', new DateTimeZone('Asia/Kolkata'));

if ($today > $cutoffDate) {
    //include 'close.php';
    //exit;
}

if (!isset($_SESSION)) {
    session_start();
}

// DB connection
$con = new mysqli('localhost', 'gdhhsxmy_mawa', 'gdhhsxmy_mawa', 'gdhhsxmy_mawa');
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}

$fullname = $_POST['fullname'];
$mobile   = $_POST['contact'];

// ✅ Duplicate entry check by Fullname + Mobile
$check = $con->prepare("SELECT id FROM applications WHERE fullname = ? AND contact = ?");
$check->bind_param("ss", $fullname, $mobile);
$check->execute();
$check->store_result();

if ($check->num_rows > 0) {

    // Duplicate found
    echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
          <script>
            Swal.fire({
              icon: 'warning',
              title: 'Duplicate Entry ❌',
              text: 'You have already submitted an application!',
              confirmButtonColor: '#d33'
            }).then(() => { window.history.back(); });
          </script>";
    $check->close();
    $con->close();
    exit;
}
$check->close();

$agegroup = !empty($_POST['agegroup']) ? $_POST['agegroup'] : 'NA';

// ✅ Insert into applications
$stmt = $con->prepare("INSERT INTO applications 
    (fullname, parentname, address, age, gender, district, taluk, localbody, contact, othercontact, whatsapp, email, food_candidate, food_parent, category, agegroup) 
    VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

$stmt->bind_param(
    "sssisiisssssssss",
    $_POST['fullname'],
    $_POST['parentname'],
    $_POST['address'],
    $_POST['age'],
    $_POST['gender'],
    $_POST['district'],
    $_POST['taluk'],
    $_POST['localbody'],
    $_POST['contact'],
    $_POST['othercontact'],
    $_POST['whatsapp'],
    $_POST['email'],
    $_POST['food_candidate'],
    $_POST['food_parent'],
    $_POST['category'],
    $agegroup
);



if ($stmt->execute()) {
    $application_id = $stmt->insert_id;

    // Insert selected items
    if (!empty($_POST['items']) && is_array($_POST['items'])) {
        $stmt2 = $con->prepare("INSERT INTO application_items (application_id, item) VALUES (?, ?)");
        foreach ($_POST['items'] as $item) {
            $stmt2->bind_param("is", $application_id, $item);
            $stmt2->execute();
        }
        $stmt2->close();
    }

    // ✅ SweetAlert success with HTML context
    echo "
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset='UTF-8'>
        <script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
    </head>
    <body>
    <style>
    body {
      background: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4)),
                  url('fb1c39f459d6727cb164eb106f3fe298.jpg') no-repeat center center fixed;
      background-size: cover;
      font-family: 'Poppins', sans-serif;
      background-color: #000;
    }
    </style>
     
        <script>
            Swal.fire({
              icon: 'success',
              title: 'Submitted Successfully 🎉',
              text: 'Your application has been saved.',
              confirmButtonColor: '#3085d6'
            }).then(() => { window.location='index.php'; });
        </script>
    </body>
    </html>";
    exit;
} else {
    echo "Error: " . $stmt->error;
}

$stmt->close();
$con->close();
