<?php
if (isset($_SESSION['user_id'])) {
    _selectNoParam(
        $stmt,
        $count,
        "SELECT teacher.id, teacher.fname, teacher.lname, teacher.phone, teacher.email, teacher.at, teacher.pass, teacher.user_role, teacher.tuluv, teacher.office_id, teacher.department_id, at.name, tzereg.id, tzereg.name 
            FROM teacher INNER JOIN at ON teacher.user_role = at.id INNER JOIN tzereg ON teacher.zereg = tzereg.id ORDER BY lname",
        $id,
        $fname,
        $lname,
        $phone,
        $email,
        $at,
        $pass,
        $user_role,
        $tuluv,
        $office_id,
        $department_id,
        $user_type,
        $tzereg,
        $tzname
    );

    _select(
        $ostmt,
        $ocount,
        "SELECT id, name FROM office WHERE tuluv=?",
        "i",
        [1],
        $oid,
        $oname
    );

    _select(
        $dstmt,
        $dcount,
        "SELECT id, name FROM department WHERE tuluv=?",
        "i",
        [1],
        $did,
        $dname
    );
    $oarray = array();
    $darray = array();
    while (_fetch($ostmt)) {
        $orow = new stdClass();
        $orow->oid = $oid;
        $orow->oname = $oname;
        array_push($oarray, $orow);
    }

    while (_fetch($dstmt)) {
        $orow = new stdClass();
        $orow->did = $did;
        $orow->dname = $dname;
        array_push($darray, $orow);
    }
?>
    <table class="table table-bordered table-hover" id="datalist">
        <thead class="table">
            <tr>
                <th>№</th>
                <th></th>
                <th>Эцэг/эхийн нэр</th>
                <th>Нэр</th>
                <th>Утас</th>
                <th>E-mail</th>
                <th>Төрөл</th>
                <th>Албан тушаал</th>
                <th>Зэрэг</th>
                <th>Алба</th>
                <th>Хэлтэс</th>
                <th style="display: none"></th>
                <th>Төлөв</th>
                <th></th>
            </tr>
        </thead>
        <?php if ($count > 0) : ?>
            <?php $too = 0;
            while (_fetch($stmt)) :
                $too++ ?>
                <tr>
                    <td><?= $too ?></td>
                    <td role="button" class="text-center" data-bs-toggle="modal" data-bs-target="#imagechange" onclick="imageBtn(<?= $id ?>)">
                        <?php if (file_exists(ROOT . "/www/images/users/$id-t.jpg")) { ?>
                            <img src="/images/users/<?= $id ?>-t.jpg" id="imageList-<?= $id ?>" class="rounded-circle cover" height="30" loading="lazy" width="30">
                        <?php } else { ?>
                            <img src="/images/user.jpg" id="imageList-<?= $id ?>" class="rounded-circle cover" height="30" loading="lazy" width="30">
                        <?php } ?>
                    </td>
                    <td id="f1-<?= $id ?>"><?= $fname ?></td>
                    <td id="f2-<?= $id ?>"><?= $lname ?></td>
                    <td id="f3-<?= $id ?>"><?= $phone ?></td>
                    <td id="f4-<?= $id ?>"><small><?= $email ?></small></td>
                    <td id="f9-<?= $id ?>"><small class="badge bg-info"><?= $user_type ?></small></td>
                    <td id="f5-<?= $id ?>"><small><?= $at ?> </small></td>
                    <td><small><?= $tzname ?> </small></td>
                    <td id="f8-<?= $id ?>">
                        <small><?php
                                foreach ($oarray as $e) {
                                    if ($e->oid == $office_id) {
                                        echo $e->oname;
                                    }
                                }
                                ?>
                        </small>
                    </td>
                    <td id="f9-<?= $id ?>">
                        <small>
                            <?php
                            foreach ($darray as $e) {
                                if ($e->did == $department_id) {
                                    echo $e->dname;
                                }
                            }
                            ?>
                        </small>
                    </td>
                    <td id="f6-<?= $id ?>" style="display: none"><?= $user_role ?></td>
                    <td id="f7-<?= $id ?>"><small class="alert alert-<?= $tuluvColor[$tuluv] ?> p-1"><?= $tuluv_Teacher[$tuluv] ?></small></td>
                    <td>
                        <div class="dropdown">
                            <a href="javascript:void(0)" class="avatar-text avatar-md ms-auto" data-bs-toggle="dropdown" data-bs-offset="0,28" aria-expanded="false">
                                <i class="feather feather-more-horizontal"></i>
                            </a>
                            <ul class="dropdown-menu" style="">
                                <?php if (checkErh(6, $_SESSION['user_role'], $_SESSION['user_id'])) { ?>
                                    <li>
                                        <a class="dropdown-item" href="javascript:void(0)" role="button" onclick="pass(<?= $id ?>, '<?= $fname ?> <?= $lname ?>')">
                                            <i class="feather feather-key me-3"></i>
                                            <span>Нууц үг сэргээх</span>
                                        </a>
                                    </li>
                                <?php } ?>
                                <li>
                                    <a class="dropdown-item" href="javascript:void(0)" role="button" data-bs-toggle="modal" data-bs-target="#imagechange" onclick="imageBtn(<?= $id ?>)">
                                        <i class="feather feather-image me-3"></i>
                                        <span>Зураг солих</span>
                                    </a>
                                </li>
                                <li>
                                    <a class="dropdown-item" href="javascript:void(0)" role="button" data-bs-toggle="modal" data-bs-target="#change" onclick="editBtn(<?= $id ?>, '<?= $tuluv ?>', '<?= $office_id ?>', '<?= $department_id ?>', '<?= $tzereg ?>')">
                                        <i class="feather feather-edit me-3"></i>
                                        <span>Мэдээлэл засах</span>
                                    </a>
                                </li>
                                <li>
                                    <a class="dropdown-item" href="javascript:void(0)" role="button" data-bs-toggle="modal" data-bs-target="#delete" onclick="deleteBtn(<?= $id ?>)">
                                        <i class="feather feather-trash-2 me-3"></i>
                                        <span>Устгах</span>
                                    </a>
                                </li>
                            </ul>
                        </div>
                    </td>
                </tr>
            <?php endwhile; ?>
        <?php endif; ?>
    </table>
<?php
}
?>