<template>
    <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 DOKTER
                        <v-flex text-md-right>
                            <v-btn v-if="xact === 'new'" @click="saveNewDoctor()" 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 row wrap>
                        <v-flex xs12 pa-2>
                            <v-layout row>
                                <v-flex xs1 pa-1>
                                    <v-text-field v-model="xpreffix" placeholder=""  label="Awalan 1"></v-text-field>
                                </v-flex>
                                <v-flex xs1 pa-1>
                                    <v-text-field v-model="xpreffix2" placeholder=""  label="Awalan 2"></v-text-field>
                                </v-flex>
                                <v-flex xs7 pa-1>
                                    <v-text-field v-model="xdoctorname" label="Nama Dokter*"></v-text-field>
                                    <p v-if="checkError('requirename')" class="error pl-2 pr-2" style="color:#fff">Nama harus diisi dong</p>
                                </v-flex>
                                <v-flex xs1 pa-1>
                                    <v-text-field v-model="xsuffix" placeholder="" label="Akhiran 1"></v-text-field>
                                </v-flex>
                                <v-flex xs1 pa-1>
                                    <v-text-field v-model="xsuffix2" placeholder=""  label="Akhiran 2"></v-text-field>
                                </v-flex>
                                <v-flex xs1 pa-1>
                                    <v-text-field v-model="xsuffix3" placeholder=""  label="Akhiran 3"></v-text-field>
                                </v-flex>
                            </v-layout>
                            
                            <v-layout row>
                                <v-flex xs3 pa-1>
                                    <v-select item-text="m_sexname" return-object :items="xsexes" v-model="xsex" label="Jenis Kelamin*"></v-select>
                                    <p v-if="checkError('requiresex')" class="error pl-2 pr-2" style="color:#fff">Jenis Kelamin harus diisi dong</p>
                                </v-flex>
                                <v-flex xs3 pa-1>
                                    <v-select item-text="M_ReligionName" return-object :items="xreligions" v-model="xreligion" label="Agama"></v-select>
                                    <p v-if="checkError('requirereligion')" class="error pl-2 pr-2" style="color:#fff">Agama harus diisi dong</p>
                                </v-flex>
                                <v-flex xs2 pa-1>
                                    <v-text-field
                                        label="Tanggal Lahir"
                                        v-model="xdob"
                                        mask="##-##-####"
                                        placeholder="DD-MM-YYYY"
                                        hint="format : DD-MM-YYYY , contoh : 22-06-1988"
                                    ></v-text-field>
                                    <!--<p v-if="checkError('requiredob')" class="error pl-2 pr-2" style="color:#fff">DOB </p>-->
                                </v-flex>
                                <v-flex xs5 pa-1>
                                    <v-select 
                                        item-text="name" 
                                        return-object 
                                        :items="xspecialistes" 
                                        v-model="selected_specialist" 
                                    label="Spesialis"></v-select>
                                    <!--<p v-if="checkError('requirereligion')" class="error pl-2 pr-2" style="color:#fff">Spesialis harus diisi dong</p>-->
                                </v-flex>
                                
                                
                                
                            </v-layout>
                            <v-layout  align-center row>
                                <v-flex xs2 pa-1>
                                    <v-text-field v-model="xphone" label="Telepon"></v-text-field>
                                </v-flex>
                                <v-flex xs3 pa-1>
                                    <v-text-field v-model="xhp" label="HP"></v-text-field>
                                </v-flex>
                                <v-flex xs5 pa-1>
                                    <v-layout row>
                                        <v-flex pt-2 text-xs-center xs2>
                                            <v-btn v-if="isdefaultemail === 'N' || isdefaultemail === ''" @click="changeBtnFlagDefaultEmail(isdefaultemail)" title="default email" small style="color:#fff;min-width:20px;padding-top:2px" color="red ligthen-3"><v-icon small>close</v-icon></v-btn>
                                            <v-btn v-if="isdefaultemail === 'Y'" @click="changeBtnFlagDefaultEmail(isdefaultemail)" title="default email" small style="color:#fff;min-width:20px;padding-top:2px" color="teal ligthen-3"><v-icon small>check</v-icon></v-btn>
                                            
                                        </v-flex>
                                        <v-flex xs10><v-text-field v-model="xemail" label="Email"></v-text-field></v-flex>
                                    </v-layout>
                                     
                                </v-flex>
                                <v-flex xs5 pa-1>
                                    <v-autocomplete label="Marketing" v-model="xstaff" :items="xstaffs" :search-input.sync="search_staff" auto-select-first no-filter
                                        item-text="Nat_StaffName" return-object :loading="isLoading" no-data-text="Pilih Marketing">
                                        <template slot="item" slot-scope="{ item }">
                                            <v-list-tile-content>
                                                <v-list-tile-title v-text="item.Nat_StaffNames"></v-list-tile-title>
                                            </v-list-tile-content>
                                        </template>
                                    </v-autocomplete>
                                </v-flex>
                            </v-layout>
                            
                            <v-layout align-center row>
                                <v-flex xs12 pa-1>
                                    <v-textarea rows="1" auto-grow v-model="xnote" label="Catatan"></v-textarea>
                                </v-flex>
                                
                            </v-layout>
                            
                
                        </v-flex>
                    </v-layout>
                </v-flex>
            </v-layout>
        </v-card>
    </v-layout>
</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: () => ({
            search_staff: ''
        }),
        computed: {
            dialogconfirmationdelete: {
                get() {
                    return this.$store.state.doctor.dialog_confirmation_delete
                },
                set(val) {
                    this.$store.commit("doctor/update_dialog_confirmation_delete", val)
                }
            },
            isLoading() {
                return this.$store.state.doctor.search_status == 1
            },
            msgconfirmationdelete() {
                return this.$store.state.doctor.msg_confirmation_delete
            },
            xact() {
                return this.$store.state.doctor.act
            },
            detail() {
                return this.$store.state.doctor.selected_doctor
            },
            xpreffix: {
                get() {
                    return this.$store.state.doctor.preffix
                },
                set(val) {
                    this.$store.commit("doctor/update_preffix", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },
            xpreffix2: {
                get() {
                    return this.$store.state.doctor.preffix2
                },
                set(val) {
                    this.$store.commit("doctor/update_preffix2", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },
            xdoctorname: {
                get() {
                    return this.$store.state.doctor.doctor_name
                },
                set(val) {
                    this.$store.commit("doctor/update_doctor_name", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },
            xsuffix: {
                get() {
                    return this.$store.state.doctor.suffix
                },
                set(val) {
                    this.$store.commit("doctor/update_suffix", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },

            xsuffix2: {
                get() {
                    return this.$store.state.doctor.suffix2
                },
                set(val) {
                    this.$store.commit("doctor/update_suffix2", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },

            xsuffix3: {
                get() {
                    return this.$store.state.doctor.suffix3
                },
                set(val) {
                    this.$store.commit("doctor/update_suffix3", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },
            xsexes() {
                return this.$store.state.doctor.sexes
            },
            xsex: {
                get() {
                    return this.$store.state.doctor.selected_sex
                },
                set(val) {
                    this.$store.commit("doctor/update_selected_sex", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },
            xreligions() {
                return this.$store.state.doctor.religions
            },
            xreligion: {
                get() {
                    return this.$store.state.doctor.selected_religion
                },
                set(val) {
                    this.$store.commit("doctor/update_selected_religion", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },

            xstaffs() {
                return this.$store.state.doctor.staffs
            },
            xstaff: {
                get() {
                    return this.$store.state.doctor.staff
                },
                set(val) {
                    this.$store.commit("doctor/update_staff", val)
                }
            },

            xphone: {
                get() {
                    return this.$store.state.doctor.phone
                },
                set(val) {
                    this.$store.commit("doctor/update_phone", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },
            xhp: {
                get() {
                    return this.$store.state.doctor.hp
                },
                set(val) {
                    this.$store.commit("doctor/update_hp", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },
            xemail: {
                get() {
                    return this.$store.state.doctor.email
                },
                set(val) {
                    this.$store.commit("doctor/update_email", val)
                    this.$store.commit("doctor/update_no_save", 1)
                    if(val != ''){
                        this.showdefaultemail = true
                    }
                    else{
                        this.showdefaultemail = false
                    }
                }
            },
            showdefaultemail: {
                get() {
                    return this.$store.state.doctor.showdefaultemail
                },
                set(val) {
                    this.$store.commit("doctor/update_showdefaultemail", val)
                    this.$store.commit("doctor/update_no_save", 1)
                    
                }
            },
            xnote: {
                get() {
                    return this.$store.state.doctor.note
                },
                set(val) {
                    this.$store.commit("doctor/update_note", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },
            xpj: {
                get() {
                    return this.$store.state.doctor.pj
                },
                set(val) {
                    this.$store.commit("doctor/update_pj", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },
            xpjmcu: {
                get() {
                    return this.$store.state.doctor.pjmcu
                },
                set(val) {
                    this.$store.commit("doctor/update_pjmcu", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },
            xdpj: {
                get() {
                    return this.$store.state.doctor.dpj
                },
                set(val) {
                    this.$store.commit("doctor/update_dpj", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },
            xclinic: {
                get() {
                    return this.$store.state.doctor.clinic
                },
                set(val) {
                    this.$store.commit("doctor/update_clinic", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },
            xmarketingconfirm: {
                get() {
                    return this.$store.state.doctor.marketing_confirm
                },
                set(val) {
                    this.$store.commit("doctor/update_marketing_confirm", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },
            xdefault: {
                get() {
                    return this.$store.state.doctor.is_default
                },
                set(val) {
                    this.$store.commit("doctor/update_is_default", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },
            isdefaultemail: {
                get() {
                    return this.$store.state.doctor.is_default_email
                },
                set(val) {
                    this.$store.commit("doctor/update_is_default_email", val)
                }
            },
            xdob: {
                get() {
                    return this.$store.state.doctor.dob
                },
                set(val) {
                    this.$store.commit("doctor/update_dob", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },
            selected_specialist: {
                get() {
                    return this.$store.state.doctor.selected_specialist
                },
                set(val) {
                    this.$store.commit("doctor/update_selected_specialist", val)
                    this.$store.commit("doctor/update_no_save", 1)
                }
            },
            xspecialistes() {
                return this.$store.state.doctor.specialistes
            },
        },
        methods: {
            checkError(value) {
                var errors = this.$store.state.doctor.errors
                if (errors.includes(value)) {
                    return true
                } else {
                    return false
                }
            },
            changeBtnFlagPJ(value) {
                var newval = (value === 'Y') ? 'N' : 'Y'
                this.xpj = newval
            },
            changeBtnFlagPJMCU(value) {
                var newval = (value === 'Y') ? 'N' : 'Y'
                this.xpjmcu = newval
            },
            changeBtnFlagDefaultEmail(value) {
                if (this.xemail !== '') {
                    var newval = (value === 'Y') ? 'N' : 'Y'
                    this.isdefaultemail = newval
                } else {
                    var msg = "si mail main ayunan, email kosong jangan dibiarkan"
                    this.$store.commit("doctor/update_msg_info", msg)
                    this.$store.commit("doctor/update_open_dialog_info", true)
                }
            },
            changeBtnFlagDefaultPJ(value) {
                var newval = (value === 'Y') ? 'N' : 'Y'
                this.xdpj = newval
            },
            changeBtnFlagDefault(value) {
                var newval = (value === 'Y') ? 'N' : 'Y'
                this.xdefault = newval
            },
            changeBtnFlagClinic(value) {
                var newval = (value === 'Y') ? 'N' : 'Y'
                this.xclinic = newval
            },
            changeBtnFlagMarketing(value) {
                var newval = (value === 'Y') ? 'N' : 'Y'
                this.xmarketingconfirm = newval
            },
            saveData() {
                //alert('dasdsad')
                this.$store.commit("doctor/update_errors", [])
                var errors = this.$store.state.doctor.errors
                if (this.xdoctorname === '') {
                    errors.push("requirename")
                }
                if (_.isEmpty(this.xsex)) {
                    errors.push("requiresex")
                }
                var xDate = moment(this.$store.state.doctor.M_DoctorDOB, 'DD-MM-YYYY', true)
                var isValidDate = xDate.isValid()

                if (errors.length === 0) {
                    var prm = {}
                    prm.M_DoctorID = this.$store.state.doctor.selected_doctor.M_DoctorID
                    prm.M_DoctorCode = this.$store.state.doctor.selected_doctor.M_DoctorCode
                    prm.M_DoctorPrefix = this.xpreffix
                    prm.M_DoctorPrefix2 = this.xpreffix2
                    prm.M_DoctorName = this.xdoctorname
                    prm.M_DoctorSufix = this.xsuffix
                    prm.M_DoctorSufix2 = this.xsuffix2
                    prm.M_DoctorSufix3 = this.xsuffix3
                    prm.M_DoctorM_SexID = this.$store.state.doctor.selected_sex.M_SexID
                    prm.M_DoctorM_ReligionID = this.$store.state.doctor.selected_religion.M_ReligionID
                    prm.M_DoctorM_StaffID = this.xstaff.Nat_StaffID
                    prm.M_DoctorM_StaffNIK = this.xstaff.Nat_StaffNIK
                    prm.M_DoctorEmail = this.xemail
                    prm.M_DoctorHP = this.xhp
                    prm.M_DoctorNote = this.xnote
                    prm.M_DoctorPhone = this.xphone
                    prm.M_DoctorIsMarketingConfirm = this.xmarketingconfirm
                    prm.M_DoctorEmailIsDefault = this.isdefaultemail
                    prm.M_DoctorM_SpecialistID = this.selected_specialist.id 
                    prm.M_DoctorDOB = this.xdob
                    this.$store.dispatch("doctor/save", prm)
                }
            },
            thr_search_staff: _.debounce(function () {
                this.$store.dispatch("doctor/searchstaff", this.search_staff)
            }, 2000),
            saveNewDoctor() {
                this.$store.commit("doctor/update_errors", [])
                var errors = this.$store.state.doctor.errors
                if (this.xdoctorname === '') {
                    errors.push("requirename")
                }
                if (_.isEmpty(this.xsex)) {
                    errors.push("requiresex")
                }
                if (errors.length === 0) {
                    var prm = {}
                    prm.M_DoctorID = 0
                    prm.M_DoctorPrefix = this.xpreffix
                    prm.M_DoctorPrefi2 = this.xpreffix2
                    prm.M_DoctorName = this.xdoctorname
                    prm.M_DoctorSufix = this.xsuffix
                    prm.M_DoctorSufix2 = this.xsuffix2
                    prm.M_DoctorSufix3 = this.xsuffix3
                    prm.M_DoctorM_SexID = this.$store.state.doctor.selected_sex.M_SexID
                    prm.M_DoctorM_ReligionID = this.$store.state.doctor.selected_religion.M_ReligionID
                    prm.M_DoctorM_StaffID = this.xstaff.Nat_StaffID
                    prm.M_DoctorM_StaffNIK = this.xstaff.Nat_StaffNIK
                    prm.M_DoctorEmail = this.xemail
                    prm.M_DoctorHP = this.xhp
                    prm.M_DoctorNote = this.xnote
                    prm.M_DoctorPhone = this.xphone
                    prm.M_DoctorIsMarketingConfirm = this.xmarketingconfirm
                    prm.M_DoctorEmailIsDefault = this.isdefaultemail
                    prm.M_DoctorM_SpecialistID = this.selected_specialist.id 
                    prm.M_DoctorDOB = this.xdob
                    prm.addresses =  this.$store.state.doctor.addresses
                    this.$store.dispatch("doctor/newdoctor", prm)
                }
            },
            deleteData() {
                let msg = "Yakin, akan menghapus data dokter " + this.xdoctorname + " ?"
                this.$store.commit("doctor/update_msg_confirmation_delete", msg)
                this.$store.commit("doctor/update_dialog_confirmation_delete", true)
            },
            doDeleteData() {
                var prm = {}
                prm.M_DoctorID = this.$store.state.doctor.selected_doctor.M_DoctorID
                prm.M_DoctorPrefix = this.xpreffix
                prm.M_DoctorPrefix2 = this.xpreffix2
                prm.M_DoctorName = this.xdoctorname
                prm.M_DoctorSufix = this.xsuffix
                prm.M_DoctorSufix2 = this.xsuffix2
                prm.M_DoctorSufix3 = this.xsuffix3
                this.$store.dispatch("doctor/delete", prm)
            }
        },
        watch: {
      search_staff(val, old) {
        if (val == old) return
        if (!val) return
        if (val.length < 1) return
        if (this.$store.state.doctor.update_autocomplete_status == 1) return
        this.thr_search_staff()
      }
    }
    }
</script>