<template>
  
    <v-dialog
      v-model="dialog"
      :width="this.requirements.length <= 5 ? 500 : 900"
    >
      

      <v-card>
        <v-card-title
          class="headline grey lighten-2 pt-2 pb-2"
          primary-title
        >
          Sample Verification
        </v-card-title>

        <v-card-text class="pt-2 pb-2">
          <v-layout row wrap>
            <v-flex v-bind:class="this.requirements.length <= 5 ? 'xs12' : 'xs6'">
              <v-layout row wrap>
                <v-flex xs12 v-for="(req, i) in reqs_odd" v-bind:key="i">
                  <v-checkbox :label="req.req_name" v-model="selected_requirements" :value="req.req_id"
                    hide-details
                    class="mt-2"></v-checkbox>
                </v-flex>
              </v-layout>
            </v-flex>

            <v-flex xs6 v-if="this.requirements.length > 5">
              <v-layout row wrap>
                <v-flex xs12 v-for="(req, i) in reqs_even" v-bind:key="i">
                  <v-checkbox :label="req.req_name" v-model="selected_requirements" :value="req.req_id"
                    hide-details
                    class="mt-2"></v-checkbox>
                </v-flex>
              </v-layout>
            </v-flex>
          </v-layout>

          <v-layout row wrap>
            <v-flex xs12 pt-4>
              <v-textarea
                auto-grow
                label="Catatan"
                rows="3"
                v-model="note"
                outline
              ></v-textarea>
            </v-flex>
          </v-layout>

        </v-card-text>
        <v-divider></v-divider>

        <v-card-actions>
            <v-btn
            color="primary"
            flat
            @click="dialog = false"
          >
            Tutup
          </v-btn>
          <v-spacer></v-spacer>
          <v-btn
            color="red"
            @click="save_reject"
            :disabled="selected_requirements.length < 1"
            :dark="selected_requirements.length > 0"
          >
            Tolak
          </v-btn>
          <v-btn
            color="primary"
            @click="save_process"
            :disabled="selected_requirements.length < 1"
            :dark="selected_requirements.length > 0"
          >
            Proses
          </v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
  
</template>

<style scoped>
</style>

<script>
module.exports = {
    components : {
    },
    methods : {
       save_reject () {
         this.$store.dispatch('verification_patient/reject')
          //  this.$store.dispatch('verification_patient/save_requirement', "N")
       },

       save_process () {
         this.$store.dispatch('verification_patient/verify')
       }
    },
    computed : {
       dialog: {
          get() {
             return this.$store.state.verification_patient.dialog_requirement;
          },
          set(val) {
             this.$store.commit('verification_patient/update_dialog_requirement', val);
          }
       },

       requirements () {
         if (this.$store.state.verification_patient.selected_patient.reqs)
          return this.$store.state.verification_patient.selected_patient.reqs

        return []
       },

       selected_requirements : {
         get () {
           return this.$store.state.verification_patient.selected_requirements
         },
         set (v) {
           this.$store.commit('verification_patient/update_selected_requirements', v)
         }
       },

        note : {
            get () {
                let x = this.$store.state.verification_patient.selected_patient.data
                if (typeof x == "undefined")
                    return ""

                return x.note
            },
            set (v) {
                this.$store.commit('verification_patient/update_verification_note', v)
            }
        },

        reqs_odd () {
          if (!this.requirements)
            return []

          if (this.requirements.length <= 5)
            return this.requirements

          if (this.requirements <= 10)
            return this.requirements.slice(0,4)
          
          else {
            let x = Math.ceil(this.requirements.length / 2)
            return this.requirements.slice(0, x-1)
          }
        },

        reqs_even () {
          if (!this.requirements)
            return []

          if (this.requirements.length <= 5)
            return []

          if (this.requirements <= 10)
            return this.requirements.slice(5, this.requirements-1)
          
          else {
            let x = Math.ceil(this.requirements.length / 2)
            return this.requirements.slice(x, this.requirements.length-1)
          }
        }
    },

    mounted () {
      this.$store.dispatch('verification_patient/get_requirement')
    },

    watch : {
      dialog(n, o) {
        let x = this.$store.state.verification_patient.reqs
        if (n && !o)
          this.$store.commit('verification_patient/update_selected_requirements', x)
      }
    }
}
</script>
