var VPKompetensiDasar = (function () {
    let gridConf = {
        ready: false,
        data: [],
        id: "kd-table",
        ref: "kd-table",
        columns: [{ id: 'nomor', name: "Nomor" }, { id: 'nama', name: "Nama" }, { id: 'kode', name: "Kode" },
        { id: 'nama_mapel', name: "Mata Pelajaran" }, { id: 'kode_tingkat', name: "Tingkat" }, { id: 'created_by', name: "Dibuat Oleh" },
        {
            id: 'id',
            name: 'Aksi',
            formatter: (_, row) => {
                let btn = `<div class="d-flex">&nbsp;<button class="btn btn-primary rounded-md" id="trigger-edit" data-id="${row.cells[6].data}"><i id="trigger-edit" data-id="${row.cells[6].data}" class='lni lni-pencil-alt'></i></button>`;
                btn += `&nbsp;<button class="btn btn-danger rounded-md" id="trigger-delete" data-id="${row.cells[6].data}"><i id="trigger-delete" data-id="${row.cells[6].data}" class='lni lni-trash-can'></i></button>`;
                btn += "</div>";
                return gridjs.html(btn);
            }
        },
        ]
    };
    let vp_template = `
    <div class="card-style">
        <div style="text-align:right">
            <button class="main-btn btn-sm success-btn rounded-md btn-hover" @click="filterModal('show')">
                <i class="fas fa-filter"></i>
            </button>
            &nbsp;
            <button class="main-btn btn-sm primary-btn rounded-md btn-hover" @click="modal('show')">
                <i class="lni lni-plus"></i>
            </button>
            &nbsp;
            <button class="main-btn btn-sm success-btn rounded-md btn-hover" @click="$refs[grid.ref].reload()">
                <i class="lni lni-reload"></i>
            </button>
        </div>
        <div class="table-responsive">
            <grid :id="grid.id" :limits="true" :url="urlkompetensi" :ref="grid.ref" :columns="grid.columns"
                :ondataload="ondataload"></grid>
        </div>
    </div>
    <!-- Modal -->

    <div class="modal fade" ref="modal"  tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-centered">
          <div class="modal-content card-style">
            <div class="modal-header px-0 border-0">
              <h5 class="text-bold">{{mdl.id?'Ubah':'Tambah'}} Kompetensi Dasar</h5>
              <button class="border-0 bg-transparent h1" data-bs-dismiss="modal">
                <i class="lni lni-cross-circle"></i>
              </button>
            </div>
            <div class="modal-body px-0">
              <div class="content">
                <form @submit.prevent="mdl.id?edit():add()">
                <div class="my-4" >
                  <label>Mata Pelajaran</label>
                  <tomselect id="mapel" :options="mdl.mapel.options" :value="mdl.mapel.value" @changed="mdl.mapel.value=$event" placeholder="Pilih Mata Pelajaran"  eltype="select" style="width: 100%;">
                  </tomselect>
                </div>
                <div class="my-4" >
                  <label>Tingkat</label>
                  <tomselect id="tingkat" :options="mdl.tingkat.options" :value="mdl.tingkat.value" @changed="mdl.tingkat.value=$event" placeholder="Pilih Tingkat"  eltype="select" style="width: 100%;">
                  </tomselect>
                </div>
                <div class="select-style-1">
                    <label>Kode</label>
                    <div class="select-position">
                        <select required v-model="mdl.kode">
                            <option value="KP">Kompetensi Pengetahuan</option>
                            <option value="KK">Kompetensi Keterampilan</option>
                        </select>
                    </div>
                </div>
                <div class="input-style-1">
                    <label>Nomor</label>
                    <input required v-model="mdl.nomor" min="1" type="number" placeholder="Nomor">
                </div>
                <div class="input-style-1">
                    <label>Nama</label>
                    <input required v-model="mdl.nama"  placeholder="Nama">
                </div>
                <div class="accordion my-2" id="accordionExample">
                    <div class="accordion-item">
                    <h2 class="accordion-header" id="headingOne">
                        <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                            Daftar Kompetensi Sudah Ada
                        </button>
                    </h2>
                    <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
                        <div class="accordion-body">
                            <div v-if="listKompetensi.length<=0" class="my-2 text-center">Belum Ada Kompetensi</div>
                            <table v-else class="table table-bordered border-primary">
                                <tbody>
                                    <tr v-for="kom in listKompetensi">
                                        <td class="p-2">{{kom.nomor}}</td>
                                        <td class="p-2" >{{kom.nama}}</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    </div>
                
                </div>
                <div class="my-4" v-if="mdl.guru.show">
                  <label>Guru</label>
                  <tomselect id="guru" :options="mdl.guru.data" :value="mdl.guru.value" @changed="mdl.guru.value=$event" placeholder="Pilih Guru"  eltype="select" style="width: 100%;">
                  </tomselect>
                </div>
                <div class="action d-flex flex-wrap justify-content-end">
                  <button type="submit" :disabled="mdl.loading" class="main-btn btn-sm primary-btn btn-hover">
                    <template v-if="mdl.loading"> <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
                    </template>
                    <template v-else>
                      {{mdl.id?'Simpan':'Tambah'}}
                    </template>
                  </button>
              </div>
                </form>
              </div>
            </div>
          </div>
        </div>
    </div>
    <!-- Filter Modal -->
    <div class="modal fade" ref="filterModal"  tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-centered">
          <div class="modal-content card-style">
            <div class="modal-header px-0 border-0">
              <h5 class="text-bold">Filter</h5>
              <button class="border-0 bg-transparent h1" data-bs-dismiss="modal">
                <i class="lni lni-cross-circle"></i>
              </button>
            </div>
            <div class="modal-body px-0">
              <div class="content">
                <div class="select-style-1">
                    <label>Tingkat</label>
                    <div class="select-position">
                        <select required v-model="filterMdl.tingkat_id">
                            <option value="">Semua</option>
                            <option v-for="tingkat in mdl.tingkat.options" :value="tingkat.value">{{tingkat.text}}</option>
                        </select>
                    </div>
                </div>
                <div class="select-style-1">
                    <label>Mata Pelajaran</label>
                    <div class="select-position">
                        <select required v-model="filterMdl.mapel_id">
                            <option value="">Semua</option>
                            <option v-for="mapel in mdl.mapel.options" :value="mapel.value">{{mapel.text}}</option>
                        </select>
                    </div>
                </div>
              </div>
            </div>
          </div>
        </div>
    </div>
    `;
    return {
        template: vp_template,
        name: "kompetensi-dasar",

        data() {
            return {
                grid: gridConf,

                filterMdl: {
                    tingkat_id: '',
                    mapel_id: '',
                },
                createdKompetensi: [],
                mdl: {
                    guru: {
                        show: false,
                        value: null,
                        data: []
                    },
                    mapel: {
                        value: null,
                        options: [],
                        raw: []
                    },
                    tingkat: {
                        value: null,
                        options: [],
                        raw: []
                    },
                    kode: 'KP'
                },
            }
        },
        mounted() {

            this.eventListener();
            this.loadConfig();
            this.loadMapel();
            this.loadTingkat();
        },
        computed: {
            listKompetensi() {
                return this.createdKompetensi.filter(v => v.kode == this.mdl.kode).sort((a, b) => parseFloat(a.nomor) - parseFloat(b.nomor));
            },
            urlkompetensi() {
                let url = window.location.href + "/all?";
                if (this.filterMdl.tingkat_id) url += `&tingkat_id=${this.filterMdl.tingkat_id}`;
                if (this.filterMdl.mapel_id) url += `&mapel_id=${this.filterMdl.mapel_id}`;
                return url;
            },
            mata_pelajaran: {
                get() {
                    return this.mdl.mapel.value;
                },
                set(v) {
                    this.mdl.mapel.value = v;
                }
            },
            tingkat: {
                get() {
                    return this.mdl.tingkat.value;
                },
                set(v) {
                    this.mdl.tingkat.value = v;
                }
            },
        },
        watch: {
            mata_pelajaran(v) {
                this.loadKompetensi();
            },
            tingkat(v) {
                this.loadKompetensi();
            },
        },
        methods: {
            eventListener() {
                document.addEventListener('click', (e) => {
                    if (e.target && e.target.id == 'trigger-delete') {
                        this.delete(e.target.dataset.id)
                    }
                    if (e.target && e.target.id == 'trigger-edit') {
                        this.detail(e.target.dataset.id)
                    }
                    this.$refs.modal.addEventListener('hide.bs.modal', (e) => {
                        this.mdl.id = null;
                    });
                });
            },
            detail(id) {
                showDismisableLoading();
                axios({
                    method: 'get',
                    url: window.location.href + "/detail/" + id,
                }).then((response) => {
                    Swal.close();
                    if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error');
                    this.mdl.id = response.data.data.id;
                    this.mdl.nomor = response.data.data.nomor;
                    this.mdl.nama = response.data.data.nama;
                    this.mdl.kode = response.data.data.kode;
                    this.mdl.mapel.value = response.data.data.mapel_id;
                    this.mdl.tingkat.value = response.data.data.tingkat_id;
                    this.mdl.guru.value = response.data.data.user_id;
                    this.modal("show")
                }, (error) => {
                    Swal.fire('Error', gaem(error), 'error')
                });
            },
            add() {
                let data = {
                    [this.$store.state.config.csrf.name]: this.$store.state.config.csrf.value,
                    nomor: this.mdl.nomor,
                    nama: this.mdl.nama,
                    kode: this.mdl.kode,
                    mapel_id: this.mdl.mapel.value,
                    tingkat_id: this.mdl.tingkat.value,
                    user_id: this.mdl.guru.value
                };
                this.mdl.loading = true;
                let url = window.location.href + "/add"
                axios({
                    method: 'POST',
                    data: data,
                    url: url,
                }).then((response) => {
                    if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error');
                    Swal.fire('Success', response.data.message, 'success')
                    this.modal("hide");
                    this.$refs[this.grid.ref].reload()
                    this.loadKompetensi();
                }, (error) => {
                    Swal.fire('Error', gaem(error), 'error')
                }).finally(() => this.mdl.loading = false);
            },
            edit() {
                let data = {
                    [this.$store.state.config.csrf.name]: this.$store.state.config.csrf.value,
                    nomor: this.mdl.nomor,
                    nama: this.mdl.nama,
                    kode: this.mdl.kode,
                    mapel_id: this.mdl.mapel.value,
                    tingkat_id: this.mdl.tingkat.value,
                    user_id: this.mdl.guru.value
                };
                this.mdl.loading = true;
                let url = window.location.href + "/edit/" + this.mdl.id;
                axios({
                    method: 'PATCH',
                    data,
                    url,
                }).then((response) => {
                    if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error');
                    Swal.fire('Success', response.data.message, 'success')
                    this.modal("hide");
                    this.$refs[this.grid.ref].reload()
                    this.loadKompetensi();
                }, (error) => {
                    this.mdl.loading = false;
                    Swal.fire('Error', gaem(error), 'error')
                }).finally(() => this.mdl.loading = false);
            },
            loadMapel() {
                axios({
                    method: 'get',
                    url: window.location.href + `/mapel`
                }).then((response) => {
                    if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error');
                    this.mdl.mapel.raw = response.data.data;
                    this.mdl.mapel.options = response.data.data.map((v, i) => {
                        if (i == 0) this.mdl.mapel.value = v.id;
                        return { value: v.id, text: `${v.kode} - ${v.nama}` };
                    })
                }, (error) => {
                    Swal.fire('Gagal', 'Terjadi Kesalahan Saat Mengambil Data Mata Pelajaran', 'error')
                });
            },
            loadTingkat() {
                axios({
                    method: 'get',
                    url: this.$store.state.config.base_url + `/panel/e-learning/kompetensi-dasar/tingkat`
                }).then((response) => {
                    if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error');
                    this.mdl.tingkat.options = response.data.data.map((v, i) => {
                        if (i == 0) this.mdl.tingkat.value = v.id;
                        return { value: v.id, text: v.kode }
                    });
                }, (error) => {
                    Swal.fire('Gagal', 'Terjadi Kesalahan Saat Mengambil Data Tingkat', 'error')
                });
            },
            loadKompetensi() {
                let url = window.location.href + "/all?";
                if (this.tingkat) url += `&tingkat_id=${this.tingkat}`;
                if (this.mata_pelajaran) url += `&mapel_id=${this.mata_pelajaran}`;
                axios({
                    method: 'get',
                    url,
                }).then((response) => {
                    if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error');
                    this.createdKompetensi = response.data.data;
                }, (error) => {
                    Swal.fire('Error', gaem(error, 'Terjadi Kesalahan Saat Mengambil Data Kompetensi Dasar'), 'error')
                });
            },
            delete(id) {
                SwalDelete({
                    confirmed: () => {
                        showDismisableLoading();
                        axios({
                            method: 'delete',
                            data: { [this.$store.state.config.csrf.name]: this.$store.state.config.csrf.value, },
                            url: window.location.href + "/delete/" + id,
                        }).then((response) => {
                            Swal.close();
                            if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error');
                            this.$refs[this.grid.ref].reload();
                            Swal.fire(response.data.deleted ? 'Berhasil' : 'Gagal', response.data.message, response.data.deleted ? 'success' : 'error')
                        }, (error) => {
                            Swal.fire('Gagal', 'Terjadi Kesalahan Saat Menghubungi Server', 'error')
                        });
                    }
                });
            },
            loadConfig() {
                axios({
                    method: 'get',
                    url: window.location.href + "/config",
                }).then((response) => {
                    if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error');
                    this.mdl.guru.show = response.data.fieldGuru;
                    if (this.mdl.guru.show) this.loadGuru();
                }, (error) => {
                });
            },
            loadGuru() {
                axios({
                    method: 'get',
                    url: this.$store.state.config.base_url + `/panel/users/staff/all?role=guru`
                }).then((response) => {
                    if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error');
                    this.mdl.guru.data = response.data.data.map((v, i) => {
                        if (i == 0) {
                            this.mdl.guru.value = v.id;
                        }
                        return { value: v.id, text: v.fullname };
                    });
                }, (error) => {
                    Swal.fire('Gagal', 'Gagal Mengambil Data Guru', 'error')
                });
            },
            modal(e = 'show') {
                if (!this.mdl.instance) {
                    this.mdl.instance = new bootstrap.Modal(this.$refs.modal);
                }
                switch (e) {
                    case "hide":
                        this.mdl.instance.hide();
                        break;
                    case "show":
                        this.mdl.instance.show();
                        break;
                    case "toggle":
                        this.mdl.instance.toggle();
                        break;
                    case "dispose":
                        this.mdl.instance.dispose();
                        break;
                }
            },
            filterModal(e = 'show') {
                if (!this.filterMdl.instance) {
                    this.filterMdl.instance = new bootstrap.Modal(this.$refs.filterModal);
                }
                switch (e) {
                    case "hide":
                        this.filterMdl.instance.hide();
                        break;
                    case "show":
                        this.filterMdl.instance.show();
                        break;
                    case "toggle":
                        this.filterMdl.instance.toggle();
                        break;
                    case "dispose":
                        this.filterMdl.instance.dispose();
                        break;
                }
            },
            ondataload(data) {
                this.grid.data = data.data;
                this.grid.ready = true;
                return data.data;
            },
        },
    }
})();
