<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

include 'db.php';

$success = false;
$alreadyAccepted = false;
$error = "";

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

    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $contact = mysqli_real_escape_string($conn, $_POST['contact']);

    // Check if already accepted
    $check = $conn->query("
        SELECT id
        FROM offer_acceptance
        WHERE email='$email'
        OR mobile='$contact'
    ");

    if ($check->num_rows > 0) {

        $alreadyAccepted = true;

    } else {

        $ip = $_SERVER['REMOTE_ADDR'];
        $device = $_SERVER['HTTP_USER_AGENT'];

        $sql = "INSERT INTO offer_acceptance
        (
            candidate_name,
            email,
            mobile,
            status,
            accepted_at,
            ip_address,
            device_info
        )
        VALUES
        (
            '$name',
            '$email',
            '$contact',
            'Accepted',
            NOW(),
            '$ip',
            '$device'
        )";

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

<!DOCTYPE html>
<html>
<head>

<meta charset="UTF-8">

<title>Offer Letter Confirmation</title>

<meta name="viewport"
      content="width=device-width, initial-scale=1">

<style>

body{
    font-family:Arial;
    background:#f5f5f5;
    display:flex;
    justify-content:center;
    align-items:center;
    min-height:100vh;
    margin:0;
}

.box{
    background:#fff;
    width:90%;
    max-width:500px;
    padding:30px;
    border-radius:12px;
    box-shadow:0 0 15px rgba(0,0,0,0.1);
    text-align:center;
}

input{
    width:100%;
    padding:14px;
    margin:10px 0;
    border:1px solid #ddd;
    border-radius:8px;
    box-sizing:border-box;
}

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

.success{
    color:green;
}

.warning{
    color:#ff9800;
}

.login-btn{
    display:inline-block;
    background:#007bff;
    color:white;
    text-decoration:none;
    padding:15px 25px;
    border-radius:8px;
    margin-top:20px;
    font-weight:bold;
}

</style>
</head>

<body>

<div class="box">

<?php if($success){ ?>

    <h2 class="success">
        ✅ Offer Letter Accepted
    </h2>

    <h1>
        Welcome Aboard 🎉
    </h1>

    <p>
        We wish you all the best for your journey in our organisation.
    </p>

    <a href="https://offer.careers-pharmaforceiq.com/login.php"
       class="login-btn">

       Continue Joining Formalities

    </a>

<?php } elseif($alreadyAccepted){ ?>

    <h2 class="warning">
        ⚠ Offer Already Accepted
    </h2>

    <p>
        Our records show that you have
        already accepted this offer letter.
    </p>

    <a href="https://offer.careeratagoda.com/login.php"
       class="login-btn">

       Login To Continue

    </a>

<?php } else { ?>

    <h2>
        Offer Letter Confirmation
    </h2>

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

    <form method="POST">

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

        <input
            type="email"
            name="email"
            placeholder="Email Address"
            required
        >

        <input
            type="text"
            name="contact"
            placeholder="Contact Number"
            required
        >

        <label>
            <input
                type="checkbox"
                required
                style="width:auto;"
            >
            I accept the offer letter
        </label>

        <br><br>

        <button type="submit">
            Accept Offer Letter
        </button>

    </form>

<?php } ?>

</div>

</body>
</html>