var VPBlockedApps = (function () {
    let vp_template = `
    <div class="card-style">
          <div style="text-align:right">
            <button title="Filter" class="main-btn btn-sm primary-btn rounded-md btn-hover" data-bs-toggle="modal" data-bs-target="#modal-filter">
                <i class="fas fa-filter"></i>
            </button>
            &nbsp;
            <button  class="main-btn btn-sm primary-btn rounded-md btn-hover" data-bs-toggle="modal" data-bs-target="#modal-common">
                <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="parsedURL" :ref="grid.ref" :columns="grid.columns" :ondataload="ondataload"></grid>
          </div>
      </div>



      <div class="modal fade" id="modal-filter" :ref="mdl.filter.ref" 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>Status</label>
                        <div class="select-position">
                        <select required v-model="mdl.filter.status">
                            <option value="all">Semua</option>
                            <option value="1">Aktif</option>
                            <option value="0">Tidak Aktif</option>
                        </select>
                        </div>
                    </div>

                  </div>
              </div>
          </div>
      </div>
  </div>

  <div class="modal fade" id="modal-common" :ref="mdl.common.ref" 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.common.id?'Ubah':'Tambah'}}</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.common.id?ubah():tambah()">
                        <div class="input-style-1">
                            <label>Nama Aplikasi</label>
                            <input v-model="mdl.common.name" type="text" class="form-control" placeholder="Nama Aplikasi">
                        </div>
                        <div class="input-style-1">
                            <label>Nama Paket Aplikasi</label>
                            <input required v-model="mdl.common.package_name" type="text" class="form-control" placeholder="Contoh com.candycbt.app">
                        </div>
                            <div class="row">
                                <div class="col-6">
                                    <div class="select-style-1">
                                        <label>Status</label>
                                        <div class="select-position">
                                        <select required v-model="mdl.common.status">
                                            <option value="1">Blokir</option>
                                            <option value="0">Tidak Diblokir</option>
                                        </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="modal-footer px-0">
                                <button type="submit" class="main-btn btn-sm success-btn btn-hover">
                                    <template v-if="mdl.common.id"> 
                                        Simpan
                                    </template>
                                    <template v-else>
                                        Tambah
                                    </template>
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
  
`
    let gridConf = {
        ready: false,
        data: [],
        id: "apps-table",
        ref: "apps-table",
        columns: [

            {
                id: 'name',
                name: "Nama Aplikasi"
            }, {
                id: 'package_name',
                name: "Nama Paket Aplikasi"
            },
            {
                id: 'status',
                name: 'Status',
                formatter: (_, row) => {
                    let state = row.cells[2].data == '1';
                    let k = "<div class='d-flex'>";
                    k += `&nbsp;<span class="badge bg-${state ? 'success' : 'danger'}">${state ? 'Diblokir' : 'Tidak Diblokir'}</span>`;
                    k += '</div>';
                    return gridjs.html(k);
                }
            },
            {
                id: 'id',
                name: 'Aksi',
                formatter: (_, row) => {
                    let btn = "";
                    btn += "<div class='d-flex'>";
                    btn += `<button class="btn btn-primary rounded-md" id="trigger-edit" data-id="${row.cells[1].data}"><i id="trigger-edit" data-id="${row.cells[1].data}" class='lni lni-pencil-alt'></i></button>`;
                    btn += `&nbsp;<button class="btn btn-danger rounded-md" id="trigger-delete" data-id="${row.cells[1].data}"><i id="trigger-delete" data-id="${row.cells[1].data}" class='lni lni-trash-can'></i></button>`;
                    btn += "</div>";
                    return gridjs.html(btn);
                }
            },
        ]
    };

    return {
        name: "blocked-apps",
        template: vp_template,

        data() {
            return {
                grid: gridConf,
                mdl: {
                    filter: {
                        ref: "filter-modal",
                        status: 'all',
                        is_default: 'all',
                        is_connected: 'all',
                    },
                    common: {
                        id: null,
                        name: '',
                        ref: "common-modal",
                        package_name: '',
                        status: '1',
                    }
                }
            }
        },
        mounted() {
            this.eventListener();
        },
        methods: {
            eventListener() {
                document.addEventListener('click', (e) => {
                    if (e.target && e.target.id == 'trigger-edit') {
                        this.rincian(e.target.dataset.id)
                    }
                    if (e.target && e.target.id == 'trigger-delete') {
                        this.hapus(e.target.dataset.id)
                    }
                });
                this.$refs[this.mdl.common.ref].addEventListener('hide.bs.modal', (e) => {
                    this.mdl.common.id = null
                })
            },
            ubah() {
                let data = {
                    [this.$store.state.config.csrf.name]: this.$store.state.config.csrf.value,
                    status: this.mdl.common.status,
                    name: this.mdl.common.name,
                    package_name: this.mdl.common.package_name,
                };
                showDismisableLoading();
                axios({
                    method: 'PUT',
                    data: data,
                    url: this.$store.state.config.base_url + "/panel/exambro/blocked-apps/edit/" + this.mdl.common.id,
                }).then((response) => {
                    Swal.close()
                    if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error');
                    Swal.fire('Berhasil', response.data.message, 'success');
                    this.$refs[this.grid.ref].reload()
                    this.CommonModal('hide');
                }, (error) => {
                    Swal.close()
                    if (typeof error.response == 'undefined') return;
                    let errMsg = gaem(error);
                    return Swal.fire('Gagal', errMsg, 'error')
                });
            },
            rincian(id) {
                showDismisableLoading();
                axios({
                    method: 'GET',
                    url: this.$store.state.config.base_url + "/panel/exambro/blocked-apps/detail/" + id,
                }).then((response) => {
                    Swal.close()
                    if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error');
                    this.mdl.common.id = response.data.package_name;
                    this.mdl.common.package_name = response.data.package_name;
                    this.mdl.common.name = response.data.name;
                    this.mdl.common.status = response.data.status;
                    this.CommonModal('show');
                }, (error) => {
                    this.mdl.common.id = null;
                    Swal.close()
                    if (typeof error.response == 'undefined') return;
                    let errMsg = gaem(error);
                    return Swal.fire('Gagal', errMsg, 'error')
                });
            },
            hapus(id) {
                showDismisableLoading();
                axios({
                    method: 'DELETE',
                    data: { [this.$store.state.config.csrf.name]: this.$store.state.config.csrf.value, },
                    url: this.$store.state.config.base_url + "/panel/exambro/blocked-apps/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()
                    return Swal.fire('Success', response.data.message, 'success');
                }, (error) => {
                    Swal.close()
                    if (typeof error.response == 'undefined') return;
                    let errMsg = gaem(error);
                    return Swal.fire('Gagal', errMsg, 'error')
                });
            },
            tambah() {
                let data = {
                    [this.$store.state.config.csrf.name]: this.$store.state.config.csrf.value,
                    name: this.mdl.common.name,
                    package_name: this.mdl.common.package_name,
                    status: this.mdl.common.status,
                };
                showDismisableLoading();
                axios({
                    method: 'POST',
                    data: data,
                    url: this.$store.state.config.base_url + "/panel/exambro/blocked-apps/add",
                }).then((response) => {
                    Swal.close()
                    if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error');
                    Swal.fire('Berhasil', response.data.message, 'success');
                    this.$refs[this.grid.ref].reload()
                    this.CommonModal('hide');
                }, (error) => {
                    Swal.close()
                    if (typeof error.response == 'undefined') return;
                    let errMsg = gaem(error);
                    return Swal.fire('Gagal', errMsg, 'error')
                });
            },
            ondataload(data) {
                this.grid.data = data.data;
                this.grid.ready = true;
                return data.data;
            },
            FilterModal(e = 'show') {
                if (!this.mdl.filter.instance) {
                    this.mdl.filter.instance = new bootstrap.Modal(this.$refs[this.mdl.filter.ref]);
                }
                switch (e) {
                    case "hide":
                        this.mdl.filter.instance.hide();
                        break;
                    case "show":
                        this.mdl.filter.instance.show();
                        break;
                    case "toggle":
                        this.mdl.filter.instance.toggle();
                        break;
                    case "dispose":
                        this.mdl.filter.instance.dispose();
                        break;
                }
            },
            CommonModal(e = 'show') {
                if (!this.mdl.common.instance) {
                    this.mdl.common.instance = new bootstrap.Modal(this.$refs[this.mdl.common.ref]);
                }
                switch (e) {
                    case "hide":
                        this.mdl.common.instance.hide();
                        break;
                    case "show":
                        this.mdl.common.instance.show();
                        break;
                    case "toggle":
                        this.mdl.common.instance.toggle();
                        break;
                    case "dispose":
                        this.mdl.common.instance.dispose();
                        break;
                }
            }
        },
        computed: {
            parsedURL() {
                let url = window.location.href + '/all?';
                url += `&status=${this.mdl.filter.status}`;
                return url;
            },
        }
    }
})();
