<?php
session_start();
include 'db.php';

/* ===============================
   FILE UPLOAD FUNCTION
================================ */
function uploadFile($file, $folder) {
    if (!empty($file['name'])) {
        if (!is_dir("uploads/$folder")) {
            mkdir("uploads/$folder", 0777, true);
        }
        $filename = time() . '_' . preg_replace("/[^a-zA-Z0-9.]/", "_", $file['name']);
        $target = "uploads/$folder/" . $filename;
        move_uploaded_file($file['tmp_name'], $target);
        return $filename;
    }
    return null;
}

/* ===============================
   AUTO GENERATE APPLICATION NO
   FORMAT: SS/2026-27/1
================================ */
$year_code = "2026-27";

$result = $conn->query("SELECT id FROM application ORDER BY id DESC LIMIT 1");
$next_id = ($result && $result->num_rows > 0)
    ? $result->fetch_assoc()['id'] + 1
    : 1;

$app_no = "SS/$year_code/$next_id";
$applicant_id = $_SESSION['user_id'];
/* ===============================
   FILE UPLOADS
================================ */
$photo              = uploadFile($_FILES['file'], 'photo');
$twelfth_marksheet  = uploadFile($_FILES['twoth_d'], '12th');
$grad_marksheet     = uploadFile($_FILES['highest_d'], 'graduation');
$pg_marksheet       = uploadFile($_FILES['pg_marksheet'], 'pg');
$recommendation     = uploadFile($_FILES['reco'], 'recommendation');

/* ===============================
   INSERT QUERY
================================ */
$sql = "INSERT INTO application (
    app_no, applicant_id, mumbai_univ, training_duration,
    name, guardian_name, photo,
    address, state, mobile, email, dob, gender, category, nationality,
    twelfth_board, twelfth_year, twelfth_subjects, twelfth_division, twelfth_percentage, twelfth_marksheet,
    grad_degree, grad_subject, grad_university, grad_year, grad_division, grad_percentage, grad_marksheet,
    pg_course, pg_year, pg_subject, pg_university, pg_division, pg_percentage, pg_marksheet,
    college_name, college_address, hod_name, hod_contact,
    training_from, training_to, covid_status,
    hod_approval, recommendation_letter
) VALUES (
    ?,?,?,?,?,?,?,?,?,?,?,?,?,?,
    ?,?,?,?,?,?,
    ?,?,?,?,?,?,
    ?,?,?,?,?,?,?,
    ?,?,?,?,
    ?,?,?,
    ?,?,?,?
)";

$stmt = $conn->prepare($sql);

/* ===============================
   BIND PARAMETERS (45 TOTAL)
================================ */
$stmt->bind_param(
	"ssssssssssssssssssssssssssssssssssssssssssss",
    $app_no,
	$applicant_id,
    $_POST['mumbai_univ'],
    $_POST['training_duration'],
    $_POST['nam'],
    $_POST['guardian_name'],
    $photo,
    $_POST['address_co'],
    $_POST['states'],
    $_POST['mob_no'],
    $_POST['email_id'],
    $_POST['dob'],
    $_POST['gender'],
    $_POST['category'],
    $_POST['nationality'],
    $_POST['twelfth_board'],
    $_POST['twelfth_year'],
    $_POST['twelfth_subjects'],
    $_POST['twelfth_division'],
    $_POST['twelfth_percentage'],
    $twelfth_marksheet,
    $_POST['grad_degree'],
    $_POST['grad_subject'],
    $_POST['grad_university'],
    $_POST['grad_year'],
    $_POST['grad_division'],
    $_POST['grad_percentage'],
    $grad_marksheet,
    $_POST['pg_course'],
    $_POST['pg_year'],
    $_POST['pg_subject'],
    $_POST['pg_university'],
    $_POST['pg_division'],
    $_POST['pg_percentage'],
    $pg_marksheet,
    $_POST['college_name'],
    $_POST['college_address'],
    $_POST['hod_name'],
    $_POST['hod_contact'],
    $_POST['training_from'],
    $_POST['training_to'],
    $_POST['covid_status'],
    $_POST['hod_approval'],
    $recommendation
);

/* ===============================
   EXECUTE
================================ */
if ($stmt->execute()) {
	$_SESSION['app_no'] = $app_no;
    $_SESSION['success'] = "Application submitted successfully.<br>
    <strong>Your Application No:</strong> $app_no";

    header("Location: dashboard.php");
    exit;
} else {
    echo "Database Error: " . $stmt->error;
}

$stmt->close();
$conn->close();
?>
