var VPBackupRestore = (function () {
    let vt_template = `
    <div class="card p-4">
        <div class="row">
            <div class="col-md-6">
                <h5>Cadangkan Data</h5>
                <p>Klik Tombol dibawah ini untuk membackup database </p>
                <a :href="this.$store.state.config.base_url+'/panel/app/settings/backup-restore/backup?db=true'" target="_blank" class="btn btn-sm btn-success rounded-md"><i class="lni lni-database"></i> Database</a>
                &nbsp;
                <a :href="this.$store.state.config.base_url+'/panel/app/settings/backup-restore/backup?assets=true'" target="_blank" class="btn btn-sm btn-success rounded-md"><i class="lni lni-folder"></i> Assets</a>
                &nbsp;
                <a :href="this.$store.state.config.base_url+'/panel/app/settings/backup-restore/backup?assets=true&db=true'" target="_blank" class="btn btn-sm btn-success rounded-md"><i class="lni lni-database"></i> Database & Asset</a>
                <br> 
                <br> 
                <a :href="this.$store.state.config.base_url+'/panel/app/settings/backup-restore/backup?db=true&limited-memory=true'" target="_blank" class="btn btn-sm btn-success rounded-md"><i class="lni lni-database"></i> Database (Lambat)</a>
                &nbsp;
                <a :href="this.$store.state.config.base_url+'/panel/app/settings/backup-restore/backup?assets=true&db=true&limited-memory=true'" target="_blank" class="btn btn-sm btn-success rounded-md"><i class="lni lni-database"></i> Database & Asset (Lambat)</a>
                <br><br>
                <h5>Pulihkan Data</h5>
                <form @submit.prevent>
                    <p>Klik Tombol dibawah ini untuk merestore database </p>
                    <input @change="rfile" class="form-control" type="file" required>
                    <button @click="restore()" :disabled="!restoreFile" type="submit" class="btn btn-sm btn-success rounded-md mt-2"><i class="lni lni-database"></i>Pulihkan</button>
                    &nbsp;
                    <button @click="restore(true)" :disabled="!restoreFile" type="submit" class="btn btn-sm btn-success rounded-md mt-2"><i class="lni lni-database"></i>Pulihkan (Lambat)</button>
                </form>
               
                <button @click="reset" class="btn btn-sm btn-danger rounded-md mt-2 mb-2"><i class="lni lni-reload"></i> Setelan Awal</button>
            </div>
            <div class="col-md-6">
            <h5><i class="lni lni-database"></i> Hapus Data Master</h5>
          
                <div class="form-check checkbox-style mx-1">
                    <input class="form-check-input" type="checkbox" v-model="dataMaster.siswa" id="md-siswa">
                    <label class="form-check-label" for="md-siswa">Siswa</label>
                </div>
                <div class="form-check checkbox-style mx-1">
                    <input class="form-check-input" type="checkbox" v-model="dataMaster.mapel" id="md-mapel">
                    <label class="form-check-label" for="md-mapel">Mata Pelajaran</label>
                </div>
                <div class="form-check checkbox-style mx-1">
                    <input class="form-check-input" type="checkbox" v-model="dataMaster.kelas" id="md-kelas">
                    <label class="form-check-label" for="md-kelas">Kelas</label>
                </div>
                <div class="form-check checkbox-style mx-1">
                    <input class="form-check-input" type="checkbox" v-model="dataMaster.tingkat" id="md-tingkat">
                    <label class="form-check-label" for="md-tingkat">Tingkat</label>
                </div>
                <div class="form-check checkbox-style mx-1">
                    <input class="form-check-input" type="checkbox" v-model="dataMaster.jurusan" id="md-jurusan">
                    <label class="form-check-label" for="md-jurusan">Jurusan</label>
                </div>
                <div class="form-check checkbox-style mx-1">
                    <input class="form-check-input" type="checkbox" v-model="dataMaster.ruang" id="md-ruang">
                    <label class="form-check-label" for="md-ruang">Ruang</label>
                </div>
                <div class="form-check checkbox-style mx-1">
                    <input class="form-check-input" type="checkbox" v-model="dataMaster.jenis_ujian" id="md-jenis_ujian">
                    <label class="form-check-label" for="md-jenis_ujian">Jenis Ujian</label>
                </div>
                <div v-if="this.dataMasterReady">
                    <span class="text-danger">Tindakan ini akan menghapus data:</span>
                    <span v-if="dataMaster.siswa" >Siswa termasuk Hasil Ujian, Presensi Siswa, Tugas Siswa.</span>
                    <span v-if="dataMaster.mapel" >Mata Pelajaran termasuk Kompetensi Dasar,RPP,Materi,Jadwal Pelajaran,Draft Ujian, Jadwal Ujian, Berita Acara.</span>
                    <span v-if="dataMaster.kelas" >Kelas termasuk Jadwal Pelajaran, Tugas Siswa.</span>
                    <span v-if="dataMaster.tingkat" >Tingkat termasuk Kompetensi Dasar,RPP,Materi,Tugas Siswa.</span>
                    <span v-if="dataMaster.ruang" >Ruang Ujian termasuk Jadwal Ujian, Hasil Ujian, Berita Acara.</span>
                    <span v-if="dataMaster.jenis_ujian" >Jenis Ujian termasuk Jadwal Ujian, Hasil Ujian, Berita Acara.</span>
                </div>
                <button :disabled="!this.dataMasterReady" @click="deleteDM" class="btn btn-sm btn-danger rounded-md mt-2 "><i class="lni lni-database"></i>Hapus</button>               
            </div>
        </div>   
    </div>


    `;

    return {
        name: "backup-restore",
        template: vt_template,

        data() {
            return {

                errMsg: '',
                restoreFile: null,
                patchFile: null,
                dataMaster: {
                    siswa: false,
                    mapel: false,
                    kelas: false,
                    tingkat: false,
                    jurusan: false,
                    ruang: false,
                    jenis_ujian: false
                },
            }
        },
        mounted() {
        },
        methods: {
            rfile(e) {
                let [file] = e.target.files;
                if (!file) return this.restoreFile = null;
                this.restoreFile = file;
            },
            deleteDM() {
                if (!this.dataMasterReady) return false;
                let data = {
                    [this.$store.state.config.csrf.name]: this.$store.state.config.csrf.value,
                    siswa: this.dataMaster.siswa,
                    mapel: this.dataMaster.mapel,
                    kelas: this.dataMaster.kelas,
                    tingkat: this.dataMaster.tingkat,
                    jurusan: this.dataMaster.jurusan,
                    ruang: this.dataMaster.ruang,
                    jenis_ujian: this.dataMaster.jenis_ujian,
                }
                SwalDelete({
                    params: {
                        title: 'Hapus Data',
                        text: 'Hapus data akan menghapus semua data pilihan yang ada di basis data, apakah anda yakin?',
                        type: 'warning',
                        showCancelButton: true,
                        confirmButtonColor: '#3085d6',
                        cancelButtonColor: '#d33',
                        confirmButtonText: 'Ya, Hapus!'
                    },
                    confirmed: () => {
                        showDismisableLoading();
                        let url = this.$store.state.config.base_url + "/panel/app/settings/backup-restore/data-master";
                        axios({
                            method: 'DELETE',
                            data,
                            url,
                        }).then((response) => {
                            if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error');
                            Swal.close();
                            Swal.fire('Berhasil', response.data.message || "Berhasil Hapus Data Master", 'success')
                        }, (error) => {
                            Swal.fire('Gagal', gaem(error), 'error')
                        });
                    }
                });
            },
            reset() {
                SwalDelete({
                    params: {
                        title: 'Reset Data',
                        text: 'Reset data akan menghapus semua data yang ada di database, apakah anda yakin?',
                        type: 'warning',
                        showCancelButton: true,
                        confirmButtonColor: '#3085d6',
                        cancelButtonColor: '#d33',
                        confirmButtonText: 'Ya, Reset!'
                    },
                    confirmed: () => {
                        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/app/settings/backup-restore/reset",
                        }).then((response) => {
                            if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error');
                            Swal.close();
                            Swal.fire('Berhasil', response.data.message || "Berhasil Mengembalikan Setelan Default", 'success')
                        }, (error) => {
                            Swal.fire('Gagal', gaem(error), 'error')
                        });
                    }
                });
            },
            restore(limitedMemory = false) {
                if (!this.restoreFile) return;
                const formData = new FormData();
                formData.append([this.$store.state.config.csrf.name], this.$store.state.config.csrf.value);
                formData.append('file', this.restoreFile);

                SwalDelete({
                    params: {
                        title: 'Pulihkan Data',
                        text: 'Pastikan cadangkan data terlebih dahulu agar menghindari kesalahan!',
                        type: 'warning',
                        showCancelButton: true,
                        confirmButtonColor: '#3085d6',
                        cancelButtonColor: '#d33',
                        confirmButtonText: 'Pulihkan!'
                    },
                    confirmed: () => {
                        showDismisableLoading();
                        let url = `${this.$store.state.config.base_url}/panel/app/settings/backup-restore/restore`;
                        if (limitedMemory) url += '?limited-memory=true';
                        axios({
                            method: 'POST',
                            data: formData,
                            url: url,
                            responseType: 'json'
                        }).then((response) => {
                            if (!isObject(response.data)) return Swal.fire('Error', gaem({ response }, response.data), 'error')
                            Swal.fire('Success', response.data.message, 'success')
                        }, (error) => {
                            if (typeof error.response == 'undefined') return;
                            Swal.fire('Error', gaem(error), 'error')
                        });
                    }
                });

            },
        },
        computed: {
            dataMasterReady() {
                return this.dataMaster.siswa || this.dataMaster.mapel || this.dataMaster.kelas || this.dataMaster.tingkat || this.dataMaster.jurusan || this.dataMaster.ruang || this.dataMaster.jenis_ujian
            }
        }
    }
})();
