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

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

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

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

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

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

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

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

    // Match account number

    if($account_number !=
       $confirm_account){

        $error =
        "Account numbers do not match.";

    } else {

        $bank_proof = "";

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

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

            $target =
            "uploads/bank/" . $file;

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

            $bank_proof = $file;
        }

        $sql = "INSERT INTO
        candidate_bank_details
        (
            candidate_id,
            account_holder_name,
            bank_name,
            account_number,
            ifsc_code,
            branch_name,
            upi_id,
            bank_proof
        )
        VALUES
        (
            '$candidate_id',
            '$holder_name',
            '$bank_name',
            '$account_number',
            '$ifsc',
            '$branch',
            '$upi',
            '$bank_proof'
        )";

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

            $success = true;

        } else {

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

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Bank Details</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%;
    padding:14px;
    margin-top:12px;
    border:1px solid #ddd;
    border-radius:8px;
    box-sizing:border-box;
}

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>
✅ Bank Details Saved
</h2>

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

<?php } else { ?>

<h2>
Bank Details
</h2>

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

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

<?php } ?>

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

<input type="text"
name="holder_name"
placeholder="Account Holder Name"
required>

<input type="text"
name="bank_name"
placeholder="Bank Name"
required>

<input type="text"
name="account_number"
placeholder="Account Number"
required>

<input type="text"
name="confirm_account"
placeholder="Confirm Account Number"
required>

<input type="text"
name="ifsc"
placeholder="IFSC Code"
required>

<input type="text"
name="branch"
placeholder="Branch Name"
required>

<input type="text"
name="upi"
placeholder="UPI ID (Optional)">

<p>
Upload Cancelled Cheque /
Passbook
</p>

<input type="file"
name="bank_proof">

<button type="submit">
Save Bank Details
</button>

</form>

<?php } ?>

</div>

</body>
</html>