<?php

require_once __DIR__ . '/../library/auth.php';

$school_id = $_SESSION['school_id'];

allow_roles(['admin', 'accountant']);

$id = intval($_GET['id'] ?? 0);


// ======================================================
// FETCH TC DATA
// ======================================================

$query = "

    SELECT

        tc.*,

        s.name AS student_name,
        s.dob,
        s.gender,

        ss.roll_no,
        ss.admission_no,

        sp.father_name,
        sp.mother_name,

        sa.address,

        c.class_name,

        sch.school_name,
        sch.school_address,
        sch.school_phone,
        sch.school_email,
        sch.school_logo,
        sch.school_website,
        sch.school_reg_no,
        sch.dise_code,
        sch.current_session

    FROM tc_certificates tc

    INNER JOIN students s
        ON tc.student_id = s.id

    LEFT JOIN student_sessions ss
        ON ss.student_id = s.id
        AND ss.session_id = tc.session_id

    LEFT JOIN student_parents sp
        ON sp.student_id = s.id
        AND sp.session_id = tc.session_id

    LEFT JOIN student_address sa
        ON sa.student_id = s.id
        AND sa.session_id = tc.session_id

    LEFT JOIN classes c
        ON ss.class_id = c.id

    INNER JOIN school_settings sch
        ON tc.school_id = sch.school_id

    WHERE tc.id = ?
    AND tc.school_id = ?

";

$stmt = $pdo->prepare($query);

$stmt->execute([
    $id,
    $school_id
]);

$tc = $stmt->fetch(PDO::FETCH_ASSOC);


// ======================================================
// INVALID TC
// ======================================================

if (!$tc) {

    exit('Invalid TC Record');
}

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>

        Transfer Certificate - <?= htmlspecialchars($tc['tc_no']) ?>

    </title>

    <style>

        * {

            box-sizing: border-box;
        }

        body {

            margin: 0;
            padding: 20px;
            background: #f1f5f9;
            font-family: "Segoe UI", sans-serif;
            color: #222;
        }

        .print-btn {

            text-align: right;
            margin-bottom: 20px;
        }

        .print-btn button {

            background: #2563eb;
            color: #fff;
            border: none;
            padding: 12px 22px;
            border-radius: 8px;
            cursor: pointer;
            font-size: 15px;
            font-weight: 600;
        }

        .tc-wrapper {

            max-width: 1000px;
            margin: auto;
            background: #fff;
            border: 8px solid #1e293b;
            border-radius: 12px;
            overflow: hidden;
        }

        .tc-header {

            background: linear-gradient(135deg, #1e3a8a, #2563eb);
            color: #fff;
            padding: 30px;
            text-align: center;
        }

        .school-logo img {

            width: 95px;
            height: 95px;
            object-fit: contain;
            background: #fff;
            padding: 6px;
            border-radius: 12px;
            margin-bottom: 15px;
        }

        .school-name {

            margin: 0;
            font-size: 34px;
            font-weight: 700;
            text-transform: uppercase;
        }

        .school-info {

            margin-top: 10px;
            font-size: 14px;
            line-height: 24px;
        }

        .tc-body {

            padding: 35px;
        }

        .tc-title {

            text-align: center;
            margin-bottom: 35px;
        }

        .tc-title h2 {

            display: inline-block;
            margin: 0;
            padding-bottom: 8px;
            border-bottom: 4px solid #2563eb;
            text-transform: uppercase;
            letter-spacing: 1px;
        }

        .top-info {

            display: flex;
            justify-content: space-between;
            margin-bottom: 30px;
            font-size: 15px;
            font-weight: 600;
        }

        .info-table {

            width: 100%;
            border-collapse: collapse;
        }

        .info-table th,
        .info-table td {

            border: 1px solid #cbd5e1;
            padding: 14px;
            font-size: 15px;
        }

        .info-table th {

            width: 35%;
            background: #f8fafc;
            text-align: left;
            color: #0f172a;
        }

        .declaration {

            margin-top: 35px;
            padding: 20px;
            background: #f8fafc;
            border-left: 5px solid #2563eb;
            line-height: 30px;
            font-size: 15px;
            text-align: justify;
        }

        .signatures {

            margin-top: 70px;
            display: flex;
            justify-content: space-between;
        }

        .signature-box {

            width: 220px;
            text-align: center;
            font-weight: 600;
        }

        .signature-line {

            margin-bottom: 10px;
            border-top: 1px solid #000;
            padding-top: 10px;
        }

        @media print {

            body {

                background: #fff;
                padding: 0;
            }

            .print-btn {

                display: none;
            }

            .tc-wrapper {

                border: none;
                border-radius: 0;
            }
        }

    </style>

</head>

<body>

<div class="print-btn">

    <button onclick="window.print()">

        🖨️ Print Transfer Certificate

    </button>

</div>

<div class="tc-wrapper">

    <!-- ====================================================== -->
    <!-- HEADER -->
    <!-- ====================================================== -->

    <div class="tc-header">

        <?php if (!empty($tc['school_logo'])): ?>

            <div class="school-logo">

                <img
                    src="../uploads/schools/logo/<?= htmlspecialchars($tc['school_logo']) ?>"
                    alt="School Logo"
                >

            </div>

        <?php endif; ?>

        <h1 class="school-name">

            <?= htmlspecialchars($tc['school_name']) ?>

        </h1>

        <div class="school-info">

            <?= htmlspecialchars($tc['school_address']) ?>

            <br>

            📞 <?= htmlspecialchars($tc['school_phone']) ?>

            |

            ✉️ <?= htmlspecialchars($tc['school_email']) ?>

        </div>

    </div>


    <!-- ====================================================== -->
    <!-- BODY -->
    <!-- ====================================================== -->

    <div class="tc-body">

        <div class="tc-title">

            <h2>

                Transfer Certificate

            </h2>

        </div>


        <!-- ====================================================== -->
        <!-- TOP INFO -->
        <!-- ====================================================== -->

        <div class="top-info">

            <div>

                TC No:
                <strong>

                    <?= htmlspecialchars($tc['tc_no']) ?>

                </strong>

            </div>

            <div>

                Issue Date:
                <strong>

                    <?= date('d M Y', strtotime($tc['issue_date'])) ?>

                </strong>

            </div>

        </div>


        <!-- ====================================================== -->
        <!-- STUDENT DETAILS -->
        <!-- ====================================================== -->

        <table class="info-table">

            <tr>

                <th>Student Name</th>

                <td>

                    <?= htmlspecialchars($tc['student_name']) ?>

                </td>

            </tr>

            <tr>

                <th>Father Name</th>

                <td>

                    <?= htmlspecialchars($tc['father_name'] ?? '-') ?>

                </td>

            </tr>

            <tr>

                <th>Mother Name</th>

                <td>

                    <?= htmlspecialchars($tc['mother_name'] ?? '-') ?>

                </td>

            </tr>

            <tr>

                <th>Admission Number</th>

                <td>

                    <?= htmlspecialchars($tc['admission_no'] ?? '-') ?>

                </td>

            </tr>

            <tr>

                <th>Roll Number</th>

                <td>

                    <?= htmlspecialchars($tc['roll_no'] ?? '-') ?>

                </td>

            </tr>

            <tr>

                <th>Class</th>

                <td>

                    <?= htmlspecialchars($tc['class_name'] ?? '-') ?>

                </td>

            </tr>

            <tr>

                <th>Date of Birth</th>

                <td>

                    <?= !empty($tc['dob']) ? date('d M Y', strtotime($tc['dob'])) : '-' ?>

                </td>

            </tr>

            <tr>

                <th>Gender</th>

                <td>

                    <?= htmlspecialchars($tc['gender'] ?? '-') ?>

                </td>

            </tr>

            <tr>

                <th>Address</th>

                <td>

                    <?= nl2br(htmlspecialchars($tc['address'] ?? '-')) ?>

                </td>

            </tr>

            <tr>

                <th>Reason</th>

                <td>

                    <?= htmlspecialchars($tc['reason']) ?>

                </td>

            </tr>

        </table>


        <!-- ====================================================== -->
        <!-- DECLARATION -->
        <!-- ====================================================== -->

        <div class="declaration">

            This is to certify that the above-mentioned student
            was studying in our institution and is hereby granted
            this Transfer Certificate upon request.

            We wish the student success in future studies and career.

        </div>


        <!-- ====================================================== -->
        <!-- SIGNATURE -->
        <!-- ====================================================== -->

        <div class="signatures">

            <div class="signature-box">

                <div class="signature-line"></div>

                Class Teacher

            </div>

            <div class="signature-box">

                <div class="signature-line"></div>

                Principal Signature

            </div>

        </div>

    </div>

</div>

<script>

window.onload = function () {

    window.print();
};

</script>

</body>

</html>