<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-divider></v-divider>
            <v-layout class="mb-2" row>
              <v-flex xs6>
                <v-layout align-center row>
                  <v-flex xs12>
                    <h5 class="subheading">ADEKUASI BAHAN PEMERIKSAAN</h5>
                  </v-flex>
                </v-layout>
                <v-layout v-for="chx_adekuasi in adekuasi" align-center row>
                  <v-flex  xs12>
                      <v-checkbox
                        v-model="chx_adekuasi.chex"
                        :label="chx_adekuasi.name" 
                        hide-details
                      ></v-checkbox>
                  </v-flex>
                </v-layout>
              </v-flex>
              <v-flex xs6>
                <v-layout align-center row>
                  <v-flex xs12>
                    <h5 class="subheading">Kategori umum</h5>
                  </v-flex>
                </v-layout>
                <v-layout v-for="chx_kategoriumum in kategoriumum" align-center row>
                  <v-flex  xs12>
                      <v-checkbox
                        v-model="chx_kategoriumum.chex"
                        :label="chx_kategoriumum.name"
                        hide-details
                      ></v-checkbox>
                  </v-flex>
                </v-layout>
              </v-flex>
            </v-layout>
            <v-divider></v-divider>
            <v-layout class="mt-3" row>
              <v-flex v-for="(details,idx) in interpretasi" :class="{'xs3':idx===0,'xs7 pl-3':idx===1,'xs2':idx===2}">
                <h5 class="subheading">{{details.group_name}}</h5>
                <v-layout class="mb-2" v-for="(subgroups,idxx) in details.subgroups" row>
                  <v-flex pr-2 xs12>
                      <p class="mb-0 font-weight-black">{{subgroups.subgroup_name}}</p>
                      <v-layout v-if="subgroups.childs.length > 0" v-for="(childs,idxxx) in subgroups.childs" row>
                        <v-flex :class="{'pl-2':childs.code.length === 2,'pl-4':childs.code.length === 4}" pr-2 xs12>
                            <p class="mb-0" v-if="childs.has_result === 'Y' && childs.type_result === 'CHECK'">
                              <v-checkbox
                                class="lcprep"
                                v-model="childs.chex"
                                :label="childs.name"
                                :disabled="childs.is_parent === 'Y'"
                                @change="changeChildsChex(childs,idxxx,idxx,idx)"
                                hide-details
                              ></v-checkbox>
                            </p>
                            <p class="mb-0" v-if="childs.has_result === 'Y' && childs.type_result === 'TEXT'">
                              <v-text-field
                                label=""
                                outline
                                v-model="childs.value"
                                style="width:100px"
                                box
                                single-line
                              ></v-text-field>
                            </p>
                        </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="blue darken-1" flat @click="dialog_lcprep = false">Tutup</v-btn>
          <v-btn color="blue 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: {
        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{
              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>
