<?php
	ob_start();
    session_start();
?>
<?php
chdir('../');
?>
<?php
	require_once("public/connection.php");
	require_once("public/functions.php");
?>
<?php
	if (!empty($_GET['id'])) {
		$id = $_GET['id']; // Extract the id from the URL
		
		$query = " SELECT * FROM transfer_certificate WHERE id = '{$id}' ";
		$run_query = mysqli_query($connection, $query);
		if(mysqli_num_rows($run_query) == 1){
			while($result = mysqli_fetch_assoc($run_query)){
				$ref_no = $result['id'];
				$fullname = ucfirst($result['student_name']);
				$parentname = ucfirst($result['parent_name']);
				$student_class = $result['current_class'];
				$reg_number = $result['reg_number']; 
				$student_dob = $result['dob'];
				$student_state = ucwords($result['state']);
				$student_lga = ucwords($result['lga']);
				$last_class = $result['last_class'];
				$last_class_date = $result['last_class_date'];
				$last_att_date = $result['last_att_date'];
				$section = $result['section'];
				$indebtedness = $result['indebtedness'];
				$conduct = $result['conduct'];
				$cause_of_leaving = $result['cause_of_leaving'];
				$student_passport = $result['passport'];
				$date_issued = $result['date_issued'];
			}
		}
	}
?>
<?php
////////// POST ACTION TO VIEW ALL THE UPLOADED ADMINISTRATIVE INFORMATION ////////////
$query = " SELECT * FROM `administratives` ";
$run_query = mysqli_query($connection, $query);

if (mysqli_num_rows($run_query) > 0) {
    while ($result = mysqli_fetch_assoc($run_query)) {
        $administratives_id = $result['id'];
        $school_name = $result['school_name'];
        $school_motto = $result['school_motto'];
        $school_logo = $result['school_logo'];
        $school_address = $result['school_address'];
        $school_phone = $result['school_phone'];
        $school_mail = $result['school_mail'];
        $school_website = $result['school_website'];
        $school_whatsapp = $result['school_whatsapp'];
        $school_short_name = $result['school_short_name'];
        $school_favicon = $result['school_favicon'];
        $school_initial = $result['adm_no_initials'];
        $school_stamp = $result['school_stamp'];
        //echo "<img src='$admin_logo' />";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $fullname; ?> Transfer Certificate</title>
    <link rel="shortcut icon" href="../<?php echo $school_favicon; ?>">
    <style>
        body {
            font-family: "Algerian", Times, serif;
            margin: 0;
            padding: 0;
            background-color: #fff;
            color: #000;
        }

        .certificate {
            width: 210mm;
            height: 297mm;
            margin: 0 auto;
            padding: 20px;
            border: 2px solid #000;
            box-sizing: border-box;
            position: relative;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }

        .header {
            text-align: center;
            position: relative;
        }

        .header h1 {
            font-size: 24px;
            margin: 0;
            text-transform: uppercase;
        }

        .header p {
            margin: 3px 0;
            font-size: 12px;
        }

        .header .motto {
            font-weight: bold;
            margin-top: 5px;
        }

        .logo-left, .logo-right {
            position: absolute;
            top: 10px;
            width: 80px;
            height: 80px;
        }

        .logo-left {
            left: 10px;
        }

        .logo-right {
            right: 10px;
        }

        .three-column {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin: 10px 0;
        }

        .three-column .column {
            flex: 1;
            font-size: 12px;
            text-align: right;
        }
		.three-column .column1 {
            flex: 1;
            font-size: 12px;
            text-align: left;
        }
		.three-column .column2 {
            flex: 1;
            font-size: 12px;
            text-align: center;
        }

        .three-column .line {
            width: 100%;
            height: 1px;
            background-color: #000;
            margin-top: 1px;
        }

        .body {
            flex-grow: 1;
            padding: 10px;
            border: 1px solid #000;
            border-radius: 8px;
        }

        .row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 33px;
        }

        .row label {
            width: 40%;
            font-size: 14px;
            text-align: left;
        }

        .row .value {
            width: 60%;
            font-size: 14px;
            text-align: center;
            border-bottom: 1px solid #000;
        }

        .multi-line {
            height: 30px;
            line-height: 15px;
            text-align: center;
        }

        .danger-text {
            color: red;
            text-align: center;
            font-weight: bold;
            font-size: 14px;
            text-transform: uppercase;
        }

        .footer {
            text-align: right;
            font-size: 12px;
        }

        .footer p {
            margin: 3px 0;
        }
		    @media print {
        #printButton, #cancelButton {
            display: none; /* Hide both buttons during printing */
        }
    }
    </style>
</head>
<body>
    <div class="certificate">
        <div class="header">
            <img src="../<?php echo $school_logo; ?>" alt="Logo" class="logo-left">
            <img src="../<?php echo $student_passport; ?>" alt="Logo" class="logo-right">
            <h1><?php echo $school_name; ?></h1>
            <p><?php echo $school_address; ?></p>
            <p><?php echo $school_phone; ?></p>
            <p><?php echo $school_motto; ?></p>
        </div>
        <br />
	
        <div class="three-column">
            <div class="column1">
                Our Ref: <?php echo $school_initial; ?>/TC/0<?php echo $ref_no; ?>/<span id="year"></span>
                <div class="line"></div>
				<div class="line"></div>
            </div>
            <div class="column2">
                Your Ref: 
                <div class="line"></div>
				<div class="line"></div>
            </div>
            <div class="column">
                Date: <?php echo $date_issued; ?>
                <div class="line"></div>
				<div class="line"></div>
            </div>
        </div>		
        <div class="body">
			<div class="header">
				<h1>TRANSFER CERTIFICATE</h1>
				<p>(TO BE KEPT BY THE RECEIVING SCHOOL)</p>
			</div>
			</br>
            <div class="row">			
                <label>Section:</label>
                <div class="value"><?php echo $section; ?></div>
            </div>
            <div class="row">
                <label>Adm. No:</label>
                <div class="value"><?php echo $reg_number; ?></div>
            </div>
            <div class="row">
                <label>Name of Pupil in Full:</label>
                <div class="value"><?php echo $fullname; ?></div>
            </div>
            <div class="row">
                <label>Name of Parent:</label>
                <div class="value"><?php echo $parentname; ?></div>
            </div>
            <div class="row">
                <label>State of Origin:</label>
                <div class="value"><?php echo $student_state; ?></div>
            </div>
            <div class="row">
                <label>L.G.A:</label>
                <div class="value"><?php echo $student_lga; ?></div>
            </div>
            <div class="row">
                <label>Date of Birth:</label>
                <div class="value"><?php echo $student_dob; ?></div>
            </div>
            <div class="row">
                <label>Class Last Passed with Date:</label>
                <div class="value"><?php echo $last_class; ?> on <?php echo $last_class_date; ?></div>
            </div>
            <div class="row">
                <label>Class at Present:</label>
                <div class="value"><?php echo $student_class; ?></div>
            </div>
            <div class="row">
                <label>Date of Last Attendance</br> at the Above School:</label>
                <div class="value"><?php echo $last_att_date; ?></div>
            </div>
            <div class="row">
                <label>Indebtedness to the School:</label>
                <div class="value"><?php echo $indebtedness; ?></div>
            </div>
            <div class="row">
                <label>Conduct (If Unsatisfactory,</br> Give Reasons):</label>
                <div class="value multi-line"><?php echo $conduct; ?></div>
            </div>
            <div class="row">
                <label>Cause of Leaving:</label>
                <div class="value multi-line"><?php echo $cause_of_leaving; ?></div>
            </div>
            <p class="danger-text">Any alteration render this certificate valueless</p>
			</br>
			</br>
			</br>
			
			<div class="footer">
				<p>School Admin Name</p>
				<p><strong>Proprietor</strong></p>
			</div>
        </div>
		<div style="text-align: center; margin-top: 20px;">
    <button id="printButton" style="
        background-color: #4CAF50; /* Green background */
        color: white; /* White text */
        padding: 15px 30px; /* Padding for the button */
        font-size: 16px; /* Larger font size */
        border: none; /* Remove border */
        border-radius: 5px; /* Rounded corners */
        cursor: pointer; /* Pointer cursor on hover */
        box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.2); /* Subtle shadow effect */
        transition: all 0.3s ease-in-out; /* Smooth hover effect */
        margin-right: 10px; /* Space between buttons */
    " onclick="printInvoice()">🖨️ Print</button>
    
    <button id="cancelButton" style="
        background-color: #f44336; /* Red background */
        color: white; /* White text */
        padding: 15px 30px; /* Padding for the button */
        font-size: 16px; /* Larger font size */
        border: none; /* Remove border */
        border-radius: 5px; /* Rounded corners */
        cursor: pointer; /* Pointer cursor on hover */
        box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.2); /* Subtle shadow effect */
        transition: all 0.3s ease-in-out; /* Smooth hover effect */
    " onclick="cancelInvoice()">❌ Cancel</button>
</div>
        
    </div>
</body>
</html>

<script>
    function printInvoice() {
        window.print(); // Trigger print functionality
    }

    function cancelInvoice() {
        // Redirect the user to the previous page or a specific URL
        if (document.referrer) {
            window.location.href = document.referrer; // Go back to the previous page
        } else {
            window.location.href = '/'; // Default to homepage if no referrer
        }
    }

    // Optional: Add hover effects for buttons
    const printButton = document.getElementById('printButton');
    printButton.addEventListener('mouseover', () => {
        printButton.style.backgroundColor = '#45a049'; // Darker green
        printButton.style.transform = 'scale(1.05)'; // Slightly larger on hover
    });
    printButton.addEventListener('mouseout', () => {
        printButton.style.backgroundColor = '#4CAF50'; // Reset to original color
        printButton.style.transform = 'scale(1)'; // Reset size
    });

    const cancelButton = document.getElementById('cancelButton');
    cancelButton.addEventListener('mouseover', () => {
        cancelButton.style.backgroundColor = '#e53935'; // Darker red
        cancelButton.style.transform = 'scale(1.05)'; // Slightly larger on hover
    });
    cancelButton.addEventListener('mouseout', () => {
        cancelButton.style.backgroundColor = '#f44336'; // Reset to original color
        cancelButton.style.transform = 'scale(1)'; // Reset size
    });

  // Auto-update the year
  document.getElementById("year").textContent = new Date().getFullYear();
</script>
