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

$error = "";

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

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

    $sql = "SELECT * FROM offer_acceptance
            WHERE email='$email'
            AND mobile='$phone'
            AND status='Accepted'";

    $result = $conn->query($sql);

    if ($result->num_rows > 0) {

        $candidate = $result->fetch_assoc();

        $_SESSION['candidate_id'] = $candidate['id'];
        $_SESSION['candidate_name'] = $candidate['candidate_name'];
        $_SESSION['candidate_email'] = $candidate['email'];
        $_SESSION['candidate_phone'] = $candidate['mobile'];

        header("Location: dashboard.php");
        exit();

    } else {
        $error = "Invalid email or phone number.";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Candidate Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1">

<style>

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

.box{
    background:white;
    padding:35px;
    width:90%;
    max-width:450px;
    border-radius:12px;
    box-shadow:0 0 20px rgba(0,0,0,0.08);
}

h2{
    text-align:center;
}

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

button{
    width:100%;
    background:#007bff;
    border:none;
    color:white;
    padding:15px;
    border-radius:8px;
    margin-top:20px;
    cursor:pointer;
    font-size:16px;
}

.error{
    color:red;
    text-align:center;
}

</style>
</head>
<body>

<div class="box">

<h2>Candidate Login</h2>

<p style="text-align:center;">
Login using the same email & phone number
used during offer acceptance.
</p>

<?php if(!empty($error)){ ?>
    <p class="error">
        <?php echo $error; ?>
    </p>
<?php } ?>

<form method="POST">

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

    <input
        type="text"
        name="phone"
        placeholder="Phone Number"
        required
    >

    <button type="submit">
        Login
    </button>

</form>

</div>

</body>
</html>