<template>
  <v-layout>
    <v-flex xs12>
      <v-card class="mb-2" color="white">
        <v-toolbar color="blue lighten-2" dark height="50px">
          <v-toolbar-title class="white--text">Pemeriksaan untuk {{xcourier.name}}</v-toolbar-title>
          <v-spacer></v-spacer>
          <v-btn @click="openFormCourierArea()" icon>
            <v-icon class="white--text">library_add</v-icon>
          </v-btn>
        </v-toolbar>

        <v-layout row wrap>
          <v-flex class="border-bottom-dashed" xs12 pt-2 pl-4 pr-4 pb-4>

            <v-layout xs12 row wrap>
              <v-flex xs4 v-for="(vst,idx) in vareas" :key="vst.Nat_AreaID" class="pr-2 pb-1">
                <v-layout row>
                  <v-flex class="boxoutline" style="text-overflow:ellipsis;overflow:hidden;" xs11>
                    <span>{{vst.Nat_AreaName}}</span>
                  </v-flex>
                  <v-flex class="boxoutline" class="text-center" style="padding-top:10px" pl-2 pb-2 pr-2 xs2>
                    <v-layout row align-center justify-space-between>
                      <v-icon style="color:red" @click="deleteArea(vst.Nat_AreaID,vst.Nat_AreaName)">clear</v-icon>
                    </v-layout>
                  </v-flex>
                </v-layout>
              </v-flex>
            </v-layout>

          </v-flex>
        </v-layout>

        <template>
          <v-layout row justify-center>
            <v-dialog v-model="dialogcourierarea" persistent max-width="600px">
              <v-card>
                <v-card-title>
                  <span class="headline">Form Courier Area</span>
                </v-card-title>
                <v-card-text class="pt-0 pb-0">
                  <v-form ref="formcourierarea" v-model="validarea" lazy-validation>
                    <v-layout wrap>
                      <v-flex xs12>
                        SCHEDULE : {{xcourier.name}}
                      </v-flex>
                      <v-flex xs12>
                        <v-autocomplete label="Area" v-model="area" :items="xareas" :search-input.sync="search_area" auto-select-first no-filter
                          item-text="Nat_AreaName" return-object :loading="isLoading" no-data-text="Pilih Area">
                          <template slot="item" slot-scope="{ item }">
                            <v-list-tile-content>
                              <v-list-tile-title v-text="item.Nat_AreaName"></v-list-tile-title>
                            </v-list-tile-content>
                          </template>
                        </v-autocomplete>
                      </v-flex>
                    </v-layout>
                </v-card-text>
                <v-card-actions>
                  <v-spacer></v-spacer>
                  <v-btn color="blue darken-1" flat @click="updateDialogFormCourierArea()">Tutup</v-btn>
                  <v-btn color="blue darken-1" flat @click="saveFormCourierArea()">Simpan</v-btn>
                </v-card-actions>
                </v-form>
              </v-card>
            </v-dialog>
          </v-layout>
        </template>

      </v-card>
    </v-flex>
  </v-layout>
</template>

<style scoped>
  .searchbox .v-input.v-text-field .v-input__slot {
    min-height: 60px;
  }

  .searchbox .v-btn {
    min-height: 60px;
  }

  .boxoutline {
    color: teal;
    border: 1px solid teal;
    justify-content: center;
    height: 45px;
    line-height: 45px;
    padding-left: 10px;
    background: #ffffff;
    font-size: 14px;
    font-weight: 500;
    border-radius: 1px
  }

  .boxoutline:hover {
    background: rgba(0, 0, 0, 0.07) !important;
    font-size: 15px;
    font-weight: 700;
  }


  table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
    background: white;
    border: 0px;
  }

  th,
  td {
    border: 1px solid black;
    border-collapse: collapse;
    padding-top: 2px;
    padding-bottom: 2px;
  }

  table>tr>td {
    padding: 8px;
  }

  table>tr>td:first {
    padding-left: 15px !important;
  }
</style>

<script>
  module.exports = {
    data() {
      return {
        isLoading: false,
        validarea: false,
        search_area: '',
        items: [],
        msgalertmou: ""
      };
    },
    computed: {
      selected: {
        get() {
          return this.$store.state.area.selected
        }
      },
      xcourier() {
        return this.$store.state.courier.selected_courier
      },
      dialogcourierarea() {
        return this.$store.state.area.dialog_form_courier_area
      },
      xareas() {
        return this.$store.state.area.areas
      },
      area: {
        get() {
          return this.$store.state.area.area
        },
        set(val) {
          this.$store.commit("area/update_area", val)
        }
      },
      vareas() {
        return this.$store.state.area.selected
      }
    },
    methods: {
      isSelected(id) {
        let vals = this.$store.state.area.selected
        if (vals.indexOf(id) > -1) return true
        return false
      },
      async deleteArea(id, name) {
        let courierid = this.$store.state.courier.selected_courier.id
        await this.$store.dispatch("area/delete", {
          id: id,
          name: name,
          courierid: courierid
        })
        await this.$store.dispatch("area/selected", courierid)
      },
      updateDialogFormCourierArea() {
        this.$store.commit("area/update_dialog_form_courier_area", false)
      },
      thr_search_area: _.debounce(function () {
        this.$store.dispatch("area/searcharea", {search: this.search_area, couriergroup_id :this.$store.state.courier.selected_courier.id})
      }, 2000),
      openFormCourierArea(val) {
        //console.log("masuk")
        this.xid = val
        this.search_area = ''
        this.$store.commit("area/update_areas", [])
        this.area = {}
        this.$refs.formcourierarea.reset()
        this.$refs.formcourierarea.resetValidation()
        this.$store.commit("area/update_dialog_form_courier_area", true)
      },
      saveFormCourierArea() {
        if (this.$refs.formcourierarea.validate()) {
          this.$store.dispatch("area/save", {
            xid: this.xid,
            courierid: this.$store.state.courier.selected_courier.id,
            couriername: this.$store.state.courier.selected_courier.name,
            areaid: this.area.Nat_AreaID
          })
        }
      },
      updateAlert_success(val) {
        this.$store.commit("courier/update_alert_success", val)
      }
    },
    watch: {
      search_area(val, old) {
        if (val == old) return
        if (!val) return
        if (val.length < 1) return
        if (this.$store.state.area.update_autocomplete_status == 1) return
        this.thr_search_area()
      }
    }
  }
</script>