<?php
// include('session_validator.php');
// Include your database connection code here
include('includes/db.php'); // Adjust the path based on your file structure

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Collect data from the form
    $studentName = $_POST["student_name"];
    $fatherName = $_POST["father_name"];
    $motherName = $_POST["mother_name"];
    $studentAddress = $_POST["student_address"];
    $yearAdmitted = $_POST["year_admitted"];
    $yearLeft = $_POST["year_left"];
    $levelCompletion = $_POST["level_completion"];
    $Gpa = $_POST["gpa"];
    $completedAcademicYear = $_POST["completed_academic_year"];
    $studentDOB = $_POST["student_dob"];
    $studentAdmittedDate = $_POST["student_admitted_date"];
    $Sym_number = $_POST["sym_number"];
    $Regd_number = $_POST["regd_number"];
    $certificateType = $_POST["certificate_type"];
    $serial_no = $_POST["serial_no"];

    $schoolDetailsQuery = "SELECT school_name, address, establishment_year, logo FROM schools WHERE id = 1"; // Assuming your school details are in a table with id=1
    $schoolDetailsResult = $conn->query($schoolDetailsQuery);

    if ($schoolDetailsResult->num_rows > 0) {
        $schoolDetails = $schoolDetailsResult->fetch_assoc();

        // Get the path to the frame image
        $frameImagePath = "certificate/includes/cer_frame.png"; // Adjust the path based on your file structure

        // Get the path to the custom fonts
        $variableFontPath = "certificate/fonts/StencilBT-Regular.otf"; // Font for variable names
        $textFontPath = "certificate/fonts/"; // Font for other text

        // Create certificate content with school details and student information
        $certificateContent = "
            <style>
                @page {
                    size: A4 landscape;
                    margin: 10mm;
                }

                body {
                    width: 100%;
                    height: 100%;
                    box-sizing: border-box;
                    background-image: url('$frameImagePath');
                    background-size: cover;
                    padding: 10mm;
                    background-origin: border-box;
                    background-clip: content-box, border-box;
                }

                .watermark {
                    font-size: 12px;
                    font-weight: '';
                    color: rgba(0, 255, 255, 0.9); /* Adjust the alpha value for watermark transparency */
                    background-repeat: repeat; /* Repeat the watermark throughout the page */
                }

                .content-container {
                    background-color: rgba(0, 255, 255, 0.0 ); /* Set a transparent white background */
                    padding: 15px;
                }

                .bold-red { font-weight: bold; color: red; }
                .bold-uppercase { font-weight: bold; text-transform: uppercase; }
                .certificate-text { font-family: '$variableFontPath', StencilBT-Regular.otf; font-style: italic; font-size: 16px; }
                .certificate-body-text { font-family: '$textFontPath', times_new_roman.ttf; font-style: italic; font-size: 16px; }
                .school-info-container { display: flex; align-items: center; }
                .school-logo { max-width: 100px; margin-right: 20px; }
            </style>

            <div class='content-container'>
                <h5 class='bold-red watermark'>S.N. : $serial_no</h5>
                <div class='watermark'>MOHANMAYA SECONDARY SCHOOL</div>
                
                <div class='school-info-container'>
                    <img class='school-logo' src='{$schoolDetails['logo']}' alt='School Logo'>
                    <div>
                        <p class='bold-uppercase' style='font-size: 58px; text-align:center; font-family: $variableFontPath;'>{$schoolDetails['school_name']}</p>
                        <p class='bold-uppercase' style='font-size: 30px; text-align:center; font-family: $variableFontPath;'> {$schoolDetails['address']}</p>
                        <p class='bold-uppercase' style='font-size: 20px; text-align:center; font-family: $variableFontPath;'>Estd: {$schoolDetails['establishment_year']} B.S.</p>
                    </div>
                </div>

                <div class='certificate-text' style='font-size: 30px; text-align:center'>$certificateType</div>
<div class'certificate-body-text'>
                <p >

                    This is to certify that Mr. $studentName, son of Mr. $fatherName and Mrs. $motherName, a permanent resident of $studentAddress, was a bona fide student of this school from $yearAdmitted to $yearLeft. He completed his $levelCompletion with GPA $Gpa from this school in the academic year $completedAcademicYear.</p>

                <p class='certificate-body-text'>As per school record, his date of birth is $studentDOB B.S. and the date of admission is $studentAdmittedDate B.S.</p>

                <p class='certificate-body-text'>His NEB symbol number is $Sym_number, and the registration number is $Regd_number.</p>

                <p class='certificate-body-text'>He bears good moral character. I wish him success in the future.</p>
                </div>
            </div>
        ";
    } else {
        $certificateContent = "<p>Error: School details not found</p>";
    }

    // Close the database connection
    $conn->close();
    ?>

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Generated Certificate</title>
    </head>
    <body>
        <?php echo $certificateContent; ?>
        <!-- Add an edit button or link to redirect back to the form for editing -->
        <p><a href="Certificate.html">Edit Certificate Information</a></p>
    </body>
    </html>

<?php
}
?>
