<template>
  <v-layout row justify-center>
    <v-dialog v-model="dialog_lcprep" persistent max-width="75%">
      <v-card>
        <v-card-title class="red white--text">
          <span class="headline">PAP SMEAR (LCPrep)</span>
        </v-card-title>
        <v-card-text>
            <v-layout class="mb-2" row>
              <v-flex xs12>
                  <v-select
                      item-text="name"
                      return-object
                      style="font-size:12px"
                      class="mini-select"
                      outline
                      :items="doctors"
                      v-model="selected_doctor"
                      hide-details
                      label="Dokter"
                  ></v-select>
                  <p v-if="error_doctor" class="error pl-2 pr-2" style="color:#fff">Dokternya siapa ? jangan lupa ya ...</p>
              </v-flex>
            </v-layout>
              
                <v-layout class="mt-2 mb-2" row>
                    <v-flex xs6>
                      <v-card
                        outlined
                        class="mr-1 pa-2"
                      >
                      <v-card-text>
                        <p class="overline mb-0 font-weight-bold">ADEKUASI BAHAN PEMERIKSAAN</p>
                        <v-checkbox v-for="chx_adekuasi in adekuasi"
                              class="mono"
                              v-model="chx_adekuasi.chex"
                              :label="chx_adekuasi.name" 
                              hide-details
                            ></v-checkbox>
                      </v-card-text>
                      
                       </v-card>
                    </v-flex>
                  <v-flex xs6>
                    <v-card
                      outlined
                      class="ml-1 pa-2"
                    >
                    <v-card-text>
                        <p class="overline mb-0 font-weight-bold">KATEGORI UMUM</p>
                        <v-checkbox v-for="chx_kategoriumum in kategoriumum" 
                              class="mono"
                              v-model="chx_kategoriumum.chex"
                            :label="chx_kategoriumum.name"
                              hide-details
                            ></v-checkbox>
                      </v-card-text>
                    </v-card>
                  </v-flex>
                </v-layout>
            <v-divider></v-divider>
            <v-layout wrap>
              <v-flex v-for="(details,idx) in interpretasi" :class="{'xs3':idx===0,'xs7 pl-1':idx===1,'xs2 pl-1':idx===2}">
                <v-layout row>
                  <v-flex xs12>
                    <v-card>
                      <v-card-title color="warning"  class="purple white--text">
                          <h5 class="subheader overline font-weight-bold">{{details.group_name}}</h5>
                      </v-card-title>
                      <v-list
                        dense
                        subheader
                        single-line
                        v-for="(subgroups,idxx) in details.subgroups" :key="details.id"
                      >
                        <v-subheader class="font-weight-black">{{subgroups.subgroup_name}}</v-subheader>
                        <v-list-tile v-if="subgroups.childs.length > 0" v-for="(childs,idxxx) in subgroups.childs" :key="childs.id" >
                          <div v-if="childs.has_result === 'Y' && childs.type_result === 'CHECK'"
                              :class="{'pl-0':childs.code.length === 2,'pl-4':childs.code.length === 4}"
                          >
                            <v-list-tile-action class="mb-2 mono">
                              <v-checkbox
                                v-model="childs.chex"
                                :label="childs.name"
                                :disabled="childs.is_parent === 'Y'"
                                @change="changeChildsChex(childs,idxxx,idxx,idx)"
                                hide-details
                              ></v-checkbox>
                            </v-list-tile-action>
                        </div>
                        <div v-if="childs.has_result === 'Y' && childs.type_result === 'TEXT'">
                            <v-list-tile-action class="mb-2 pl-0 mono">
                              <v-text-field
                                label=""
                                single-line
                                v-model="childs.value"
                              ></v-text-field>
                            </v-list-tile-action>
                        </div>

                      </v-list>
                    </v-card>
                  </v-flex>
                </v-layout>
              </v-flex>
            </v-layout>
            <v-layout class="mb-2 mt-2" column >
              <v-card>
                <v-card-title color="warning"  class="purple white--text">
                  <h5 class="subheader overline font-weight-bold">HASIL AKHIR</h5>
                </v-card-title>
                <v-card-text class="pa-2">
                  <v-layout v-for="result in results"  mt-2 mb-2 row>
                    <v-flex xs12>
                        <v-textarea
                            auto-grow
                            :label="result.label"
                            outline
                            rows="2"
                            v-model="result.result"
                            hide-details
                        ></v-textarea>
                    </v-flex>
                </v-layout>
                </v-card-text>
              </v-card>
            </v-layout>
        </v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn color="green darken-1" flat @click="dialog_lcprep = false">Tutup</v-btn>
          <v-btn v-if="!onprocess" color="green darken-1" @click="saveResult_Lcprep" flat >Simpan</v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
  </v-layout>
</template>
<style scoped>
.v-input.lcprep .v-label {
    height: 20px!important;
    line-height: 25px!important;
}
</style>
 <script>
 module.exports = {
   mounted() {
      //this.$store.dispatch("re_lcprep/get_doctors",{token:''})
   },
   data() {
      return {
        error_doctor:false
        
      };
   },
    computed: {
      onprocess(){
          return this.$store.state.re_lcprep.onprocess
        },
        dialog_lcprep: {
            get() {
                return this.$store.state.re_lcprep.dialog_lcprep
            },
            set(val) {
                this.$store.commit("re_lcprep/update_dialog_lcprep", val)
            }
        },
        results: {
            get() {
                return this.$store.state.re_lcprep.results
            },
            set(val) {
                this.$store.commit("re_lcprep/update_results", val)
            }
        },
        adekuasi: {
            get() {
                return this.$store.state.re_lcprep.adekuasi
            },
            set(val) {
                this.$store.commit("re_lcprep/update_adekuasi", val)
            }
        },
        kategoriumum: {
            get() {
                return this.$store.state.re_lcprep.kategoriumum
            },
            set(val) {
                this.$store.commit("re_lcprep/update_kategoriumum", val)
            }
        },
        interpretasi: {
            get() {
                return this.$store.state.re_lcprep.interpretasi
            },
            set(val) {
                this.$store.commit("re_lcprep/update_interpretasi", val)
            }
        },
        doctors(){
          return this.$store.state.re_lcprep.doctors
        },
        selected_doctor: {
            get() {
                return this.$store.state.re_lcprep.selected_doctor
            },
            set(val) {
                this.$store.commit("re_lcprep/update_selected_doctor", val)
            }
        }
    },
    methods: {
        changeChildsChex(childs,idxxx,idxx,idx){
          let interpretasi = this.interpretasi
          var parent_id = interpretasi[idx].subgroups[idxx].childs[idxxx].parent_id
          if(parent_id !== '0'){
            let childs = interpretasi[idx].subgroups[idxx].childs
            let index_parent = _.findIndex(childs, function(o) { return o.id === parent_id }) 
            if(childs.chex)
              interpretasi[idx].subgroups[idxx].childs[index_parent].chex = true
            else
              {
                let xfilter = _.filter(childs, { 'chex': true, 'parent_id': parent_id })
                if(xfilter.length === 0)
                  interpretasi[idx].subgroups[idxx].childs[index_parent].chex = false
                else
                  interpretasi[idx].subgroups[idxx].childs[index_parent].chex = true
              }
          }
          this.interpretasi = interpretasi
        },
        saveResult_Lcprep(){
            if(_.isEmpty(this.selected_doctor) || this.selected_doctor.id == '0'){
              this.error_doctor = true

            }else{
              this.$store.commit("re_lcprep/update_onprocess", true)
              let selected_lcprep = this.$store.state.re_lcprep.selected_lcprep
              var prm = {
                doctor:this.selected_doctor.id,
                order_id:selected_lcprep.order_id,
                detail_id:selected_lcprep.id,
                results:this.results,
                interpretasi:this.interpretasi,
                kategoriumum:this.kategoriumum,
                adekuasi:this.adekuasi
              }
              this.$store.dispatch("re_lcprep/saveresult_lcprep",prm)
            }
        }
    }
 
 }
</script>
