var VPSalinMateri = (function () {
    let vt_template = `
    <div class="modal fade" ref="modal"  tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-fullscreen modal-dialog-centered">
          <div class="modal-content card-style">
            <div class="modal-header px-0 border-0">
              <h5 class="text-bold">Salin Materi Pelajaran</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="row m-0">
                    <div class="col-md-6">
                        <div class="text-center my-5" v-if="existingMateri.loading">
                            <div class="spinner-grow text-primary" role="status"><span class="visually-hidden">Loading...</span></div>
                        </div>
                        <div v-else>
                            <small> Jumlah Materi : {{filExistM.filter(v=>v.show).length}}</small>
                            <div class="input-style-1 mb-1"><input type="text" v-model="existingMateri.query" placeholder="Cari Materi"></div>
                            <div title="Hapus Semua" @click="this.existingMateri.data=[]" class="border border-light shadow-sm rounded-top p-2 text-center" style="cursor: pointer;"><i class="mdi mdi-trash-can"></i></div>
                            <div class="siswa-list overflow-auto">
                                <template v-for="(fem,i) in filExistM">
                                    <div v-if="fem.show" :title="'Hapus '+fem.judul" @click="this.existingMateri.data = this.existingMateri.data.filter((v, index) => i!=index);" class="border border-light shadow-sm rounded px-1 siswa-item" style="cursor: pointer;">{{fem.parsedName}}</div>
                                </template>
                            </div>
                        </div>
                        
                    </div>
                    <div class="col-md-6">
                        <div class="text-center my-5" v-if="relatedMateri.loading">
                            <div class="spinner-grow text-primary" role="status"><span class="visually-hidden">Loading...</span></div>
                        </div>
                        <div v-else>
                            <small> Jumlah Materi : 0</small>
                            <div class="input-style-1 mb-1"><input type="text" v-model="relatedMateri.query" placeholder="Cari Materi"></div>
                            <div title="Tambah Semua" @click="addAllRel" class="border border-light shadow-sm rounded-top p-2 text-center" style="cursor: pointer;"><i class="mdi mdi-arrow-up-bold"></i><i class="mdi mdi-arrow-up-bold"></i></div>
                            <div class="siswa-list overflow-auto">
                                <template v-for="(frm,i) in filRelM">
                                    <div v-if="frm.exists" title="Materi Sudah Ada" class="text-white bg-primary border border-light shadow-sm rounded px-1 siswa-item" style="cursor: pointer;">{{frm.parsedName}}</div>
                                    <div v-else :title="'Tambah '+frm.judul" @click="existingMateri.data.push(relatedMateri.data[i]);" class="border border-light shadow-sm rounded px-1 siswa-item" style="cursor: pointer;">{{frm.parsedName}}</div>
                                </template>
                            </div>
                        </div>
                    </div>
                </div>

              </div>
            </div>
            <div class="modal-footer px-0">

                <div class="action d-flex flex-wrap justify-content-end">
                    &nbsp;
                <button type="submit" @click="save" :disabled="!ready" class="main-btn btn-sm primary-btn btn-hover">
                    <template v-if="!ready"> <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
                    </template>
                    <template v-else>
                        Simpan
                    </template>
                </button>
                </div>
            </div>
          </div>
        </div>
    </div>
`;

    return {
        name: "salin-materi",
        props: ['jadwalId', 'hscrf'],
        template: vt_template,
        data() {
            return {
                relatedMateri: {
                    query: '',
                    loading: false,
                    data: [],
                },
                existingMateri: {
                    query: '',
                    loading: false,
                    data: [],
                },

                mdl: {
                    loading: false,
                    instance: null,
                }
            }
        },
        mounted() {
        },
        computed: {
            filExistM() {
                let data = this.existingMateri.data.map(m => {
                    m.parsedName = `${m.judul} | ${m.kode_kompetensi == 'KP' ? 3 : 4}.${m.nomor_kompetensi} ${m.nama_kompetensi}`;
                    m.show = this.existingMateri.query == '' || m.parsedName.toLowerCase().indexOf(this.existingMateri.query) != -1;
                    return m;
                });
                return data;
            },
            filRelM() {
                let data = this.relatedMateri.data.map(m => {
                    m.parsedName = `${m.kode_mapel}  | ${m.nama_kelas} | ${m.judul} | ${m.kode_kompetensi == 'KP' ? 3 : 4}.${m.nomor_kompetensi} ${m.nama_kompetensi}`;
                    m.show = this.relatedMateri.query == '' || m.parsedName.toLowerCase().indexOf(this.relatedMateri.query) != -1;
                    m.exists = this.existingMateri.data.findIndex(v => v.id == m.id) != -1;
                    return m;
                });
                return data;
            },
            ready() {
                return !this.mdl.loading && !this.relatedMateri.loading && !this.existingMateri.loading
            }
        },
        methods: {
            start() {
                this.modal();
                this.loadRelatedMateri();
                this.loadExistingMateri();
            },
            addAllRel() {
                this.relatedMateri.data.forEach(el => {
                    if (this.existingMateri.data.findIndex(v => v.id == el.id) != -1) return;
                    this.existingMateri.data.push(el);
                });
            },
            save() {
                if (!this.ready) return;
                this.mdl.loading = true;
                let data = { [this.$store.state.config.csrf.name]: this.$store.state.config.csrf.value };
                data.cm = this.existingMateri.data.filter(v => !v.is_new).map(v => {
                    return v.id;
                });
                data.nm = this.existingMateri.data.filter(v => v.is_new).map(v => {
                    return v.id;
                });
                axios({
                    method: 'POST',
                    data,
                    url: `${this.$store.state.config.base_url}/panel/e-learning/jadwal-mengajar/materi/${this.jadwalId}/save`
                }).then((response) => {
                    this.$emit('updated', true);
                    this.loadExistingMateri();
                    this.loadRelatedMateri();
                }, (error) => {
                    Swal.fire('Gagal', gaem(error), 'error')
                }).finally(() => this.mdl.loading = false);
            },
            loadExistingMateri() {
                if (this.existingMateri.loading) return;
                this.existingMateri.loading = true;
                axios({
                    method: 'get',
                    url: `${this.$store.state.config.base_url}/panel/e-learning/jadwal-mengajar/materi/${this.jadwalId}/all`
                }).then((response) => {
                    if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error');
                    this.$store.state.config.csrf = this.$store.state.config.csrf;
                    this.existingMateri.data = response.data.materi;
                }, (error) => {
                    Swal.fire('Gagal', gaem(error), 'error')
                }).finally(() => this.existingMateri.loading = false);
            },
            loadRelatedMateri() {
                axios({
                    method: 'get',
                    url: `${window.location.href}/related`
                }).then((response) => {
                    if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error');

                    this.$store.state.config.csrf = this.$store.state.config.csrf;
                    this.relatedMateri.data = response.data.materi.map(v => {
                        v.is_new = true;
                        return v;
                    });
                }, (error) => {
                    Swal.fire('Gagal', gaem(error), 'error')
                }).finally(() => this.relatedMateri.loading = false);

            },
            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;
                }
            },
        }
    }
})();
