<?php
require ROOT . "/pages/start.php";
require ROOT . "/pages/header.php";

_select(
    $tstmt,
    $tcount,
    "SELECT id, fname, UPPER(lname) FROM teacher WHERE tuluv=? and user_role=1 ORDER BY lname",
    "i",
    ['1'],
    $tid,
    $fname,
    $lname
);

$teachers = array();

while (_fetch($tstmt)) {
    $item = new stdClass();
    $item->tid = $tid;
    $item->name = $fname . " " . $lname;

    array_push($teachers, $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>
            <li class="breadcrumb-item">Бүртгэл</li>
            <li class="breadcrumb-item">Анги</li>
        </ul>
    </div>
</div>
<div class="main-content">
    <div class="card stretch stretch-full">
        <div class="card-header">
            <h5 class="card-title">Ангийн бүртгэл</h5>
            <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#add">БҮРТГЭХ</button>
        </div>
        <div class="card-body overflow-auto">
            <div id="table">

            </div>
        </div>
    </div>
</div>
<?php require ROOT . "/pages/main-end.php"; ?>

<div class="modal fade" id="change" tabindex="-1" aria-labelledby="changeLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="changeLabel">Анги засах</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="form-outline mb-4">
                    <label class="form-label" for="angi_name">Мэргэжил</label>
                    <input type="text" value="" id="angi_name" class="form form-control mb-3" />
                </div>
                <input type="text" value="0" id="angi_id" readonly style="display: none;" />
                <div class="row mb-4">
                    <div class="col-2">
                        <label class="form-label" for="hugacaa">Анги</label>
                        <input type="text" value="" id="angi" placeholder="1-2" class="form form-control mb-3" />
                    </div>
                    <div class="col-2">
                        <label class="form-label" for="hugacaa">Хугацаа</label>
                        <select class="form form-control mb-3" id="hugacaa">
                            <option>3</option>
                            <option>1</option>
                            <option>1.5</option>
                        </select>
                    </div>
                    <div class="col">
                        <label class="form-label" for="teacherList">Багш</label>
                        <select class="form form-control mb-3" id="teacherList">
                            <?php foreach ($teachers as $el) : ?>
                                <option value="<?= $el->tid ?>"><?= $el->name ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
                <div id="changeinfo" class="alert alert-warning" style="display: none;">

                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Хаах</button>
                <button type="button" class="btn btn-primary" onclick="editAngi()">Хадгалах</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="add" tabindex="-1" aria-labelledby="addLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="addLabel">Анги бүртгэх</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="form-outline mb-4">
                    <label class="form-label" for="add_angi_name">Мэргэжил</label>
                    <input type="text" value="" id="add_angi_name" class="form form-control" />
                </div>
                <div class="row mb-4">
                    <div class="col-3">
                        <label class="form-label" for="hugacaa">Ангийн нэр</label>
                        <input type="text" value="" id="addangi" placeholder="1-2" class="form form-control mb-3" />
                    </div>
                    <div class="col-2">
                        <label class="form-label" for="addhugacaa">Хугацаа</label>
                        <select class="form form-control" id="addhugacaa">
                            <option>3</option>
                            <option>1</option>
                            <option>1.5</option>
                        </select>
                    </div>
                    <div class="col">
                        <label class="form-label" for="add_teacher_id">Багш</label>
                        <select class="form form-control" id="add_teacher_id">
                            <?php foreach ($teachers as $el) : ?>
                                <option value="<?= $el->tid ?>"><?= $el->name ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
                <div id="addinfo" class="alert alert-warning" style="display: none;">

                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Хаах</button>
                <button type="button" class="btn btn-primary" onclick="addAngi()">Хадгалах</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="delete" tabindex="-1" aria-labelledby="deleteLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="deleteLabel">Анги устгах</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div id="deletebody" class="mb-3">

                </div>
                <input type="text" value="0" id="delete_angi_id" readonly style="display: none;" />
                <div id="deleteinfo" class="alert alert-warning" style="display: none;">

                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Болих</button>
                <button type="button" class="btn btn-danger" onclick="deleteAngi()">Устгах</button>
            </div>
        </div>
    </div>
</div>
</div>
<?php
require ROOT . "/pages/footer.php"; ?>
<script>
    function grad(id, text) {
        let result = confirm(text + " ангийг <?= $this_on ?> хичээлийн жилд төгсөлтөөр бүртгэхдээ итгэлтэй байна уу? Нийт суралцагчид төгсөгчөөр бүртгэгдэх болно.");
        if (result === true) {
            $.ajax({
                url: "ajax",
                type: "POST",
                data: {
                    mode: "grad",
                    angi_id: id
                },
                error: function(xhr, textStatus, errorThrown) {

                },
                beforeSend: function() {

                },
                success: function(data) {
                    if (data === "Амжилттай!") {
                        window.location.reload();
                    } else alert(data);
                },
                async: true
            });
        }
    }

    function get() {
        $.ajax({
            url: "ajax-list",
            type: "POST",
            data: {
                angi_id: $('#angi_id').val(),
                teacher_id: $('#teacherList').val()
            },
            error: function(xhr, textStatus, errorThrown) {
                $("#table").html("Алдаа гарлаа !");
            },
            beforeSend: function() {
                $("#table").html("Түр хүлээнэ үү ...");
            },
            success: function(data) {
                $("#table").html(data);
            },
            async: true
        });
    }
    get();

    function deleteAngi() {
        $.ajax({
            url: "ajax",
            type: "POST",
            data: {
                mode: 3,
                angi_id: $('#delete_angi_id').val()
            },
            error: function(xhr, textStatus, errorThrown) {
                $('#deleteinfo').show();
                $("#deleteinfo").html("Алдаа гарлаа !");
            },
            beforeSend: function() {
                $('#deleteinfo').show();
                $("#deleteinfo").html("Түр хүлээнэ үү!");
            },
            success: function(data) {
                if (data === "Амжилттай!") {
                    get();
                    $('#delete').modal('hide');
                } else $("#deleteinfo").html(data);
            },
            async: true
        });
    }

    function editAngi() {
        if ($('#teacherList').val() === null) {
            $('#changeinfo').html("Багш сонго!");
            $('#changeinfo').show();
        } else {
            $.ajax({
                url: "ajax",
                type: "POST",
                data: {
                    mode: 1,
                    angi_id: $('#angi_id').val(),
                    teacher_id: $('#teacherList').val(),
                    angi_name: $('#angi_name').val(),
                    hugacaa: $('#hugacaa').val(),
                    angi: $('#angi').val()
                },
                error: function(xhr, textStatus, errorThrown) {
                    $('#changeinfo').show();
                    $("#changeinfo").html("Алдаа гарлаа !");
                },
                beforeSend: function() {
                    $('#changeinfo').show();
                    $("#changeinfo").html("Түр хүлээнэ үү ...");
                },
                success: function(data) {
                    get();
                    $('#change').modal('hide');
                },
                async: true
            });
        }
    }

    function addAngi() {
        $('#addinfo').hide();
        if ($('#add_teacher_id').val() === null || $('#add_angi_name').val() === '') {
            $('#addinfo').html("Мэдээлэл дутуу байна!");
            $('#addinfo').show();
        } else {
            $.ajax({
                url: "ajax",
                type: "POST",
                data: {
                    mode: 2,
                    teacher_id: $('#add_teacher_id').val(),
                    angi_name: $('#add_angi_name').val(),
                    hugacaa: $('#addhugacaa').val(),
                    angi: $('#addangi').val()
                },
                error: function(xhr, textStatus, errorThrown) {
                    $("#addbody").html("Алдаа гарлаа !");
                },
                beforeSend: function() {
                    $('#addinfo').html("Түр хүлээнэ үү ...");
                    $('#addinfo').show();
                },
                success: function(data) {
                    get();
                    $('#addinfo').html("");
                    $('#addinfo').hide();
                    $('#add_angi_name').val("");
                    $('#addangi').val("");
                    $('#add').modal('hide');
                },
                async: true
            });
        }
    }

    function setTeacher(id, angi) {
        $('#changeinfo').hide();
        $('#teacherList').val(id);
        $('#angi_id').val(angi);
        $('#angi_name').val($('#f1-' + angi).text());
        $('#angi').val($('#f0-' + angi).text());
    }

    function deleteBtn(angi) {
        $('#deleteinfo').hide();
        $('#delete_angi_id').val(angi);
        $('#deletebody').html('"' + $('#f1-' + angi).text() + '" ангийг утгахдаа итгэлтэй байна уу?');
    }
</script>
<?php
require ROOT . "/pages/end.php";
?>