<?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') {

    $signature =
    mysqli_real_escape_string(
    $conn,
    $_POST['signature']
    );

    $policy =
    isset($_POST['policy']) ? 1 : 0;

    $nda =
    isset($_POST['nda']) ? 1 : 0;

    $background =
    isset($_POST['background']) ? 1 : 0;

    if(empty($signature)){

        $error =
        "Digital signature required.";

    } else {

        $sql = "INSERT INTO
        candidate_declaration
        (
            candidate_id,
            policy_acceptance,
            nda_acceptance,
            background_verification,
            digital_signature
        )
        VALUES
        (
            '$candidate_id',
            '$policy',
            '$nda',
            '$background',
            '$signature'
        )";

        if($conn->query($sql)
           === TRUE){

            $success = true;

        } else {

            $error =
            $conn->error;
        }
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Final Declaration</title>

<style>

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

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

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

input[type=text]{
    width:100%;
    padding:14px;
    border:1px solid #ddd;
    border-radius:8px;
    margin-top:12px;
    box-sizing:border-box;
}

</style>
</head>
<body>

<div class="box">

<?php if($success){ ?>

<h2>
🎉 Joining Completed Successfully
</h2>

<p>
Welcome to the organization.
Your joining formalities
have been completed.
</p>

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

<?php } else { ?>

<h2>
Final Declaration
</h2>

<?php if(!empty($error)){ ?>

<p style="color:red;">
<?php echo $error; ?>
</p>

<?php } ?>

<form method="POST">

<p>
<label>
<input type="checkbox"
name="policy"
required>

I accept all company
policies and code of conduct.
</label>
</p>

<p>
<label>
<input type="checkbox"
name="nda"
required>

I agree to confidentiality
and NDA terms.
</label>
</p>

<p>
<label>
<input type="checkbox"
name="background"
required>

I consent to background
verification.
</label>
</p>

<p>
Digital Signature
(Type Full Name)
</p>

<input type="text"
name="signature"
placeholder="Enter Full Name"
required>

<button type="submit">
Complete Joining
</button>

</form>

<?php } ?>

</div>

</body>
</html>