<template>
   <v-layout class="fill-height" column>
      <v-dialog v-model="dialogsuccess" persistent  max-width="290">
        <v-card>
            <v-card-title color="success"  class="headline">Berhasil !</v-card-title>
            <v-card-text>
                {{msgsuccess}}
            </v-card-text>
            <v-card-actions>
            <v-spacer></v-spacer>
            <v-btn color="green darken-1" flat @click="closeDialogSuccess">OK</v-btn>
            </v-card-actions>
        </v-card>
      </v-dialog>
      <v-card   class="mb-2 pa-2 searchbox">
         <v-layout >
               <v-text-field class="xs9 ma-1"
                             label=""
                             placeholder="Nama"
                             single-line
                             outline
                             v-model="name"
                             hide-details
                             ></v-text-field>

              
            <v-btn class="xs6 ma-1" color="success"  @click="searchDoctor" >Cari</v-btn>

         </v-layout>
      </v-card>
      <v-card >
         <v-layout row>
            <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
               <v-data-table 
                  :headers="headers" 
                  :items="staffs"
                  :loading="isLoading"
                  :pagination.sync="pagination"
                   class="elevation-1">
                  <template slot="items" slot-scope="props">
                     <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.M_StaffName }}</td>
                     <td class="text-xs-right pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                        <div>
                              <v-btn @click="addToHomeService(props.item,'select')"  v-if="props.item.status === 0 || props.item.status === '0'" small color="error"><v-icon left>close</v-icon>  Belum dipilih</v-btn>
                              <v-btn  v-if="props.item.status > 0" flat small color="success"><v-icon left>check</v-icon> Sudah terpilih</v-btn>
                          </div>
                     </td>
                  </template>
                  <template>
                     <div class="text-xs-center">
                        <v-pagination
                           v-model="page"
                           :length="15"
                           :total-visible="7"
                        ></v-pagination>
                     </div>
                  </template>
               </v-data-table>
            </v-flex>
         </v-layout>
      </v-card>
       <one-dialog-alert :status="openalertconfirmation" :msg="msgalertconfirmation" @forget-dialog-alert="forgetAlertConfirmation()" @close-dialog-alert="closeAlertConfirmation()"></one-dialog-alert>
   </v-layout>
</template>

<style scoped>
.searchbox .v-input.v-text-field .v-input__slot{
   min-height:60px;
}
.searchbox .v-btn {
   min-height:60px;
}
table.v-table tbody td,table.v-table tbody th {
   height: 40px;
}

table.v-table thead tr {
   height: 40px;
}

</style>

<script>
module.exports = {
components : {
      'one-dialog-info':httpVueLoader('../../common/oneDialogInfo.vue'),
      'one-dialog-alert':httpVueLoader('../../common/oneDialogAlert.vue')
   },
   mounted() {
      this.$store.dispatch("staff/search",{
         name:this.name,
         lastid:-1
      })
   },
   methods : {
      isSelected(p) {
         return p.M_StaffID == this.$store.state.staff.selected_staff.M_StaffID
      },
      searchDoctor() {
         this.$store.dispatch("staff/search",{
            name:this.name,
            status: this.xselectedstatus.value,
            lastid:-1
         })
      },
      selectMe(staff) {
         if(this.$store.state.staff.no_save == 0 ){
            this.$store.commit("staff/update_selected_staff",staff)
            this.$store.commit("staff/update_act",'edit')
         }else{
            this.$store.commit("staff/update_open_alert_confirmation",true)
         }
            
      },
      closeAlertConfirmation(){
          this.$store.commit("staff/update_open_alert_confirmation",false)
      },
      forgetAlertConfirmation(){
          this.$store.commit("staff/update_no_save",0)
          this.$store.commit("staff/update_open_alert_confirmation",false)
      },
      updateAlert_success(val){
         this.$store.commit("staff/update_alert_success",val)
      },
      setNewDoctor(){
         this.$store.commit("staff/update_selected_staff",{})
         this.$store.commit("staff/update_preffix",'')
         this.$store.commit("staff/update_staff_name",'')
         this.$store.commit("staff/update_suffix",'')
         this.$store.commit("staff/update_selected_sex",{})
         this.$store.commit("staff/update_selected_religion",{})
         this.$store.commit("staff/update_phone",'')
         this.$store.commit("staff/update_email",'')
         this.$store.commit("staff/update_hp",'')
         this.$store.commit("staff/update_pj",'N')
         this.$store.commit("staff/update_dpj",'N')
         this.$store.commit("staff/update_clinic",'N')
         this.$store.commit("staff/update_marketing_confirm",'N')
         this.$store.commit("staff/update_is_default",'N')
         this.$store.commit("staff/update_addresses",[])

         this.$store.commit("staff/update_act",'new')
      },
      closeDialogSuccess(){
         let arrstaff = this.$store.state.staff.staffs
         var idx = _.findIndex(arrstaff, item => item.M_StaffID === this.$store.state.staff.last_id)
         console.log(idx)
         this.$store.dispatch("staff/search",{
            name:this.name,
            status: this.xselectedstatus.value,
            lastid: idx
         })
         this.$store.commit("staff/update_dialog_success",false)
      },
       addToHomeService(value,act){
          value.searchname = this.name
         this.$store.dispatch("staff/settohomeservice",value)
      }
   },
   computed: {
      dialogsuccess: {
         get() {
            return this.$store.state.staff.dialog_success
         },
         set(val) {
            this.$store.commit("staff/update_dialog_success",val)
         }
      },
      msgsuccess(){
         return this.$store.state.staff.msg_success
      },
      snackbar: {
         get() {
            return this.$store.state.staff.alert_success
         },
         set(val) {
            this.$store.commit("staff/update_alert_success",val)
         }
      },
      isLoading() {
         return this.$store.state.staff.search_status == 1
      },
      xstatuses() {
         return this.$store.state.staff.statuses
      },
      xselectedstatus: {
         get() {
            return this.$store.state.staff.selected_status
         },
         set(val) {
            this.$store.commit("staff/update_selected_status",val)
         }
      },
      staffs() {
         return this.$store.state.staff.staffs
      },
      openalertconfirmation: {
         get() {
            return this.$store.state.staff.open_alert_confirmation
         },
         set(val) {
            this.$store.commit("staff/update_open_alert_confirmation",val)
         }
      }
   },
   data() {
      return {
         msgalertconfirmation:"Perubahan yang telah dilakukan belum disimpan dong !",
         items: [],
         name: '',
         page:1,
         headers: [
            {
               text: "NAMA",
               align: "left",
               sortable: false,
               value: "M_StaffName",
               width: "25%",
               class: "pa-2 blue lighten-3 white--text"
            },
            {
               text: "STATUS",
               align: "right",
               sortable: false,
               value: "status",
               width: "15%",
               class: "pa-2 blue lighten-3 white--text"
            }
         ],
         pagination:{
            descending: false,
            page: 1,
            rowsPerPage: 10,
            sortBy: 'M_StaffName',
            totalItems: this.$store.state.staff.total_staffs
         }
      };
   }
}
</script>
