<?php
if (isset($_SESSION['user_id'])) {
    _selectNoParam(
        $stmt,
        $count,
        "SELECT id, teacher.fname, teacher.lname, at FROM teacher WHERE id = '" . $_SESSION['user_id'] . "' and tuluv = 1",
        $id,
        $fname,
        $lname,
        $at
    );

    _selectNoParam(
        $cstmt,
        $ccount,
        "SELECT id, hezee, tsag, userid FROM att_work WHERE userid = '" . $_SESSION['user_id'] . "' ORDER BY hezee DESC LIMIT 300",
        $id,
        $hezee,
        $tsag,
        $userid
    );

    $cagArr = [];

    while (_fetch($cstmt)) {
        $item = new stdClass();
        $item->id = $id;
        $item->hezee = $hezee;
        $item->tsag = $tsag;
        $item->userid = $userid;
        array_push($cagArr, $item);
    }

?>
    <div class="page-header">
        <div class="page-header-left d-flex align-items-center">
            <div class="page-header-title">
                <h5 class="m-b-10 text-danger"><?= $this_on ?> ХИЧЭЭЛИЙН ЖИЛ</h5>
            </div>
            <ul class="breadcrumb">
                <li class="breadcrumb-item"><a href="/">Эхлэл</a></li>
            </ul>
        </div>
    </div>
    <div class="main-content">
        <div class="card stretch stretch-full">
            <div class="card-header">
                <h5 class="card-title">ЦАГ/ИРЦ БҮРТГЭЛ</h5>
            </div>
            <div class="card-body">
                <div id="table">
                    <table class="table table-bordered hovercell">
                        <thead class="table">
                            <tr>
                                <th>№</th>
                                <th>Багш/Ажилтан</th>
                                <th>Хэзээ</th>
                            </tr>
                        </thead>
                        <?php
                        $k = 0;
                        while (_fetch($stmt)) {
                            $k++; ?>
                            <tr>
                                <td style='text-align: center'><?= $k ?></td>
                                <td role="button" onclick="alert(<?= $id ?>)"><?= $fname ?> <?= $lname ?><br><small class="badge bg-info"><?= $at ?></small></td>
                                <td>
                                    <?php foreach ($cagArr as $cel) {
                                        if ($id == $cel->userid) {
                                            echo "<span class='alert alert-success'>$cel->hezee $cel->tsag</span>";
                                        }
                                    } ?>
                                </td>
                            </tr>
                        <?php
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <?php require ROOT . "/pages/main-end.php"; ?>
<?php
} else echo "Та дахин нэвтэрч орно уу! Холболт салсан байна"; ?>