<template>
   <v-layout column>
      <v-dialog
         v-model="dialog_delivery_note"
         width="500"
      >

         <v-card>
            <v-card-title
               class="headline blue lighten-2 white--text"
            >
               Catatan Pengiriman
            </v-card-title>

         <v-card-text>
            <v-layout row>
            <v-flex d-flex>
               <v-textarea
                  outline
                  label="Isikan Catatan"
                  v-model="selected_delivery_for_note.description"
               ></v-textarea>
            </v-flex>
            </v-layout>
            <v-layout row>
               <v-flex d-flex>
                  <p class="red--text font-weight-bold caption">
                     Untuk pengiriman email, penambahan catatan digunakan sebagai pengganti alamat email terpilih, 
                     jika lebih dari satu, 
                     tulis alamat email dengan separator koma
                     contoh : aku@gmail.com, kamu@gmail.com, dia@gmail.com
                  </p>
               </v-flex>
            </v-layout>
            <v-layout row>
               <v-flex d-flex>
                  <p class="red--text font-weight-bold caption">
                     Untuk pengiriman kurir, penambahan catatan digunakan sebagai keterangan
                     dan tidak merubah kecamatan kelurahan,
                     ketika proses spk kurir
                  </p>
               </v-flex>
            </v-layout>
            
        </v-card-text>

         <v-divider></v-divider>

         <v-card-actions>
            <v-spacer></v-spacer>
            <v-btn
               color="primary"
               flat
               @click="addDeliveryNoteNew()"
            >
               Tambahkan
            </v-btn>
         </v-card-actions>
         </v-card>
      </v-dialog>
      <v-layout align-center row>
         <v-flex xs12>
            <v-card tile flat color=" pa-2 red lighten-4">
               <h5 class=" subtitle-1 font-weight-bold">PENGIRIMAN HASIL</h5>
            </v-card>
         </v-flex>
      </v-layout>
      
         <v-layout mt-1 row wrap >
            <v-flex class="row" ma-1 xs11 v-for="(delivery,idx) in deliveries">
                  <v-layout  align-center row>
                     <v-flex  xs1>
                        <v-btn v-if="delivery.chex  === 'N'" @click="changeBoxRight(delivery,idx)" dark style="min-width:20px" small color="grey darken-1"><v-icon small>close</v-icon></v-btn>
                        <v-btn v-if="delivery.chex  === 'Y'" @click="changeBoxRight(delivery,idx)" depressed style="min-width:20px" small color="red lighten-4"><v-icon color="red" small>check</v-icon></v-btn>
                     </v-flex>
                     <v-flex mb-1 pl-2 xs11>
                        <p :class="getColorFont(delivery)" 
                           class="mb-0 font-weight-black"
                           style="font-size:11px">
                           {{delivery.delivery_name.toUpperCase()}}
                        </p>
                        <div  v-if="delivery.description !== ''" 
                           class="body-2 mb-1 mono caption" 
                           v-html="reHTMLAddress(delivery.description)">
                        </div>
                        <v-textarea
                           v-if="delivery.chex === 'Y'"
                           v-model="delivery.note"
                           rows="1"
                           auto-grow
                           single-line
                           style="color:#800000;font-weight:500"
                           placeholder="Catatan"
                           hide-details
                           class="body-2 mb-1 mt-0 pt-0"
                        ></v-textarea>
                     </v-flex>
                  </v-layout>
            </v-flex >
         </v-layout>

   </v-layout>

</template>

<script>
module.exports = {
   data () {
      return {
          dialog_delivery_note:false,
          selected_delivery_for_note:{}
       }
    },

   methods: {
      reHTMLAddress(entry){
         var rtn = ''
         if(entry){
            var perpart = entry.split("^")
         rtn += "<p class='mb-0' style='font-size:12px'>"+perpart[0]+"</p>"
         if(perpart[1])
            rtn += "<p class='mb-0' style='font-size:12px'>"+perpart[1]+"</p>"
         }
         

         
         return rtn
      },
      changeBoxRight(value,idx){
            var deliveries = this.$store.state.delivery.data_deliveries
            console.log(deliveries)
            deliveries[idx].chex = value.chex === 'N'?'Y':'N'
            console.log(deliveries[idx].chex)
            this.$store.commit("delivery/update_data_deliveries",deliveries)
        },
        getColorFont(value,type){
            
            
            var xcolor = 'black--text'
            if(value.chex === 'Y'){
               xcolor = 'red--text'
            }
            
            
            return xcolor
        },
      updateSelected(idx,val) {
         console.log('idx:'+idx)
         console.log('val:'+val)
         var deliveries = this.$store.state.delivery.deliveries
         deliveries[idx].selected = val
         this.$store.commit("delivery/update_deliveries",deliveries)
      },
      updateDelivery(idx,val) {
         var deliveries = this.$store.state.delivery.deliveries
         deliveries[idx].note = val
         this.$store.commit("delivery/update_deliveries",deliveries)
      },
      doAction(idx,val){
         if(val.typeform === 'md'){
            let dlv = this.$store.state.delivery.checked_id
            dlv.push(val.idx)
            this.$store.commit("delivery/update_checked_id",dlv)
            this.selected_delivery_for_note = _.clone(val, true)
            
            if(val.code == 'EMAIL')
               this.selected_delivery_for_note.note = val.note
            else
               this.selected_delivery_for_note.note = ""

            this.dialog_delivery_note = true
         }
         else{
            var deliveries = this.$store.state.delivery.deliveries
            deliveries.splice(idx, 1)
            this.$store.commit("delivery/update_deliveries",deliveries)
         }
         
      },
      doActionNew(idx,val){
         if(val.typeform === 'origin'){
            this.selected_delivery_for_note = _.clone(val, true)
            var deliveries = this.$store.state.delivery.data_deliveries
            deliveries[idx].chex = true
            this.$store.commit("delivery/update_data_deliveries",deliveries)
            if(val.delivery_code !== 'EMAIL')
               this.selected_delivery_for_note.description = ""

            this.dialog_delivery_note = true
         }
         else{
            var deliveries = this.$store.state.delivery.data_deliveries
            deliveries.splice(idx, 1)
            this.$store.commit("delivery/update_data_deliveries",deliveries)
         }
         
      },
      addDeliveryNote(){
         var xdt = this.selected_delivery_for_note
         let dlv = this.$store.state.delivery.checked_id
         
         dlv.push(xdt.idx)
         this.$store.commit("delivery/update_checked_id",dlv)
         
         xdt.typeform = 'note'
         xdt.id = Date.now()
         xdt.selected = xdt.idx
         var deliveries = this.$store.state.delivery.deliveries
         deliveries.push(xdt)
         this.$store.commit("delivery/update_deliveries",deliveries)
         this.dialog_delivery_note = false
      },
      addDeliveryNoteNew(){
         var xdt = this.selected_delivery_for_note
         xdt.typeform = 'additional'
         xdt.chex = true
         var deliveries = this.$store.state.delivery.data_deliveries
         deliveries.push(xdt)
         this.$store.commit("delivery/update_data_deliveries",deliveries)
         this.dialog_delivery_note = false
      }
   },
   computed: {
      deliveries() {
         return this.$store.state.delivery.data_deliveries
      },

      checked_id : {
         get() {
            return this.$store.state.delivery.checked_id
         },
         set(val) {
            console.log(val)
            this.$store.commit("delivery/update_checked_id", val)
            this.$store.commit("delivery/update_deliveries_2")
         }
      }
   },
   mounted: function() {
      this.$store.dispatch('delivery/search')
   }
}
</script>
