<?php
// session_start();
include("inc/db.php");

// Fetch student's progress from the database if available
$sname = $_SESSION['sname']; // Assuming you have a session for the logged-in user
$query = "SELECT progress_percentage FROM student_table WHERE sname = '$sname'";
$result = mysqli_query($db, $query);

$progress_percentage = 0; // Default progress
if ($result && mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    $progress_percentage = $row['progress_percentage']; // Use the saved progress from the database
}

// If no progress is found in the database, calculate based on the form completion
if ($progress_percentage == 0) {
    // Initialize session for progress tracking if not set
    if (!isset($_SESSION['progress'])) {
        $_SESSION['progress'] = [
            'student_screening_form' => false,
            'bio_data' => false,
            'guarantor_form' => false,
            'waec_result' => false,
            'jamb' => false,
            'state_origin' => false,
            'medical_report' => false,
            'passport' => false,
        ];
    }

    // Function to calculate progress percentage
    function calculate_progress() {
        $total_forms = 8;
        $completed_forms = 0;
        foreach ($_SESSION['progress'] as $form => $status) {
            if ($status) $completed_forms++;
        }
        return ($completed_forms / $total_forms) * 100;
    }

    // Calculate the current progress based on form completion
    $progress_percentage = calculate_progress();

    // Save progress percentage to the database if updated or if 100%
    $query = "UPDATE student_table SET progress_percentage = '$progress_percentage' WHERE sname = '$sname'";
    mysqli_query($db, $query);

    // Set a message if progress is complete
    if ($progress_percentage == 100) {
        $progress_message = "Process completed";
    } else {
        $progress_message = ""; // No message for incomplete progress
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard with Progress Bar</title>
    <!-- Include Bootstrap CSS -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

    <h3>Dashboard</h3>
    <hr />

    <div class="container">
        <!-- <div class="progress">
            <div class="progress-bar progress-bar-striped active" role="progressbar" style="width: <?php echo $progress_percentage; ?>%;" aria-valuemin="0" aria-valuemax="100">
                <?php echo round($progress_percentage); ?>%
            </div>
        </div> -->
       
        <br />

        <!-- First Row - Screening Form & Bio Data Form -->
        <div class="row">
        <div class="col-md-6 col1-class">
                <div class="row" style="border:1px solid #dddddd; padding:8px;">
                    <div class="col-md-4">
                      <a href="index.php?env=Jamb" style="text-decoration:none;color:#000000;">
                            <img src="images/waec.png" width="100%" alt="usericon" style="margin-top:10px;" />
                        </a>
                    </div>
                    <div class="col-md-8">
                      <a href="index.php?env=Jamb" style="text-decoration:none;color:#000000;">
                            <h5>Upload Your Jamb Admission Letter and Jamb Result
                            </h5>
                            <!-- <p>Here you can print your Screening Form Details</p> -->
                        </a>
                    </div>
                </div>
            </div>

            <div class="col-md-6 col2-class">
                <div class="row" style="border:1px solid #dddddd; padding:8px;">
                    <div class="col-md-4">
                         <a href="index.php?env=print-jamb_form" style="text-decoration:none;color:#000000;">
                            <img src="images/printer.svg" width="50px" alt="usericon" style="margin-top:10px;" />
                        </a>
                    </div>
                    <div class="col-md-8">
                         <a href="index.php?env=print-jamb_form" style="text-decoration:none;color:#000000;">
                            <h5>View Your Jamb Admission Letter and Jamb Result
                            </h5>
                            <!-- <p>Here you can print your Guarantor Form Details</p> -->
                        </a>
                    </div>
                </div>
            </div>
        </div>
        
        <br />

        <!-- Second Row - Guarantor Form
        <div class="row">
            <div class="col-md-6 col1-class">
                <div class="row" style="border:1px solid #dddddd; padding:8px;">
                    <div class="col-md-4">
                        <a href="index.php?env=Guarantor_Form" style="text-decoration:none;color:#000000;">
                            <img src="images/checklist2.svg" width="50px" alt="usericon" style="margin-top:10px;" />
                        </a>
                    </div>
                    <div class="col-md-8">
                        <a href="index.php?env=Guarantor_Form" style="text-decoration:none;color:#000000;">
                            <h5>Guarantor Form</h5>
                            <p>Here you can print your Guarantor Form Details</p>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <br /> -->

        <!-- Third Row - Upload Documents (WAEC/JAMB)
        <h3>Upload Your WAEC/O'level Result, JAMB Admission Letter, and JAMB Result</h3>
        <hr/>
        <div class="row">
            <div class="col-md-6 col1-class">
                <div class="row" style="border:1px solid #dddddd; padding:8px;">
                    <div class="col-md-4">
                        <a href="index.php?env=waec_result" style="text-decoration:none;color:#000000;">
                            <img src="images/jamb.png" width="120px" alt="usericon" style="margin-top:10px;" />
                        </a>
                    </div>
                    <div class="col-md-8">
                        <a href="index.php?env=waec_result" style="text-decoration:none;color:#000000;">
                            <h5>Upload Your WAEC/O'level Result</h5>
                            <p>Payments and registration for hostel here</p>
                        </a>
                    </div>
                </div>
            </div>

            <div class="col-md-6 col2-class">
                <div class="row" style="border:1px solid #dddddd; padding:8px;">
                    <div class="col-md-4">
                        <a href="index.php?env=Jamb" style="text-decoration:none;color:#000000;">
                            <img src="images/waec.png " width="150%" alt="usericon" style="margin-top:10px;" />
                        </a>
                    </div>
                    <div class="col-md-8">
                        <a href="index.php?env=Jamb" style="text-decoration:none;color:#000000;">
                            <h5>Upload Your JAMB Admission Letter and JAMB Result</h5>
                            <p>Confirm all payment here</p>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <br />

        Fourth Row - Upload State of Origin and Medical Report
        <h3>Upload Your State of Origin and Medical Report</h3>
        <hr/>
        <div class="row">
            <div class="col-md-6 col1-class">
                <div class="row" style="border:1px solid #dddddd; padding:8px;">
                    <div class="col-md-4">
                        <a href="index.php?env=State_origin" style="text-decoration:none;color:#000000;">
                            <img src="images/checklist2.svg" width="50px" alt="usericon" style="margin-top:10px;" />
                        </a>
                    </div>
                    <div class="col-md-8">
                        <a href="index.php?env=State_origin" style="text-decoration:none;color:#000000;">
                            <h5>Upload Your State of Origin</h5>
                        </a>
                    </div>
                </div>
            </div>

            <div class="col-md-6 col2-class">
                <div class="row" style="border:1px solid #dddddd; padding:8px;">
                    <div class="col-md-4">
                        <a href="index.php?env=Medical_report" style="text-decoration:none;color:#000000;">
                            <img src="images/first-aid-kit.png" width="50px" alt="usericon" style="margin-top:10px;" />
                        </a>
                    </div>
                    <div class="col-md-8">
                        <a href="index.php?env=Medical_report" style="text-decoration:none;color:#000000;">
                            <h5>Upload Your Medical Report</h5>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <br />

        Fifth Row - Upload Passport and Signature
        <h3>Upload Your Passport and Signature</h3>
        <hr/>
        <div class="row">
            <div class="col-md-6 col1-class">
                <div class="row" style="border:1px solid #dddddd; padding:8px;">
                    <div class="col-md-4">
                        <a href="index.php?env=passport" style="text-decoration:none;color:#000000;">
                            <img src="images/result.svg" width="50px" alt="usericon" style="margin-top:10px;" />
                        </a>
                    </div>
                    <div class="col-md-8">
                        <a href="index.php?env=passport" style="text-decoration:none;color:#000000;">
                            <h5>Upload Your Passport and Signature</h5>
                        </a>
                    </div>
                </div>
            </div> -->
            <div class="row" style="margin-top:50px;">
                        <div class="col-md-6">
                            <a href="index.php?env=dashboard" style="color:#ffffff;text-decoration:none;"> 
                                <button type="button" class="btn btn-primary btn-block"><i class="fa fa-arrow-left"></i> Go back</button>
                            </a>
                        </div>
                        <div class="col-md-6">
                        
                        </div>
                    </div>

        </div>
    </div>

   
</body>
</html>
