<template>
    <div>
        <v-layout class="mb-2" column>
            <v-dialog v-model="dialogconfirmationdelete" persistent max-width="290">
                <v-card>
                    <v-card-title dark class="headline error pt-2 pb-2" primary-title style="color:white">
                        <h4 dark>Konfirmasi</h4>
                    </v-card-title>
                    <v-card-text>
                        {{msgconfirmationdelete}}
                    </v-card-text>
                    <v-card-actions>
                        <v-spacer></v-spacer>
                        <v-btn small color="error darken-1 text-sm-left" flat @click="doDeleteData()">Hapus</v-btn>
                        <v-btn small color="primary darken-1 text-sm-right" flat @click="dialogconfirmationdelete = false">Batal</v-btn>
                    </v-card-actions>
                </v-card>
            </v-dialog>
            <v-card>
                <v-layout row>
                    <v-flex xs12>
                        <v-subheader red--text text--lighten-1> DETAIL METODE
                            <v-flex text-md-right>
                                <v-btn v-if="xact === 'new'" @click="saveNewMethode()" small color="info">Simpan</v-btn>
                                <v-btn v-if="xact === 'edit'" @click="deleteData()" small color="error">Hapus</v-btn>
                                <v-btn v-if="xact === 'edit'" @click="saveData()" small color="primary">Simpan</v-btn>
                            </v-flex>
                        </v-subheader>
                        <v-divider></v-divider>
                        <v-layout wrap>
                            <v-flex xs12 pa-2>
                                <v-layout row>
                                    <v-flex xs12 pa-1>
                                        <v-text-field class="pr-1" v-model="xcode" @change="checkCodeExist()" label="Kode*"></v-text-field>
                                        <p style="margin-top: -14px;margin-right:2px;color:#fff" v-if="checkError('requirecode')" class="error pl-2 pr-2" style="color:#fff">Kode harus diisi ya</p>
                                        <p style="margin-top: -14px;margin-right:2px;color:#fff" v-if="checkError('requirecodeexist')" class="error pl-2 pr-2" style="color:#fff">Sudah dipake dong</p>
                                    </v-flex>
                                </v-layout>
                                <v-layout row>
                                    <v-flex xs12 pa-1>
                                        <v-text-field class="pr-1" v-model="xmethodename" label="Nama*"></v-text-field>
                                        <p style="margin-top: -14px;margin-right:2px;color:#fff" v-if="checkError('requirename')" class="error pl-2 pr-2" style="color:#fff">Kalo gak ada namanya, bingung dong</p>
                                    </v-flex>
                                </v-layout>
                                <v-layout row>
                                    <v-flex xs12 pa-1>
                                        <v-text-field class="pr-1" v-model="xmethodenameresult" label="Nama Tercetak di Hasil*"></v-text-field>
                                        <p style="margin-top: -14px;margin-right:2px;color:#fff" v-if="checkError('requirenameresult')" class="error pl-2 pr-2" style="color:#fff">Kalo gak ada namanya, bingung dong</p>
                                    </v-flex>
                                </v-layout>
                                <v-layout row>
                                    <v-flex xs12 pa-1>
                                        <v-text-field class="pr-1" v-model="xpriority" label="Prioritas"></v-text-field>
                                    </v-flex>
                                </v-layout>
                            </v-flex>
                        </v-layout>
                    </v-flex>
                    </v-flex>
            </v-card>
            </v-layout>


            <template>

                <v-dialog v-model="dialogerrormsg" max-width="30%">
                    <v-card>
                        <v-card-title class="headline red en-1 pt-2 pb-2" primary-title>
                            <h4 style="color:#FFEBEE">Peringatan !</h4>
                        </v-card-title>
                        <v-card-text class="pt-2 pb-2">
                            <v-layout row>
                                <v-flex xs12 d-flex>
                                    <v-layout row>
                                        <v-flex pb-1 xs12>
                                            <v-layout row>
                                                <v-flex pt-2 pr-2 v-html="errormsg" xs12>

                                                </v-flex>
                                            </v-layout>
                                        </v-flex>
                                    </v-layout>
                                </v-flex>
                            </v-layout>
                        </v-card-text>
                        <v-divider></v-divider>
                        <v-card-actions>
                            <v-spacer></v-spacer>

                            <v-btn color="error" flat @click="dialogerrormsg = false">
                                Tutup
                            </v-btn>
                        </v-card-actions>
                    </v-card>
                </v-dialog>

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

<style scoped>
    table,
    td,
    th {
        border: 1px solid #ddd;
        text-align: left;
    }

    table {
        border-collapse: collapse;
        width: 100%;
    }

    th,
    td {
        padding-top: 5px;
        padding-bottom: 5px;
        padding-left: 8px;
        padding-right: 5px;
    }

    .mini-input .v-input {
        margin-top: 0px;
    }

    .mini-input .v-input,
    .mini-input .v-input--selection-controls,
    .mini-input .v-input__slot {
        margin-top: 0px;
        margin-bottom: 0px;
        margin-left: 3px;
    }

    .mini-input .v-messages {
        min-height: 0px;
    }

    input.fhm-input {
        border: 1px solid black;
        border-radius: 2px;
        -webkit-box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1),
        0 0 4px rgba(0, 0, 0, 0.1);
        -moz-box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1),
        0 0 4px rgba(0, 0, 0, 0.1);
        box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.1),
        0 0 4px rgba(0, 0, 0, 0.1);
        padding: 2px 4px;
        background: rgba(255, 255, 255, 0.5);
        margin: 0 0 1px 0;
        width: 30px;
        text-align: center;
    }
</style>

<script>
    module.exports = {
        data: () => ({
            dialogerrormsg: false,
            errormsg: '',
            search_sample: '',
            startdate: '',
            enddate: '',
            menustartdate: false,
            menuenddate: false,
            date: new Date().toISOString().substr(0, 10)
        }),
        computed: {
            dialogconfirmationdelete: {
                get() {
                    return this.$store.state.methode.dialog_confirmation_delete
                },
                set(val) {
                    this.$store.commit("methode/update_dialog_confirmation_delete", val)
                }
            },
            msgconfirmationdelete() {
                return this.$store.state.methode.msg_confirmation_delete
            },
            isLoading() {
                return this.$store.state.methode.search_status == 1
            },
            xact() {
                return this.$store.state.methode.act
            },
            detail() {
                return this.$store.state.methode.selected_methode
            },

            xcode: {
                get() {
                    return this.$store.state.methode.code
                },
                set(val) {
                    this.$store.commit("methode/update_code", val)
                    this.$store.commit("methode/update_no_save", 1)
                }
            },
            xmethodename: {
                get() {
                    return this.$store.state.methode.methode_name
                },
                set(val) {
                    this.$store.commit("methode/update_methode_name", val)
                    this.$store.commit("methode/update_no_save", 1)
                }
            },
            xmethodenameresult: {
                get() {
                    return this.$store.state.methode.methode_nameresult
                },
                set(val) {
                    this.$store.commit("methode/update_methode_nameresult", val)
                    this.$store.commit("methode/update_no_save", 1)
                }
            },
            xpriority: {
                get() {
                    return this.$store.state.methode.priority
                },
                set(val) {
                    this.$store.commit("methode/update_priority", val)
                    this.$store.commit("methode/update_no_save", 1)
                }
            },
            startComputedDateFormatted() {
                return this.formatDate(this.xstartdate)
            },
            xstartdate: {
                get() {
                    return this.$store.state.methode.startdate
                },
                set(val) {
                    this.$store.commit("methode/update_startdate", val)
                }
            },
            endComputedDateFormatted() {
                return this.formatDate(this.xenddate)
            },
            xenddate: {
                get() {
                    return this.$store.state.methode.enddate
                },
                set(val) {
                    this.$store.commit("methode/update_enddate", val)
                }
            },
        },
        methods: {
            checkError(value) {
                var errors = this.$store.state.methode.errors
                if (errors.includes(value)) {
                    return true
                } else {
                    return false
                }
            },
            checkCodeExist() {
                this.$store.dispatch("methode/checkcodeexist", {
                    code: this.xcode
                })
            },
            saveData() {
                var prm = {}
                prm.Nat_MethodeID = this.$store.state.methode.selected_methode.Nat_MethodeID
                prm.Nat_MethodeCode = this.xcode
                prm.Nat_MethodeName = this.xmethodename
                prm.Nat_MethodeNameInResult = this.xmethodenameresult
                prm.Nat_MethodePriority = this.xpriority
                this.$store.dispatch("methode/save", prm)

            },
            saveNewMethode() {
                var error_count = 0
                var error_msg = ''
                if (this.xcode === "") {
                    error_count++
                    error_msg +=
                        "<p><kbd class='text-uppercase'>Kode</kbd> : Diisi dong kodenya</p>"
                }
                if (this.$store.state.methode.code_exist === 'Y' && this.$store.state.methode.act === 'new'){
                    error_count++
                    error_msg +=
                        "<p><kbd class='text-uppercase'>Kode</kbd> : Kodenya sudah digunakan, ganti ya</p>"
                }
                if (this.xmethodename === "") {
                    error_count++
                    error_msg +=
                        "<p><kbd class='text-uppercase'>Nama Methode</kbd> : Diisi dong nama metodenya</p>"
                }
                if (this.xmethodenameresult === "") {
                    error_count++
                    error_msg +=
                        "<p><kbd class='text-uppercase'>Nama Metode di Hasil</kbd> : Diisi dong nama metode di hasilnya</p>"
                }
                if (error_count > 0) {
                    this.errormsg = error_msg
                    this.dialogerrormsg = true
                } else {
                    var prm = {}
                    prm.Nat_MethodeID = 0
                    prm.Nat_MethodeCode = this.xcode
                    prm.Nat_MethodeName = this.xmethodename
                    prm.Nat_MethodeNameInResult = this.xmethodenameresult
                    prm.Nat_MethodePriority = this.xpriority
                    this.$store.dispatch("methode/newmethode", prm)
                }
            },
            thr_search_sample: _.debounce(function () {
                this.$store.dispatch("methode/searchsample", this.search_sample)
            }, 2000),
            formatDate(date) {
                if (!date) return null

                const [year, month, day] = date.split('-')
                return `${day}-${month}-${year}`
            },
            deFormatedDate(date) {
                if (!date) return null

                const [day, month, year] = date.split('-')
                return `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`
            },
            deleteData() {
                let msg = "Yakin, akan menghapus data metode " + this.xmethodename + " ?"
                this.$store.commit("methode/update_msg_confirmation_delete", msg)
                this.$store.commit("methode/update_dialog_confirmation_delete", true)
            },
            doDeleteData() {
                var prm = {}
                prm.Nat_MethodeID = this.$store.state.methode.selected_methode.Nat_MethodeID
                prm.Nat_MethodeName = this.xmethodename
                this.$store.dispatch("methode/delete", prm)
            }
        },
        watch: {
            search_sample(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.methode.update_autocomplete_status == 1) return
                this.thr_search_sample()
            }
        }
    }
</script>
