<template>
    <v-layout  column >
        <v-card>
            <v-layout row>
                <v-flex xs12>
                    <v-subheader red--text text--lighten-1> STAF HOME SERVICE
                    <!--<v-flex text-md-right>
                        <v-btn @click="createNewAddress()" small color="info">Baru</v-btn>
                    </v-flex>-->
                    </v-subheader>
                    <v-divider></v-divider>
                    <v-layout row wrap>
                        <v-flex xs12 pa-2>
                            <v-data-table 
                                :headers="headers" 
                                :items="xhomeservicestaffs"
                                :loading="isLoading"
                                hide-actions class="elevation-1">
                                <template slot="items" slot-scope="props">
                                    <td class="text-xs-center pa-2">
                                        <v-icon color="error" @click="deleteStaff(props.item)">delete</v-icon>
                                    </td>
                                    <td class="text-xs-left pa-2">{{ props.item.M_StaffName}}</td>
                                </template>
                            </v-data-table>
                            
                        </v-flex>
                    </v-layout>
                </v-flex>
            </v-flex>          
        </v-card>
    </v-layout>
</template>

<style scoped>

</style>

<script>
module.exports = {
    data: () => ({
            search_city:'',
            oldlabel:'',
            headers: [
            {
               text: "AKSI",
               align: "center",
               sortable: false,
               value: "action",
               width: "5%",
               class: "pa-1 blue lighten-3 white--text"
            },
            {
               text: "STAF",
               align: "left",
               sortable: false,
               value: "mr",
               width: "20%",
               class: "pa-1 blue lighten-3 white--text"
            }
         ]
    }),
    computed: {
        dialogconfirmationdeleteaddr:{
            get() {
                return this.$store.state.staff.dialog_confirmation_delete_addr
            },
            set(val) {
                this.$store.commit("staff/update_dialog_confirmation_delete_addr",val)
            }
        },
        msgconfirmationdeleteaddr(){
            return  this.$store.state.staff.msg_confirmation_delete_addr
        },
        xact() {
            return this.$store.state.staff.act
        },
        xactaddr() {
            return this.$store.state.staff.act_addr
        },
        dialogformaddress:{
            get() {
                return this.$store.state.staff.dialog_form_address
            },
            set(val) {
                this.$store.commit("staff/update_dialog_form_address",val)
            }
        },
        isLoading() {
            return this.$store.state.staff.search_status == 1
        },
        xhomeservicestaffs() {
            return this.$store.state.staff.homeservice_staffs
        },
        xcities(){
            return this.$store.state.staff.cities
        },
        labeladdress:{
            get() {
                return this.$store.state.staff.label_address
            },
            set(val) {
                this.$store.commit("staff/update_label_address",val)
            }
        },
        cityaddress:{
            get() {
                return this.$store.state.staff.city_address
            },
            set(val) {
                this.$store.commit("staff/update_city_address",val)
                this.$store.dispatch("staff/getdistrict",this.$store.state.staff.city_address)
            }
        },
        xdistricts(){
            return this.$store.state.staff.districts
        },
        districtaddress:{
            get() {
                return this.$store.state.staff.district_address
            },
            set(val) {
                this.$store.commit("staff/update_district_address",val)
                this.$store.dispatch("staff/getkelurahan",this.$store.state.staff.district_address)
            }
        },
        xkelurahans(){
            return this.$store.state.staff.kelurahans
        },
        kelurahanaddress:{
            get() {
                return this.$store.state.staff.kelurahan_address
            },
            set(val) {
                this.$store.commit("staff/update_kelurahan_address",val)
            }
        },
        descriptionaddress:{
            get() {
                return this.$store.state.staff.description_address
            },
            set(val) {
                this.$store.commit("staff/update_description_address",val)
            }
        },
    },
    methods : {
        createNewAddress(){
            this.$store.commit("staff/update_act_addr",'new')
            this.search_city = ''
            this.labeladdress = ''
            this.$store.commit("staff/update_cities",[])
            this.cityaddress = {}
            this.$store.commit("staff/update_districts",[])
            this.districtaddress = {}
            this.$store.commit("staff/update_kelurahans",[])
            this.kelurahanaddress = {}
            this.descriptionaddress = ''
            this.$store.commit("staff/update_dialog_form_address",true)
        },
        thr_search_city: _.debounce( function () {
            this.$store.dispatch("staff/searchcity",this.search_city)
        },2000),
        checkError(value){
            var errors = this.$store.state.staff.errors
            if(errors.includes(value)){
                return true
            }
            else{
                return false
            }
        },
        saveNewAddress(){
            this.$store.commit("staff/update_errors",[])
            var errors = this.$store.state.staff.errors
            if(this.labeladdress === ''){
                errors.push("requiredlabel")
            }
            if(_.isEmpty(this.cityaddress)){
                errors.push("requiredcity")
            }
            if(_.isEmpty(this.districtaddress)){
                errors.push("requireddistrict")
            }
            if(_.isEmpty(this.kelurahanaddress)){
                errors.push("requiredkelurahan")
            }
            if(_.isEmpty(this.descriptionaddress)){
                errors.push("requireddescription")
            }

            if(errors.length === 0){
                var prm = {}
                prm.M_DoctorAddressM_DoctorID = this.$store.state.staff.selected_doctor.M_DoctorID
                prm.M_DoctorName = this.$store.state.staff.selected_doctor.M_DoctorName
                prm.M_DoctorAddressNote = this.labeladdress
                prm.M_DoctorAddressDescription =  this.descriptionaddress
                prm.M_DoctorAddressM_KelurahanID = this.kelurahanaddress.M_KelurahanID
                this.$store.dispatch("staff/savenewaddress",prm)
            }
        },
        editAddress(value){
            this.$store.commit("staff/update_act_addr",'edit')
            this.$store.commit("staff/update_x_addr_id",value.M_DoctorAddressID)
            this.labeladdress = value.M_DoctorAddressNote
            this.oldlabel = value.M_DoctorAddressNote
            this.$store.commit("staff/update_cities",[{M_CityID:value.M_CityID,M_CityName:value.M_CityName}])
            this.cityaddress = {M_CityID:value.M_CityID,M_CityName:value.M_CityName}
            this.$store.commit("staff/update_districts",[{M_DistrictID:value.M_DistrictID,M_DistrictName:value.M_DistrictName}])
            this.districtaddress = {M_DistrictID:value.M_DistrictID,M_DistrictName:value.M_DistrictName}
            this.$store.commit("staff/update_kelurahans",[{M_KelurahanID:value.M_DoctorAddressM_KelurahanID,M_KelurahanName:value.M_KelurahanName}])
            this.kelurahanaddress = {M_KelurahanID:value.M_DoctorAddressM_KelurahanID,M_KelurahanName:value.M_KelurahanName}
            this.descriptionaddress = value.M_DoctorAddressDescription
            this.$store.commit("staff/update_dialog_form_address",true)
        },
        saveEditAddress(){
            this.$store.commit("staff/update_errors",[])
            var errors = this.$store.state.staff.errors
            if(this.labeladdress === ''){
                errors.push("requiredlabel")
            }
            if(this.oldlabel.toLowerCase() === 'utama' && this.labeladdress.toLowerCase() !== 'utama'){
                errors.push("readonlyutama")
            }
            if(_.isEmpty(this.cityaddress)){
                errors.push("requiredcity")
            }
            if(_.isEmpty(this.districtaddress)){
                errors.push("requireddistrict")
            }
            if(_.isEmpty(this.kelurahanaddress)){
                errors.push("requiredkelurahan")
            }
            if(_.isEmpty(this.descriptionaddress)){
                errors.push("requireddescription")
            }

            if(errors.length === 0){
                var prm = {}
                prm.M_DoctorAddressID = this.$store.state.staff.x_addr_id
                prm.M_DoctorAddressM_DoctorID = this.$store.state.staff.selected_doctor.M_DoctorID
                prm.M_DoctorName = this.$store.state.staff.selected_doctor.M_DoctorName
                prm.M_DoctorAddressNote = this.labeladdress
                prm.M_DoctorAddressDescription =  this.descriptionaddress
                prm.M_DoctorAddressM_KelurahanID = this.kelurahanaddress.M_KelurahanID
                this.$store.dispatch("staff/saveeditaddress",prm)
            }
        },
        deleteAddress(value){
            this.$store.commit("staff/update_act_addr",'delete')
            this.$store.commit("staff/update_x_addr_id",value.M_DoctorAddressID)
            this.$store.commit("staff/update_errors",[])
            this.oldlabel = value.M_DoctorAddressNote
            var errors = this.$store.state.staff.errors
            if(value.M_DoctorAddressNote.toLowerCase() === 'utama'){
                errors.push("deleteutama")
            }
            var msg = ''
            if(errors.includes("deleteutama")){
                msg = "Biarkan yang utama tetap ada"
            }
            else{
                msg = "Yakin, akan menghapus data alamat dokter "+value.M_DoctorAddressNote+" ?"
            }
            
            this.$store.commit("staff/update_msg_confirmation_delete_addr",msg)
            this.$store.commit("staff/update_dialog_confirmation_delete_addr",true)
        },
        doDeleteAddr(){
            var prm = {}
            prm.M_DoctorAddressID = this.$store.state.staff.x_addr_id
            prm.M_DoctorAddressM_DoctorID = this.$store.state.staff.selected_doctor.M_DoctorID
            prm.M_DoctorName = this.$store.state.staff.selected_doctor.M_DoctorName
            prm.M_DoctorAddressNote = this.oldlabel
            this.$store.dispatch("staff/deleteaddress",prm)
        },
        deleteStaff(value){
          value.searchname = this.name
         this.$store.dispatch("staff/settohomeservice",value)
      }
    },
    watch: {
        search_city(val,old) {
            if (val == old ) return
            if (! val) return
            if (val.length < 1 ) return
            if (this.$store.state.staff.update_autocomplete_status == 1 ) return  
            this.thr_search_city()
        }
   }
}
</script>
