<template>
   <v-card-text>
      <v-layout column>
         <v-layout row>
            <v-flex xs6 class="pa-0 ma-0" >
               <v-text-field
                  label="Pemeriksaan"
                  placeholder="ketikkan pemeriksaan ..."
                  @change="search"
                  class="ma-0"
                  outline
                  hide-details
                  >
               </v-text-field>
            </v-flex>
               <v-flex xs6 class="pa-3">
                  <v-label >
                     Test Found {{test_count}}, display {{ test_count < 20 ? test_count : display_count  }}
                  </v-label>
               </v-flex>
         </v-layout>
         <v-progress-linear class="ma-0 pa-0" indeterminate :active="is_loading" ></>
      </v-layout>
      <v-container grid-list-xs text-xs-center pa-0>
         <v-layout row wrap>
            <v-btn xs3 v-for="test in tests" :key="test.T_TestPriceID" 
                       :disabled="is_selectPx"
                       @click="selectPx(test)"
                       depressed small :color="px_color(test.px_type)" dark
                       class="mt-1 mr-1 ma-0">
               {{test.T_TestName}}
            </v-btn>
         </v-layout>
      </v-container>
   </v-card-text>
</template>
<script>
let in_selectPx = false
module.exports = {
   methods: {
      search(val) {
         if ( val == this.prev_search ) return
         console.log('Searching',val)
         this.prev_search = val
         this.$store.commit("px/update_search",val)  
         this.$store.dispatch("px/search") 
      },
      
      selectPx(px) {
         try {
            // IF PROFILE
            if (px.px_type == "PR" || px.px_type == "PXR")
               return this.selectProfile(px)

            // SEARCH NAT TEST
            let nt = this.$store.state.px.nat_test
            let found_nt = false
            for (let i in px.nat_test) {
               if (nt.indexOf(px.nat_test[i]) > -1)
                  found_nt = true
            }

            if (found_nt) {
               alert('Pemeriksaan tersebut sudah ada !')
               return
            }

            if (in_selectPx) return
            in_selectPx = true
            let selected_test = this.$store.state.px.selected_test
            let flag_found = false
            selected_test.forEach( function(t,idx) {
               if (t.T_TestID == px.T_TestID) {
                  selected_test[idx] = px 
                  flag_found = true
               }
            })
            if (!flag_found) {
               selected_test.push(px)
               let tests = this.$store.state.px.tests
               let p_idx = -1
               tests.forEach(function(t,idx) {
                  if (t.T_TestID == px.T_TestID) {
                     p_idx = idx
                  }
               })
               if (p_idx >= 0 ) {
                  _.pullAt(tests,[p_idx]) 
                  let dt = {
                     records: tests,
                     total: tests.length
                  }
                  this.$store.commit('px/update_tests',dt)
               }
            }
            this.$store.commit('px/update_selected_test',selected_test)

            let req = px.requirement
            let reqs = this.$store.state.px.requirement
            if (req.length > 0) {
               for(let i in req) {
                  let found = false
                  for(let j in reqs) {
                     if (reqs[j]['req_id'] == req[i]['req_id'])
                        found = j
                  }

                  if (!found)
                     reqs.push({
                        px_id: [px.T_TestID],
                        label: req[i]['req_name'],
                        error_message: 'Hasil harus di isi',
                        is_error: true,
                        checked : false,
                        note: '',
                        req_id: req[i]['req_id']
                     })
                  else
                     reqs[found].px_id.push(px.T_TestID)
               }

               this.$store.commit('px/update_requirement', reqs)
            }

            this.$store.dispatch('px/appx_schedule')

            // if (px.T_TestRequirement != '' ) {
            //    let reqs = this.$store.state.px.requirement
            //    let rst =  _.find(reqs, function(r) { return r.label == px.T_TestRequirement; }) 
            //    if ( rst == undefined ) {
            //       reqs.push({
            //          px_id: px.T_TestID,
            //          label: px.T_TestRequirement,
            //          error_message: 'Hasil harus di isi',
            //          is_error: true,
            //          checked : false,
            //          note: ''
            //       })
            //    }
            //    this.$store.commit('px/update_requirement',reqs)

            //    // Update Janji Hasil
            //    this.$store.dispatch('px/appx_schedule')
            // }

//             checked:false
// error_message:"Hasil harus di isi"
// is_error:true
// label:null
// note:""
// px_id:"969"

            in_selectPx = false
            this.$store.commit('px/update_nat_test')
         } catch(e) {
            console.log(e)
            in_selectPx = false
         }
      },

      selectProfile(px) {
         try {
            // SEARCH NAT TEST
            let nt = this.$store.state.px.nat_test
            let found_nt = false
            for (let i in px.nat_test) {
               if (nt.indexOf(px.nat_test[i]) > -1)
                  found_nt = true
            }

            if (found_nt) {
               alert('Error')
               return
            }

            let pxs = px.child_test
            
            for (let i in pxs) {
               px = pxs[i]
               
               let selected_test = this.$store.state.px.selected_test
               let flag_found = false

               selected_test.push(px)
               let tests = this.$store.state.px.tests
               let p_idx = -1
               tests.forEach(function(t,idx) {
                  if (t.T_TestID == px.T_TestID) {
                     p_idx = idx
                  }
               })
               
               if (p_idx >= 0 ) {
                  _.pullAt(tests,[p_idx]) 
                  let dt = {
                     records: tests,
                     total: tests.length
                  }
                  this.$store.commit('px/update_tests',dt)
               }
               
               this.$store.commit('px/update_selected_test', selected_test)

               let req = px.requirement
               
               let reqs = this.$store.state.px.requirement
               if (req.length > 0) {
                  for(let i in req) {
                     let found = false
                     for(let j in reqs) {
                        if (reqs[j]['req_id'] == req[i]['req_id'])
                           found = j
                     }

                     if (!found)
                        reqs.push({
                           px_id: [px.T_TestID],
                           label: req[i]['req_name'],
                           error_message: 'Hasil harus di isi',
                           is_error: true,
                           checked : false,
                           note: '',
                           req_id: req[i]['req_id']
                        })
                     else
                        reqs[found].px_id.push(px.T_TestID)
                  }

                  this.$store.commit('px/update_requirement', reqs)
                  this.$store.dispatch('px/appx_schedule')
               }
            }

            in_selectPx = false
            this.$store.commit('px/update_nat_test')

         } catch(e) {
            console.log(e)
            in_selectPx = false
         }
      },

      px_color (x) {
         if (x == "PR")
            return "green"
         else if (x == "PN")
            return "orange"
         else
            return "error"
      }
   },
   computed: {
      test_count() {
         return this.$store.state.px.total_test
      },
      display_count() {
         return this.tests.length
      },
      tests() {
         return this.$store.state.px.tests
      },
      is_selectPx() {
         return in_selectPx
      },
      is_loading() {
         return this.$store.state.px.search_status == 1
      }
   },
   data: function(){
      return {
         prev_search : ''
      }
   }
}
</script>
