<template>
   <v-layout fil-height column>
      <v-dialog v-model="dialogvalidation" persistent width="300">
        <v-card>
            <v-card-title class="headline">Peringatan !</v-card-title>
            <v-card-text>
                <v-alert v-for="item in msgdialogvalidation"
                    :value="true"
                    type="error"
                    >
                   {{item}} [ Belum ada alasan ]
                </v-alert>
            </v-card-text>
            <v-card-actions>
            <v-spacer></v-spacer>
            <v-btn color="green darken-1" flat @click="dialogvalidation = false">OK</v-btn>
            </v-card-actions>
        </v-card>
        </v-dialog>
        <v-dialog v-model="dialoglist" persistent width="350">
           <v-card>
              <v-card-title style="font-size: 18px;padding-left: 12px;padding-top: 8px">Pilih data yang belum sesuai</v-card-title>
              <v-card-text>
                  <v-layout wrap>
                     <v-flex xs12 pa-2 v-for="(vp,idx) in vdoctor" :key="vp.id">
                        <v-layout pl-2 row>
                           <v-flex x12>
                              <v-layout row>
                                 <v-flex pt-1 xs2>
                                    <span @click="changeCheck(vp.chex,idx)" v-if="vp.chex === true" style="background: rgb(252, 252, 252);border: 2px solid #767575;" class="icon-medium-fill-base-small white--text"></span>
                                    <span @click="changeCheck(vp.chex,idx)" v-if="vp.chex === false" style="background:#9e9e9e7a" class="icon-medium-fill-base-small white--text red"><v-icon dark>close</v-icon></span>
                                 </v-flex>
                                 <v-flex xs12>
                                    <v-text-field
                                       :label="vp.label"
                                       hide-details
                                       placeholder="Isikan alasan di sini"
                                       v-model="vp.note"
                                       outline
                                    ></v-text-field>
                                 </v-flex>
                              </v-layout>
                           </v-flex>
                        </v-layout>
                     </v-flex>
                  </v-layout>
              </v-card-text>
              <v-card-actions>
               <v-spacer></v-spacer>
               <v-btn color="black" flat @click="dialoglist = false">Tutup</v-btn>
               <v-btn v-if="seen_button && show_simpan_tolak" color="primary darken-1" flat @click="tolak()">Simpan</v-btn>
               </v-card-actions>
           </v-card>
        </v-dialog>
   </v-layout>
</template>

<style scoped>
</style>

<script>
module.exports = {
   components : {
      'one-field-verification' : httpVueLoader('../../common/oneFieldVerification.1.vue'),
      'one-testing-component':httpVueLoader('../../common/oneTestingComponent.vue'),
      'one-field-verification-patient-doctor':httpVueLoader('../../common/oneFieldVerificationPatientDoctor.vue')
   },
   data () {
      return {
        dialogvalidation: false,
        msgdialogvalidation:[]
      }
    },
   computed: {
      patient_age() {
         if ( this.patient == undefined) {
            return ''
         }
         return window.calculate_age(this.patient.M_PatientDOB)
      },
      xshow() {
         return this.$store.state.patient.show_right
      },
      patient() {
         return this.$store.state.patient.selected_patient
      },
      vpatient() {
         return this.$store.state.verification.verification_patient
      },
      vdoctor() {
         return this.$store.state.patient.verifications
      },
      status(){
          return this.$store.state.status.selected_status
      },
      seen_button: {
         get() {
               return this.$store.state.patient.btn_save_seen
         },
         set(val) {
               this.$store.commit("patient/update_btn_save_seen",val)
         }
      },
      dialoglist: {
         get() {
               return this.$store.state.patient.dialoglist
         },
         set(val) {
               this.$store.commit("patient/update_dialoglist",val)
         }
      },
      show_simpan_tolak: {
         get() {
               return this.$store.state.patient.show_simpan_tolak
         },
         set(val) {
               this.$store.commit("patient/update_show_simpan_tolak",val)
         }
      },
   },
   methods : {
      changeCheck(val,idx){
         this.show_simpan_tolak = false
         var xxx = this.$store.state.patient.verifications
         var xnew = val === true ?false:true
         xxx[idx].chex = xnew
         this.$store.commit("patient/update_verifications",xxx)
         var xtolak = _.filter(xxx, function(o) { return !o.chex })
         if(xtolak.length > 0)
            this.show_simpan_tolak = true
      },
      updateDataTxtParentDoctor(idx, val) { 
          var verifications = this.$store.state.patient.verifications
          verifications[idx].note = val
          this.$store.commit("patient/update_verifications",verifications)
          var xcount = this.$store.state.patient.no_save + 1
          this.$store.commit("patient/update_no_save",xcount)
      },
      updateDataCbxParentDoctor(idx, val) {
          var verifications = this.$store.state.patient.verifications
          verifications[idx].chex = val
          this.$store.commit("patient/update_verifications",verifications)
          var xcount = this.$store.state.patient.no_save + 1
          this.$store.commit("patient/update_no_save",xcount)
      },
      tolak(){
        var xerrors = []
        var allArr = this.$store.state.patient.selected_patient 
        allArr.verifications.forEach(function(value) {
            if(value.chex === false && value.note === "")
                xerrors.push(value.label);
        });
        
         
         if(xerrors.length === 0){
            var prm = this.$store.state.patient.selected_patient 
            prm.act = 'N'
            prm.name = this.$store.state.patient.name
            prm.nolab = this.$store.state.patient.nolab
            this.$store.commit("done/update_current_page",1)
            prm.done_search = {
               startdate:this.$store.state.done.start_date,
               enddate:this.$store.state.done.end_date,
               search:this.$store.state.done.search,
               current_page:1,
               lastid:-1
            }
            this.$store.dispatch("patient/verify",prm)
         }
         else{
            var msg = ""
            xerrors.forEach(function(value) {
               msg += '<v-alert :value="true" type="error">'+value+'</v-alert>'
            });
            this.msgdialogvalidation = xerrors
            this.dialogvalidation = true
         }
         
      },
      
   }
}
</script>
