var VPInstallInit = (function () {
    let vt_template = `

    <div class="container">

    <div class="row g-0 auth-row">
        <div class="col-lg-6">
            <div class="auth-cover-wrapper bg-primary-100">
                <div class="auth-cover">
                    <div class="title text-center">
                        <h1 class="text-primary mb-10">Install Aplikasi</h1>
                        <p class="text-medium">
                            Langkah Terakhir
                        </p>
                    </div>
                    <div class="cover-image">
                        <img src="/vendors/plainadmin/assets/images/auth/signin-image.svg" alt="" />
                    </div>
                    <div class="shape-image">
                        <img src="/vendors/plainadmin/assets/images/auth/shape.svg" alt="" />
                    </div>
                </div>
            </div>
        </div>
        <!-- end col -->
        <div class="col-lg-6">
            <div class="signin-wrapper">
                <div class="form-wrapper">

                    <div class="tab-style-1 mb-30">
                        <nav class="nav" id="nav-tab">
                            <button class="active" id="tab-1-1" data-bs-toggle="tab" data-bs-target="#tabContent-1-1">
                                <small>Baru</small>
                            </button>
                            <button id="tab-1-2" data-bs-toggle="tab" data-bs-target="#tabContent-1-2" class="">
                                <small>Restore</small>
                            </button>
                        </nav>

                        <div class="tab-content" id="nav-tabContent1">
                            <div class="tab-pane fade active show" id="tabContent-1-1">
                                <div v-show="appinstalldata.errorMessage" class="alert-box danger-alert ">
                                    <div class="alert">
                                        <p class="text-medium">
                                            {{appinstalldata.errorMessage}}
                                        </p>
                                    </div>
                                </div>
                                <p class="text-sm mb-25">
                                </p>
                                <form @submit.prevent="appinstallfunc_dbinit">
                                    <div class="row">
                                        <div class="col-12">
                                            <div class="input-style-1">
                                                <label>Nama Aplikasi</label>
                                                <input required type="text" v-model="appinstalldata.appname" placeholder="Nama Aplikasi" />
                                            </div>
                                        </div>
                                        <!-- end col -->
                                        <div class="col-12">
                                            <div class="input-style-1">
                                                <label>Nama Akun Admin</label>
                                                <input required v-model="appinstalldata.fullname" type="text" placeholder="Nama Admin" />
                                            </div>
                                        </div>
                                        <!-- end col -->
                                        <div class="col-12">
                                            <div class="input-style-1">
                                                <label>Username Akun Admin</label>
                                                <input required v-model="appinstalldata.username" type="text" placeholder="Username Admin" />
                                            </div>
                                        </div>
                                        <!-- end col -->
                                        <div class="col-12">
                                            <div class="input-style-2">
                                                <label>Password Akun Admin</label>
                                                <input required v-model="appinstalldata.password" :type="appinstalldata.passoff?'text':'password'" placeholder="Password Admin" />
                                                <span style="cursor:pointer" @click="appinstalldata.passoff=!appinstalldata.passoff" class="icon"> <i :class="appinstalldata.passoff?'mdi-eye-outline':'mdi-eye-off-outline'" class="mdi"></i> </span>
                                            </div>
                                        </div>
                                        <!-- end col -->
                                        <div class="col-12">
                                            <div class="input-style-2">
                                                <label>Kode Lisensi *penting</label>
                                                <input required v-model="appinstalldata.licensekey" type="text" placeholder="Kode Lisensi" />
                                            </div>
                                        </div>
                                        <template v-if="requiredFields">
                                            <!-- end col -->
                                            <div class="col-12" v-if="requiredFields.indexOf('namasekolah')!=-1">
                                                <div class="input-style-2">
                                                    <label>Nama Sekolah *penting</label>
                                                    <input required v-model="appinstalldata.namasekolah" type="text" placeholder="Nama Sekolah" />
                                                </div>
                                            </div>
                                            <!-- end col -->
                                            <div class="col-12" v-if="requiredFields.indexOf('npsn')!=-1">
                                                <div class="input-style-2">
                                                    <label>NPSN *penting</label>
                                                    <input required v-model="appinstalldata.npsn" type="text" placeholder="NPSN" />
                                                </div>
                                            </div>
                                            <!-- end col -->
                                            <div class="col-12">
                                                <div class="input-style-2">
                                                    <label>Telegram ID</label>
                                                    <input v-model="appinstalldata.tid" type="text" placeholder="Telegram ID" />
                                                </div>
                                            </div>
                                        </template>
                                        <!-- end col -->
                                        <div class="col-12">
                                            <div class="button-group d-flex justify-content-center flex-wrap">
                                                <button :disabled="appinstallbtndisabled" :class="appinstallbtndisabled?'deactive-btn':'primary-btn'" class="main-btn btn-hover w-100 text-center">
                                                    <template v-if="appinstalldata.loading">
                                                        <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
                                                    </template>
                                                    <template v-else>
                                                        {{appinstallbtndisabled?'Isi Semua':'Berikutnya'}}
                                                    </template>
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                    <!-- end row -->
                                </form>
                            </div>
                            <div class="tab-pane fade " id="tabContent-1-2">
                                <div v-show="apprestore.errorMessage" class="alert-box danger-alert ">
                                    <div class="alert">
                                        <p class="text-medium">
                                            {{apprestore.errorMessage}}
                                        </p>
                                    </div>
                                </div>
                                <form @submit.prevent="restore">
                                    <p>Klik Tombol dibawah ini untuk merestore database </p>
                                    <input @change="fileready" name="dbfile" class="form-control" type="file" required>
                                    <br>
                                    <button v-show="apprestore.file" :disabled="apprestore.loading" type="submit" class="btn btn-sm btn-success rounded-md"><i class="lni lni-database"></i>Restore</button>

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

                    </div>
                </div>
            </div>
            <!-- end col -->
        </div>
        <!-- end row -->

    </div>
</div>
`;
    return {

        name: "install-init",
        template: vt_template,
        data() {
            return {

                appinstalldata: {
                    errorMessage: '',
                    passoff: true,
                    loading: false,
                    appname: 'APP NAME',
                    fullname: 'Administrator',
                    username: 'admin',
                    password: 'admin',
                    licensekey: ""
                },
                requiredFields: false,
                apprestore: {
                    form: "reform",
                    loading: false,
                    file: null,
                }
            }
        },
        mounted() {
            this.appinstalldata.password = makeid();
        },
        methods: {
            fileready(e) {
                var files = e.target.files || e.dataTransfer.files;
                if (!files.length) return this.apprestore.file = null;
                this.apprestore.file = files[0];
            },
            restore() {
                this.apprestore.errorMessage = '';
                this.apprestore.loading = true;
                const formData = new FormData();
                formData.append([this.$store.state.config.csrf.name], this.$store.state.config.csrf.value);
                formData.append('dbfile', this.apprestore.file);
                axios({
                    method: 'POST',
                    data: formData,
                    url: window.location.href + '/restore',
                }).then((response) => {
                    if (!isObject(response.data)) return this.apprestore.errorMessage = gaem({ response }, response.data);
                    window.location.href = this.$store.state.config.base_url + '/panel/dashboard';
                }, (error) => {
                    this.apprestore.errorMessage = gaem(error);
                    try {
                        if (typeof error.response.data.messages.csrf != 'undefined') this.$store.state.config.csrf.value = error.response.data.messages.csrf;
                    } catch (error) {

                    }
                }).finally(() => this.apprestore.loading = false);
            },
            appinstallfunc_dbinit() {
                this.appinstalldata.loading = true;
                this.appinstalldata.errorMessage = '';
                let data = Object.assign(this.appinstalldata, {
                    [this.$store.state.config.csrf.name]: this.$store.state.config.csrf.value
                });
                axios({
                    method: 'POST',
                    data: data,
                    url: window.location.href + '/new',
                }).then((response) => {
                    if (!isObject(response.data)) return this.appinstalldata.errorMessage = gaem({ response }, response.data);
                    window.location.href = this.$store.state.config.base_url + '/panel/dashboard';
                }, (error) => {
                    if (gaem(error, "Install Failed").search("migrations' doesn't exist in") != -1) {
                        this.appinstalldata.loading = false;
                        this.appinstallfunc_dbinit();
                        return;
                    }
                    this.appinstalldata.errorMessage = gaem(error);
                    try {
                        if (typeof error.response.data.messages.csrf != 'undefined') this.$store.state.config.csrf.value = error.response.data.messages.csrf;
                    } catch (error) {

                    }
                }).finally(() => this.appinstalldata.loading = false);
            }
        },
        computed: {
            appinstallfieldvalid() {
                return (this.appinstalldata.appname && this.appinstalldata.licensekey && this.appinstalldata.fullname && this.appinstalldata.username && this.appinstalldata.password);
            },
            appinstallbtndisabled() {
                return this.appinstalldata.loading || !this.appinstallfieldvalid;
            }
        }
    }
})();
