<template>
   <v-card class="pa-1" height="100%">
       <v-layout align-center row>
         <v-flex xs12>
            <v-card tile flat color=" pa-2 teal lighten-4">
               <h5 class=" subtitle-1 font-weight-bold">PENGIRIM DAN BAHASA</h5>
            </v-card>
         </v-flex>
      </v-layout>
      <v-layout column>
         <v-flex xs12 class="pa-2">
            <v-layout>
               <v-flex xs6 mr-1>
                    <v-autocomplete
                        label="Pengirim"
                        v-model="selected_doctor"
                        :items="doctors"
                        :search-input.sync="search"
                        auto-select-first
                        style="font-size:12px;padding:4px 0"
                        no-filter
                        return-object
                        clearable
                        item-text="M_DoctorName"
                        :loading="is_loading"
                        no-data-text="Pilih Pengirim"
                        hide-details
                        >
                        <template
                            slot="item"
                            slot-scope="{ item }"
                            >
                            <v-list-tile-content>
                            <v-list-tile-title v-text="item.M_DoctorName"></v-list-tile-title>
                            <v-list-tile-sub-title v-text="getAddress(item)"></v-list-tile-sub-title>
                            </v-list-tile-content>
                        </template>

                    </v-autocomplete>
                    <span class="caption">{{selected_doctor.M_DoctorNote}}</span>
               </v-flex>
                
               <v-flex xs6 ml-1>
                  <v-select
                     v-model="selected_address"
                     :items="doctor_address"
                     hide-details
                     style="font-size:12px;padding:4px 0"
                     item-text="description"
                     item-value="M_DoctorAddressID"
                     return-object
                     label="Alamat"
                     ></v-select>
               </v-flex>

            </v-layout>
            <v-layout v-if="showalias" row>
                <v-flex xs12>
                    <v-text-field
                        background-color="teal lighten-4"
                        single-line
                        hide-details
                        v-model="doctor_alias"
                         style="font-size:12px;padding:4px 0"
                        placeholder="Isikan nama alias dokter di sini, lengkap dengan gelar"
                        box
                    ></v-text-field>
                </v-flex>
            </v-layout>
            <p v-if="showalias" class="mb-1 ma-1 mono caption"><code>* Nama alias digunakan ketika data dokter belum ada, untuk ditampilkan di nota</code></p>
            <v-layout v-if="showalias" row>
                <v-flex xs12>
                    <v-textarea
                        box
                        background-color="teal lighten-4"
                         style="font-size:12px;padding:4px 0"
                        single-line
                        hide-details
                        auto-grow
                        rows="1"
                        v-model="doctor_alias_address"
                        placeholder="Isikan alamat lengkap di sini"
                        ></v-textarea>
                </v-flex>
            </v-layout>
            <p v-if="showalias" class="mb-1 ma-1 mono caption"><code>* Isikan juga alamat dokter lengkap, untuk ditampilkan di nota</code></p>
            <v-layout row mt-3>
                <v-flex xs6 mr-1>
                    <v-select
                        readonly
                        v-model="selected_language"
                        :items="languages"
                        item-text="name"
                        item-value="key"
                        style="font-size:12px;padding:4px 0"
                        label="Bahasa"
                        return-object
                        ></v-select>

                </v-flex>

                <v-flex xs6>
                    <v-select
                        v-model="selected_language_2"
                        :items="languages_2"
                        item-text="name"
                        item-value="key"
                        style="font-size:12px;padding:4px 0"
                        label="Bahasa 2"
                        return-object
                        clearable
                        ></v-select>

                </v-flex>
            </v-layout>
         </v-flex>

         <v-flex xs12>
            <patient-delivery></patient-delivery>
         </v-flex>
      </v-layout>
   </v-card>
</template>
<script>
module.exports = {
    components: {
        'patient-delivery' : httpVueLoader('./patientDelivery.vue')
    },

    data() {
        return {
            n_mounted : 1
        }
    },
   computed: {
       doctor_alias_address:{
         get(){
            return this.$store.state.doctor.doctor_alias_address
         },
         set(val)  {
            this.$store.commit('doctor/update_doctor_alias_address',val)
         }
      },
       doctor_alias:{
         get(){
            return this.$store.state.doctor.doctor_alias
         },
         set(val)  {
            //alert(val.M_DoctorName)
            this.$store.commit('doctor/update_doctor_alias',val)
            var xdoc = this.$store.state.doctor.selected_doctor
            var company = this.$store.state.company.selected_company
        

            var doctorname = xdoc.M_DoctorName.split(']')
            //alert(xdoc.M_DoctorName.split(']'))
            console.log(doctorname)
            if(doctorname[1].trim() === '-' && company.M_CompanyName !== 'PASIEN MANDIRI'){
                this.$store.commit("company/update_show_alias_doctor",true)
            }
            else{
                this.$store.commit("company/update_show_alias_doctor",false)
                 this.$store.commit('doctor/update_doctor_alias','')
                 this.$store.commit('doctor/update_doctor_alias_address','')
            }

            if(company){
                if(val === '' && doctorname[1].trim() === '-' && company.M_CompanyName !== 'PASIEN MANDIRI'){
                    this.$store.commit('doctor/update_show_doctor_alert',true)
                }
                else{
                    this.$store.commit('doctor/update_show_doctor_alert',false)
                }
            }
            else{
                this.$store.commit('doctor/update_show_doctor_alert',false)
            }
            
         }
      },
      search:{
         get(){
            return this.$store.state.doctor.search
         },
         set(val)  {
            this.$store.commit('doctor/update_search',val)
         }
      },
      
      languages() {
         return this.$store.state.language.languages
      },

        languages_2 () {
            return this.$store.state.language.languages_2
        },

        selected_language: {
            get() {
                return this.$store.state.language.selected_language
            },
            set(val) {
                this.$store.commit('language/update_selected_language', val)
                // this.$store.commit('language/update_selected_language_2', null)

                
            }
        },

        selected_language_2: {
            get() {
                return this.$store.state.language.selected_language_2
            },
            set(val) {
                this.$store.commit('language/update_selected_language_2', val)
            }
        },

      selected_doctor_pj: {
         get() {
            return this.$store.state.doctor.selected_doctor_pj
         },
         set(val) {
            this.$store.commit('doctor/update_selected_doctor_pj',val)
         }
      },
      doctors_pj() {
         return this.$store.state.doctor.doctors_pj
      },

        selected_address: {
            get() {
                return this.$store.state.doctor.selected_address
            },
            set(val) {
                this.$store.commit('doctor/update_selected_address', val)
            }
        },

      doctor_address() {
         if (! this.$store.state.doctor.selected_doctor ) return []
         if (! this.$store.state.doctor.selected_doctor.address) return []
         return this.$store.state.doctor.selected_doctor.address
      },
      doctors() {
         return this.$store.state.doctor.doctors
      },
      selected_doctor: {
         get() {
            return this.$store.state.doctor.selected_doctor
         },
         set(val) {
            if (val) {
                this.$store.commit("doctor/update_selected_doctor", val)

                // Auto CHECKED
                let cdoc = val
                //alert("YYY")
                
                //this.$store.commit('delivery/update_params', {d_id:val.M_DoctorID})
                //this.$store.dispatch('delivery/search')
                var company = this.$store.state.company.selected_company
                var doctorname = cdoc.M_DoctorName.split(' ] ')

                if(doctorname[1].trim() === '-' && company.M_CompanyName !== 'PASIEN MANDIRI'){
                    this.$store.commit("company/update_show_alias_doctor",true)
                }
                else{
                    this.$store.commit("company/update_show_alias_doctor",false)
                    this.$store.commit('doctor/update_doctor_alias','')
                    this.$store.commit('doctor/update_doctor_alias_address','')
                }
                
                console.log(company)
                if(company){
                    if(this.doctor_alias === '' && doctorname[1].trim() === '-' && company.M_CompanyName !== 'PASIEN MANDIRI'){
                        this.$store.commit('doctor/update_show_doctor_alert',true)
                        //alert('YYY')
                    }
                    else{
                        this.$store.commit('doctor/update_show_doctor_alert',false)
                    }
                }
                 else{
                        this.$store.commit('doctor/update_show_doctor_alert',false)
                    }
                
                this.$store.dispatch('delivery/search_deliveries',{type:'doctor',id:val.M_DoctorID})
            } else {
                this.$store.commit("doctor/update_selected_doctor", {})
                this.$store.commit('delivery/update_params', {d_id:0})
                this.$store.dispatch('delivery/search_deliveries',{type:'doctor',id:0})
                this.$store.commit('doctor/update_show_doctor_alert',true)
            }
            
         }
      },
      selected_doctor_x() {
         
            return this.$store.state.doctor.selected_doctor
         
      },
      is_loading: {
         get() {
            return this.$store.state.doctor.search_status == 1
         },
         set(val) {
            this.$store.commit("doctoc/update_search_status",val ? 1 : 2)
         }
      },
      showalias: {
         get() {
            return this.$store.state.company.show_alias_doctor
         },
         set(val) {
            this.$store.commit("company/update_show_alias_doctor",val)
         }
      }
   },
   methods: {
       xxxshowalias(){
           var value = this.$store.state.doctor.selected_doctor
           var doctorname = doctor.M_DoctorName.split(']')
           var company = this.$store.state.company.selected_company
           console.log(value)
           if(!_.isEmpty(value)){
              if(this.doctor_alias === '' && doctorname[1].trim() === '-' && company.M_CompanyName !== 'PASIEN MANDIRI')
               return true
               else{
                   this.doctor_alias = ''
                   this.doctor_alias_address = ''
                   return false
               }
           }
           else{
                this.doctor_alias = ''
                this.doctor_alias_address = ''
                return false
            }
           
       },
      getAddress(doc) {
         
         var s_addr = ''
         if (! doc.address ) return ''
         doc.address.forEach(function(addr){
            if (s_addr != '') s_addr += ', '
            s_addr += addr.M_DoctorAddressDescription
         });
         return s_addr
      },
        thr_search: _.debounce( function () {
            if (!window.one_token()) {
                this.$store.commit('update_message_error', 'Maaf, koneksi Anda sempat terputus silahkan Log Out dan Login kembali')
                this.$store.commit('update_dialog_error', true)
                return
            }
            
           // this.$store.commit("doctor/update_search_status", 1) // LOADING
            this.$store.dispatch("doctor/search")
        }, 1000),

      doctorAdd() {
          this.$store.commit('doctor_new/update_dialog_new', true)
      }
   },
   watch : {
        search(val, old) {
            if (this.$store.state.order.is_from_clinic)
                return
            if (this.$store.state.doctor_new.adhoc) {
                var store = this.$store
                setTimeout(function() {
                    store.commit('doctor_new/update_adhoc', false)
                    delete store;
                }, 2000)
                return
            }
           
            this.$store.commit("doctor/update_search", val)
           
            if (val == "" ) return
            if (val == old ) return
            if (! val) return
            if (val.length < 2 ) return
            if (this.$store.state.doctor.search_status == 1 ) return
            this.thr_search()
        },

        selected_address(val, o) {
            let dlv = this.$store.state.delivery.checked_id

            if (!val) {
                // REMOVE FROM DELIVERY
                let idx = dlv.indexOf(o.delivery_code)
                if ( idx > -1) {
                    dlv.splice(idx, 1)
                    this.$store.commit('delivery/update_checked_id', dlv)
                }
            }

            if (val) {
                // Auto CHECKED
                let cadd = val
                if (cadd.delivery_default == "Y") {
                    if (dlv.indexOf(cadd.delivery_code) < 0) {
                        dlv.push(cadd.delivery_code)
                        this.$store.commit('delivery/update_checked_id', dlv)
                    }
                }
                
            }
        },

        selected_doctor(val, o) {
            let dlv = this.$store.state.delivery.checked_id

            // REMOVE FROM DELIVERY
            if (!val.email_default) {
                
                let idx = dlv.indexOf(o.delivery_code)
                if ( idx > -1) {
                    dlv.splice(idx, 1)
                    this.$store.commit('delivery/update_checked_id', dlv)
                }
            } else if (val.email_default != "Y") {
                let idx = dlv.indexOf(o.delivery_code)
                if ( idx > -1) {
                    dlv.splice(idx, 1)
                    this.$store.commit('delivery/update_checked_id', dlv)
                }
            }
        }
   },
   mounted: function() {
      this.$store.dispatch("language/search")
      /*
      if ( this.$store.state.doctor.mounted < 1 ) {
         this.$store.dispatch("doctor/search")
         this.$store.dispatch("doctor/search_pj")
         this.$store.dispatch("language/search")
      }
      
      this.$store.commit("doctor/increment_mounted", 1);
      console.log("doctor mounted : ", this.$store.state.doctor.mounted)
      */
   }
}
</script>
