<template>
    <v-dialog
      v-model="dialog"
      width="500px"
    >
      <v-card>
        <v-card-title
          class="headline grey lighten-2 pt-2 pb-2"
          primary-title
        >
          CETAK BARCODE
        </v-card-title>
        <v-card-text class="pt-2 pb-2">

          <v-layout>
              <v-flex xs12>
                  <v-data-table :items="samples" 
                    
                    hide-actions class="elevation-1">

                    <template slot="headers" slot-scope="props">
                        <tr>
                            <th width="10%" class="text-xs-left pa-2 primary white--text">
                                <v-checkbox
                                    primary
                                    hide-details
                                    color="white"
                                    dark
                                    input-value="N"
                                    true-value="Y"
                                    false-value="N"
                                    @change="selectAllBarcode"
                                ></v-checkbox>
                            </th>
                            <th width="50%" class="text-xs-left pa-2 primary white--text">
                                NAMA SAMPEL
                            </th>
                            <th width="25%" class="text-xs-left pa-2 primary white--text">
                                NO BARCODE
                            </th>
                            <th width="15%" class="pa-2 primary white--text">
                                CETAK
                            </th>
                        </tr>
                    </template>

                    <template slot="no-data">
                        <v-alert :value="true" color="error" icon="warning">
                        Data Pasien tidak di temukan
                        
                        </v-alert>
                    </template>

                    <template slot="items" slot-scope="props">
                        <td class="text-xs-left pa-2" v-bind:class="is_selected(props.item)" @click="selectMe(props.item)">
                            <v-checkbox v-model="selected_barcodes" :value="props.item.barcode" hide-details></v-checkbox>    
                        </td>
                        <td class="text-xs-left pa-2" v-bind:class="is_selected(props.item)" @click="selectMe(props.item)">{{ props.item.sample_name }}</td>
                        <td class="text-xs-left pa-2" v-bind:class="is_selected(props.item)" @click="selectMe(props.item)">{{ props.item.barcode }}</td>
                        <td class="text-xs-center pa-2" v-bind:class="is_selected(props.item)" @click="selectMe(props.item)">
                            <v-btn color="orange" class="one-btn-icon mt-0 mb-0" dark @click="printOne(props.item.barcode)"><span class="icon-print"></span></v-btn>
                        </td>
            
                    </template>
                
                </v-data-table>
              </v-flex>
          </v-layout>
          
        </v-card-text>
        <v-divider></v-divider>

        <v-card-actions>
            <v-btn
                color="primary"
                @click="dialog = false"
                flat
            >
                Tutup
            </v-btn>

          <v-spacer></v-spacer>
            <v-btn
                color="orange"
                @click="printMultiple"
                :dark="selected_barcodes.length > 0"
                :disabled="selected_barcodes < 1"
                class="mr-2"
            >
            <span class="icon-print"></span>&nbsp;
            Cetak Terpilih ({{ selected_barcodes.length }})
            </v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>
  
</template>

<style scoped>
</style>

<script>
module.exports = {
    components : {
    },
    data () {
        return {}
    },

    methods : {
        selectMe (x) {
            return
        },

       is_selected (x) {
         return
       },

       selectBarcode (x, e) {
            let y = this.$store.state.barcode.selected_barcodes
            y[x] = e
            this.$store.commit('barcodev1/update_selected_barcodes', y)
       },

       selectAllBarcode (e) {
            let y = []
            if (e == "Y") {
                let x = this.$store.state.barcodev1.samples
            
                for (let i in x)
                    y.push(x[i].barcode)
            }
            
            this.$store.commit('barcodev1/update_selected_barcodes', y)
       },

        printOne(x) {
            return window.one_print_barcode_pk(x)
        },

        printMultiple () {
            let y = this.$store.state.barcodev1.selected_barcodes
            return window.one_print_barcode_pk(y.join(','))
        }
    },
    computed : {
       dialog: {
          get() {
             return this.$store.state.barcodev1.dialog_barcode;
          },
          set(val) {
             this.$store.commit('barcodev1/update_dialog_barcode', val);
          }
       },

       samples () {
           return this.$store.state.barcodev1.samples
       },

       selected_barcodes : {
           get () {
               return this.$store.state.barcodev1.selected_barcodes
           },
           set (v) {
               this.$store.commit('barcodev1/update_selected_barcodes', v)
           }
       }
    },

    watch : {
        dialog (n, o) {
            if (n && !o) {
                // let x = this.$store.state.sampling.samples
                let y = []
                // for (let i in x)
                //     y.push(x[i].barcode)
                this.$store.commit('barcodev1/update_selected_barcodes', y)
            }
        }
    }
}
</script>
