<?php
$session_year = $_SESSION["session_year"];
$madrasa_code = $_SESSION["madrasa_code"] ?? 1;

if (isset($_POST['submit'])) {
    $class = $_POST['class'] ?? '';
    $start_date = $_POST['start_date'] ?? '';
    $end_date = $_POST['end_date'] ?? '';
} else {
    $start_date = '';
    $end_date = '';
}
?>

<div class="container-fluid py-4">
    <!-- Form Card -->
    <div class="card shadow-sm border-0 mb-4">
        <div class="card-header bg-primary text-white d-flex align-items-center">
            <h5 class="mb-0"><i class="fas fa-id-card mr-2"></i> আইডি কার্ড তথ্য ইনপুট</h5>
        </div>
        <div class="card-body">
            <form action="" method="post">
                <div class="form-row align-items-end">
                    <div class="col-md-5 mb-3">
                        <label class="font-weight-bold text-secondary text-sm">ইস্যু তারিখ <span
                                class="text-danger">*</span></label>
                        <input type="date" class="form-control" name="start_date" value="<?php echo $start_date; ?>"
                            required>
                    </div>
                    <div class="col-md-5 mb-3">
                        <label class="font-weight-bold text-secondary text-sm">মেয়াদ শেষ <span
                                class="text-danger">*</span></label>
                        <input type="date" class="form-control" name="end_date" value="<?php echo $end_date; ?>"
                            required>
                    </div>
                    <div class="col-md-2 mb-3">
                        <button type="submit" name="submit" class="btn btn-success btn-block shadow-sm">
                            <i class="fas fa-sync-alt mr-1"></i> রিপোর্ট দেখুন
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>


    <!-- Print Buttons Card -->
    <div class="card shadow-sm border-0 mb-4">
        <div class="card-body bg-light text-center">
            <div class="d-flex flex-wrap justify-content-center" style="gap: 10px;">
                <a target="_blank" href="male/teacher/teacher_idcard_1_print.php?class=all"
                    class="btn btn-info btn-sm px-3 shadow-sm">
                    <i class="fas fa-print mr-1"></i> কার্ড ১
                </a>
                <a target="_blank" href="male/teacher/student_idcard_2_print.php?class=all"
                    class="btn btn-info btn-sm px-3 shadow-sm">
                    <i class="fas fa-print mr-1"></i> কার্ড ২
                </a>
                <a target="_blank" href="male/teacher/student_idcard_3_print.php?class=all"
                    class="btn btn-purple btn-sm px-3 shadow-sm text-white" style="background-color: #6f42c1;">
                    <i class="fas fa-print mr-1"></i> কার্ড ৩
                </a>
                <a target="_blank" href="male/teacher/student_idcard_4_print.php?class=all"
                    class="btn btn-dark btn-sm px-3 shadow-sm">
                    <i class="fas fa-print mr-1"></i> কার্ড ৪
                </a>
                <a target="_blank" href="male/teacher/student_idcard_download_print.php?class=all"
                    class="btn btn-primary btn-sm px-3 shadow-sm">
                    <i class="fas fa-download mr-1"></i> ডাউনলোড
                </a>
                <a target="_blank"
                    href="male/teacher/student_idcard_bacpart_print.php?class=all&start_date=<?php echo $start_date; ?>&end_date=<?php echo $end_date; ?>"
                    class="btn btn-secondary btn-sm px-3 shadow-sm">
                    <i class="fas fa-file-invoice mr-1"></i> ব্যাকপার্ট
                </a>
                <a target="_blank" href="male/student/student_idcard_correction_print.php?class=all"
                    class="btn btn-danger btn-sm px-3 shadow-sm">
                    <i class="fas fa-exclamation-triangle mr-1"></i> কারেকশন
                </a>
            </div>
        </div>
    </div>


    <!-- Teacher List Card -->
    <div class="card shadow-sm border-0">
        <div class="card-header bg-white border-bottom py-3">
            <h5 class="mb-0 font-weight-bold text-dark"><i class="fas fa-users mr-2"></i> শিক্ষকদের তালিকা</h5>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover table-striped mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="text-center border-0">ক্র.মি.</th>
                            <th class="border-0">আইডি</th>
                            <th class="border-0">নাম</th>
                            <th class="text-center border-0">ছবি</th>
                            <th class="border-0">ঠিকানা</th>
                            <th class="border-0">মোবাইল</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $i = 1;
                        $sql = mysqli_query($con, "SELECT DISTINCT * FROM `teacher` WHERE madrasa_code=$madrasa_code AND status=1 ORDER BY teacher_sr DESC");

                        if (mysqli_num_rows($sql) > 0) {
                            while ($row = mysqli_fetch_assoc($sql)) {
                                $image = !empty($row['student_img']) ? "../student_img/" . $row['student_img'] : "male/img/blank_image.png";
                                ?>
                                <tr>
                                    <td class="text-center align-middle text-muted"><?php echo $i++; ?></td>
                                    <td class="align-middle">
                                        <span
                                            class="badge badge-light p-2 border font-weight-bold text-primary"><?php echo $row['teacher_id']; ?></span>
                                    </td>
                                    <td class="align-middle font-weight-bold text-dark"><?php echo $row['name']; ?></td>
                                    <td class="text-center align-middle">
                                        <img src="<?php echo $image; ?>" class="rounded shadow-sm"
                                            style="width: 45px; height: 45px; object-fit: cover; border: 1px solid #dee2e6;"
                                            alt="Teacher">
                                    </td>
                                    <td class="align-middle text-secondary small">
                                        <i class="fas fa-map-marker-alt mr-1"></i>
                                        <?php echo $row['village'] . ', ' . $row['thana_name']; ?>
                                    </td>
                                    <td class="align-middle">
                                        <a href="tel:<?php echo $row['mobile']; ?>"
                                            class="btn btn-outline-primary btn-sm border-0 font-weight-bold">
                                            <i class="fas fa-phone-alt mr-1"></i> <?php echo $row['mobile']; ?>
                                        </a>
                                    </td>
                                </tr>
                                <?php
                            }
                        } else {
                            echo "<tr><td colspan='6' class='text-center py-5 text-muted'>কোনো তথ্য পাওয়া যায়নি</td></tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<style>
    /* Essential small helpers over Bootstrap 4 */
    .btn-purple {
        color: #fff;
        background-color: #6f42c1;
        border-color: #6f42c1;
    }

    .btn-purple:hover {
        color: #fff;
        background-color: #59359a;
        border-color: #4e2e89;
    }

    .text-sm {
        font-size: 0.875rem;
    }

    .card {
        border-radius: 0.5rem;
        overflow: hidden;
    }

    .table thead th {
        border-bottom-width: 0;
    }

    .badge {
        border-radius: 0.25rem;
    }
</style>