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

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

$candidate_id = $_SESSION['candidate_id'];

$personal_completed = false;
$documents_completed = false;
$bank_completed = false;
$declaration_completed = false;

$total_steps = 4;
$completed_steps = 0;

/* PERSONAL INFO CHECK */

$checkPersonal = $conn->query("
SELECT id
FROM candidate_onboarding
WHERE candidate_id='$candidate_id'
");

if($checkPersonal->num_rows > 0){
    $personal_completed = true;
    $completed_steps++;
}

/* DOCUMENT CHECK */

$checkDocuments = $conn->query("
SELECT id
FROM candidate_documents
WHERE candidate_id='$candidate_id'
");

if($checkDocuments->num_rows > 0){
    $documents_completed = true;
    $completed_steps++;
}

/* BANK DETAILS CHECK */

$checkBank = $conn->query("
SELECT id
FROM candidate_bank_details
WHERE candidate_id='$candidate_id'
");

if($checkBank->num_rows > 0){
    $bank_completed = true;
    $completed_steps++;
}

/* FINAL DECLARATION CHECK */

$checkDeclaration = $conn->query("
SELECT id
FROM candidate_declaration
WHERE candidate_id='$candidate_id'
");

if($checkDeclaration->num_rows > 0){
    $declaration_completed = true;
    $completed_steps++;
}

/* PROFILE COMPLETION */

$completion_percentage =
($completed_steps / $total_steps) * 100;
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Joining Dashboard</title>

<style>

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

.box{
    background:white;
    max-width:800px;
    margin:auto;
    padding:35px;
    border-radius:12px;
    box-shadow:0 0 15px rgba(0,0,0,0.08);
}

.card{
    background:#fafafa;
    border:1px solid #eee;
    padding:20px;
    border-radius:10px;
    margin-top:15px;
}

h2{
    margin-top:0;
}

.btn{
    display:inline-block;
    background:#007bff;
    color:white;
    text-decoration:none;
    padding:12px 20px;
    border-radius:8px;
    margin-top:10px;
}

.complete{
    color:green;
    font-weight:bold;
}

.pending{
    color:#ff9800;
    font-weight:bold;
}

.logout{
    background:#dc3545;
}

.progress-bar{
    background:#eee;
    border-radius:20px;
    overflow:hidden;
    margin-top:15px;
    margin-bottom:25px;
}

.progress-fill{
    width:<?php echo $completion_percentage; ?>%;
    background:#28a745;
    padding:12px;
    color:white;
    font-weight:bold;
    text-align:center;
}

</style>
</head>
<body>

<div class="box">

<h2>
Welcome,
<?php echo $_SESSION['candidate_name']; ?> 🎉
</h2>

<p>
Complete your joining formalities below.
</p>

<div class="progress-bar">

<div class="progress-fill">

<?php echo round($completion_percentage); ?>%
Completed

</div>

</div>

<!-- PERSONAL INFORMATION -->

<div class="card">

<h3>
1. Personal Information
</h3>

<?php if($personal_completed){ ?>

<p class="complete">
✅ Completed
</p>

<?php } else { ?>

<p class="pending">
⚠ Pending
</p>

<a class="btn"
href="personal-information.php">
Complete Now
</a>

<?php } ?>

</div>

<!-- DOCUMENTS -->

<div class="card">

<h3>
2. Upload Documents
</h3>

<?php if($documents_completed){ ?>

<p class="complete">
✅ Completed
</p>

<?php } else { ?>

<p class="pending">
⚠ Pending
</p>

<a class="btn"
href="upload-documents.php">
Upload Documents
</a>

<?php } ?>

</div>

<!-- BANK DETAILS -->

<div class="card">

<h3>
3. Bank Details
</h3>

<?php if($bank_completed){ ?>

<p class="complete">
✅ Completed
</p>

<?php } else { ?>

<p class="pending">
⚠ Pending
</p>

<a class="btn"
href="bank-details.php">
Complete Bank Details
</a>

<?php } ?>

</div>

<!-- FINAL DECLARATION -->

<div class="card">

<h3>
4. Final Declaration
</h3>

<?php if($declaration_completed){ ?>

<p class="complete">
✅ Completed
</p>

<?php } else { ?>

<p class="pending">
⚠ Pending
</p>

<a class="btn"
href="final-declaration.php">
Complete Declaration
</a>

<?php } ?>

</div>

<br>

<a class="btn logout"
href="logout.php">
Logout
</a>

</div>

</body>
</html>