<%-include("../partials/header.ejs") %>
<%-include("../partials/sidebar.ejs") %>
<div class="app-content">
    <%-include("../partials/navbar.ejs") %>

    <main>
        <div class="container-fluid">
            <!-- Breadcrumb start -->
            <div class="row m-1">
                <div class="col-12">
                    <h4 class="main-title">Puan Kataloğu</h4>
                    <p>
                        Öğrencileri değerlendireceğiniz kategorileri belirleyin
                    </p>
                </div>
            </div>
            <div class="row">
                <div class="col-12">
                    <div class="card ">
                        <div class="card-header ">
                            <div class="d-flex flex-row flex-1">
                                <h5 class="flex-fill">
                                    Puan Kataloğu listesi
                                </h5>
                                <div class="flex-min">
                                    <button class="btn btn-success" data-bs-target="#add_catalog" data-bs-toggle="modal" type="button">
                                        <i class="fa fa-plus"></i>
                                        Puan Kataloğu ekle
                                    </button>
                                </div>
                            </div>
                        </div>
                        <div class="card-body p-0">
                            <div class="app-datatable-default overflow-auto">
                                <table class="table display app-data-table default-data-table" id="cataloglist"></table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>
</div>
<div aria-hidden="true" class="modal fade" id="add_catalog" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Yeni Katalog Ekle</h5>
                <button aria-label="Close" class="btn-close m-0 fs-5" data-bs-dismiss="modal" type="button"></button>
            </div>
            <form class="modal-body" id="catalogsaveform" onsubmit="saveCatalog(this);return false;">
                <div class="row app-form">
                    <div class="col-md-6 mt-3">
                        <label class="form-label">Aktivite Adı <small class="text-danger">*</small></label>
                        <input type="text" class="form-control" name="name" placeholder="Aktivite Adı" required min="3">
                    </div>
                    <div class="col-md-6 mt-3">
                        <label class="form-label">Puanı <small class="text-danger">*</small></label>
                        <input type="number" class="form-control" name="score" placeholder="Skor değeri" min="0" step="1" value="10" required>
                    </div>
                </div>
            </form>
            <div class="modal-footer">
                <button class="btn btn-outline-success" type="submit" form="catalogsaveform">Kaydet</button>
                <button class="btn btn-light-secondary" data-bs-dismiss="modal" type="button">Kapat</button>
            </div>
        </div>
    </div>
</div>
<div aria-hidden="true" class="modal fade" id="update_catalog" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Öğrenci güncelle</h5>
                <button aria-label="Close" class="btn-close m-0 fs-5" data-bs-dismiss="modal" type="button"></button>
            </div>
            <form class="modal-body" id="catalogupdateform" onsubmit="updateCatalog(this);return false;">
                <input type="hidden" name="id">
                <div class="row app-form">
                    <div class="col-md-6 mt-3">
                        <label class="form-label">Aktivite Adı <small class="text-danger">*</small></label>
                        <input type="text" class="form-control" name="name" placeholder="Aktivite Adı" required min="3">
                    </div>
                    <div class="col-md-6 mt-3">
                        <label class="form-label">Puanı <small class="text-danger">*</small></label>
                        <input type="number" class="form-control" name="score" placeholder="Skor değeri" min="0" step="1" value="10" required>
                    </div>
                </div>
            </form>
            <div class="modal-footer">
                <button class="btn btn-outline-success" type="submit" form="catalogupdateform">Güncelle</button>
                <button class="btn btn-light-secondary" data-bs-dismiss="modal" type="button">Kapat</button>
            </div>
        </div>
    </div>
</div>
<script>
    let dataTable;
    $(function(){
        dataTable = $("#cataloglist").DataTable({
            serverSide: true,
            processing: true,
            autoWidth: false,
            ajax: {
                url: "/catalog/list",
                method:"post"
            },
            columns: [
                {
                    width: "1%",
                    title: "#",
                    data: null,
                    render: function (data, type, row, meta) {
                        return meta.row + 1;
                    }
                },
                {
                    title: "Katalog Adı",
                    data: "name",
                    name: "name"
                },
                {
                    title: "Skor",
                    data: "score",
                    name: "score"
                },
                {
                    width: "1%",
                    title: "İşlemler",
                    data: null,
                    orderable: false, // İşlemlere göre sıralama yapılamasın
                    render: function (data) {
                        return `
                            <div class="btn-group d-flex flex-row flex-nowrap" role="group">
                                <button class="btn btn-sm btn-outline-primary text-nowrap" onclick="updateModal(this)" title="Düzenle">
                                    <i class="fa fa-edit"></i>
                                    Düzenle
                                </button>
                                <button class="btn btn-sm btn-outline-danger text-nowrap" onclick="deleteCatalog(${data.id})" title="Sil">
                                    <i class="fa fa-trash"></i>
                                    Sil
                                </button>
                            </div>`;
                    }
                }
            ],
            "language": {
                "url": "https://cdn.datatables.net/plug-ins/1.13.6/i18n/tr.json"
            }
        });
    });
    async function saveCatalog(form)
    {
        blockui("#add_catalog .modal-content");
        $.ajax({
            url: "/catalog/store",
            type: "post",
            data: new FormData(form),
            processData: false,
            contentType: false,
            success: function(response) {
                dataTable.ajax.reload(null, false);
                $("#add_catalog").modal("hide");
                ublockui();
            },
            error: function(err) {
                ublockui();
            },
        });
    }
    async function deleteCatalog(id)
    {
        if(confirm("Kaydı silmek istediğinize emin misiniz?"))
        {
            $.ajax({
                url: "/catalogs/destroy",
                type: "post",
                data: {
                    id
                },
                success: function(response) {
                    dataTable.ajax.reload(null, false);
                    ublockui();
                },
                error: function(err) {
                    ublockui();
                },
            });
        }
    }
    async function updateCatalog(form)
    {
        blockui("#update_catalog .modal-content");
        $.ajax({
            url: "/catalog/update",
            type: "post",
            data: new FormData(form),
            processData: false,
            contentType: false,
            success: function(response) {
                dataTable.ajax.reload(null, false);
                $("#update_catalog").modal("hide");
                ublockui();
            },
            error: function(err) {
                ublockui();
            },
        });
    }
    
    async function updateModal(tr)
    {
        let data = dataTable.row($(tr).closest('tr')).data();

        $("#update_catalog form").get(0).reset();
        $("#update_catalog [name='id']").val(data.id);

        $("#update_catalog form input,#update_catalog form textarea").each(function(){
            let name = this.name;
            $(this).val(data[name]);
        });
        $("#update_catalog form select").each(function(){
            let name = this.name;
            $(this).find("option:selected").removeAttr("selected");
            $(this).find(`option[value="${data[name]}"]`).attr("selected","selected");
        });
        $("#update_catalog").modal("show");
    }
    function blockui(ui)
    {
        $(blockui.id = ui).block({
            message: '<div class="loader-container-box"><div class="loader"></div></div>',
            //timeout: 13000,
            overlayCSS: {
                backgroundColor: 'rgba(var(--dark), 0.8)',
                opacity: 0.8,
                borderRadius: 'var(--app-border-radius)',
                cursor: 'wait'
            },
            css: {
                border: 0,
                padding: 0,
                backgroundColor: 'transparent'
            }
        });
    }
    function ublockui()
    {
        $(blockui.id).unblock()
    }
</script>
<%-include("../partials/footer.ejs") %>