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

if (!isset($_SESSION['candidate_id'])) {
    header("Location: login.php");
    exit();
}

$candidate_id = $_SESSION['candidate_id'];

$success = false;
$error = "";

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $upload_dir = "uploads/documents/";

    function uploadFile($fileName, $upload_dir){

        if(isset($_FILES[$fileName]) &&
           $_FILES[$fileName]['error'] == 0){

            $file =
            time() . "_" .
            basename($_FILES[$fileName]['name']);

            $target =
            $upload_dir . $file;

            move_uploaded_file(
                $_FILES[$fileName]['tmp_name'],
                $target
            );

            return $file;
        }

        return "";
    }

    $aadhaar =
    uploadFile('aadhaar_card',
    $upload_dir);

    $pan =
    uploadFile('pan_card',
    $upload_dir);

    $photo =
    uploadFile('passport_photo',
    $upload_dir);

    $resume =
    uploadFile('resume',
    $upload_dir);

    $marksheet10 =
    uploadFile('marksheet_10',
    $upload_dir);

    $marksheet12 =
    uploadFile('marksheet_12',
    $upload_dir);

    $graduation =
    uploadFile('graduation_certificate',
    $upload_dir);

    $experience =
    uploadFile('experience_letter',
    $upload_dir);

    $sql = "INSERT INTO candidate_documents
    (
        candidate_id,
        aadhaar_card,
        pan_card,
        passport_photo,
        resume,
        marksheet_10,
        marksheet_12,
        graduation_certificate,
        experience_letter
    )
    VALUES
    (
        '$candidate_id',
        '$aadhaar',
        '$pan',
        '$photo',
        '$resume',
        '$marksheet10',
        '$marksheet12',
        '$graduation',
        '$experience'
    )";

    if($conn->query($sql) === TRUE){
        $success = true;
    } else {
        $error = $conn->error;
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Upload Documents</title>

<style>

body{
    font-family:Arial;
    background:#f5f5f5;
    padding:40px;
}

.box{
    background:#fff;
    max-width:700px;
    margin:auto;
    padding:30px;
    border-radius:10px;
}

input{
    width:100%;
    margin-top:12px;
}

button{
    width:100%;
    background:#28a745;
    border:none;
    color:#fff;
    padding:15px;
    border-radius:8px;
    margin-top:20px;
    cursor:pointer;
}

</style>
</head>
<body>

<div class="box">

<?php if($success){ ?>

<h2>
✅ Documents Uploaded Successfully
</h2>

<a href="dashboard.php">
Back To Dashboard
</a>

<?php } else { ?>

<h2>Upload Documents</h2>

<?php if(!empty($error)){ ?>
<p style="color:red;">
<?php echo $error; ?>
</p>
<?php } ?>

<form method="POST"
enctype="multipart/form-data">

<p>Aadhaar Card</p>
<input type="file"
name="aadhaar_card">

<p>PAN Card</p>
<input type="file"
name="pan_card">

<p>Passport Photo</p>
<input type="file"
name="passport_photo">

<p>Resume</p>
<input type="file"
name="resume">

<p>10th Marksheet</p>
<input type="file"
name="marksheet_10">

<p>12th Marksheet</p>
<input type="file"
name="marksheet_12">

<p>Graduation Certificate</p>
<input type="file"
name="graduation_certificate">

<p>Experience Letter</p>
<input type="file"
name="experience_letter">

<button type="submit">
Upload Documents
</button>

</form>

<?php } ?>

</div>

</body>
</html>