<?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) {

    header("Location: tc_list.php?msg=❌ TC Not Found!");

    exit;
}


// ======================================================
// PAGE
// ======================================================

$page_title = 'View TC';

include __DIR__ . '/../include/header.php';

include __DIR__ . '/../include/sidebar.php';

?>

<div class="content-wrapper">

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

    <section class="content-header">

        <div class="container-fluid">

            <div class="row mb-3">

                <div class="col-sm-6">

                    <h1>

                        <i class="fas fa-certificate text-primary"></i>

                        Transfer Certificate

                    </h1>

                </div>

                <div class="col-sm-6 text-right">

                    <a
                        href="tc_print.php?id=<?= $tc['id'] ?>"
                        target="_blank"
                        class="btn btn-success"
                    >

                        <i class="fas fa-print"></i>

                        Print TC

                    </a>

                    <a
                        href="tc_list.php"
                        class="btn btn-secondary"
                    >

                        <i class="fas fa-arrow-left"></i>

                        Back

                    </a>

                </div>

            </div>

        </div>

    </section>


    <!-- ====================================================== -->
    <!-- CONTENT -->
    <!-- ====================================================== -->

    <section class="content">

        <div class="container-fluid">

            <div class="row justify-content-center">

                <div class="col-lg-10">

                    <div class="card shadow-lg border-0">

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

                        <div class="card-header bg-primary text-white text-center py-4">

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

                                <div class="mb-3">

                                    <img
                                        src="../uploads/schools/logo/<?= htmlspecialchars($tc['school_logo']) ?>"
                                        alt="School Logo"
                                        style="
                                            width:100px;
                                            height:100px;
                                            object-fit:contain;
                                            background:#fff;
                                            padding:8px;
                                            border-radius:12px;
                                        "
                                    >

                                </div>

                            <?php endif; ?>


                            <h2 class="font-weight-bold mb-2">

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

                            </h2>

                            <p class="mb-1">

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

                            </p>

                            <p class="mb-0">

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

                                |

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

                            </p>

                        </div>


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

                        <div class="card-body p-5">

                            <div class="text-center mb-5">

                                <h3 class="text-uppercase font-weight-bold text-primary">

                                    Transfer Certificate

                                </h3>

                                <hr style="width:250px; border-top:3px solid #007bff;">

                            </div>


                            <!-- ====================================================== -->
                            <!-- TC DETAILS -->
                            <!-- ====================================================== -->

                            <div class="row mb-4">

                                <div class="col-md-6">

                                    <strong>

                                        TC Number:

                                    </strong>

                                    <span class="text-primary">

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

                                    </span>

                                </div>

                                <div class="col-md-6 text-md-right">

                                    <strong>

                                        Issue Date:

                                    </strong>

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

                                </div>

                            </div>


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

                            <div class="table-responsive">

                                <table class="table table-bordered table-striped">

                                    <tbody>

                                        <tr>

                                            <th width="35%">

                                                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>

                                    </tbody>

                                </table>

                            </div>


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

                            <div class="alert alert-light border mt-5">

                                <p class="mb-0" style="line-height:30px; text-align:justify;">

                                    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.

                                </p>

                            </div>


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

                            <div class="row mt-5 pt-4">

                                <div class="col-6 text-left">

                                    <div style="width:220px; border-top:1px solid #000; padding-top:10px;">

                                        Class Teacher

                                    </div>

                                </div>

                                <div class="col-6 text-right d-flex justify-content-end">

                                    <div style="width:220px; border-top:1px solid #000; padding-top:10px;">

                                        Principal Signature

                                    </div>

                                </div>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

        </div>

    </section>

</div>

<?php require_once __DIR__ . '/../include/footer.php'; ?>