<template>
  <section>
    <nav-bar title="托盘点位绑定"></nav-bar>
    <section class="content mgt186 mgb110">
      <div class="filter-wraper">
        <search-box
        label="托盘条码"
        v-model="val1"
        :seaShow="false"
        ></search-box>
        <div class="bottom-filter-tip">
          <div class="filter-label txtjustify">当前区域</div>
          <div class="fxcol mgl20 visible" >
            <dropdown-menu
            :option="option1"
            :active="active1"
            :open="open1"
            @toggleItem="toggleItem1"
            @dropdownMenu="dropdownMenu1">
            </dropdown-menu>
          </div>
        </div>
        <div class="bottom-filter-tip">
          <div class="filter-label txtjustify">当前点位</div>
          <div class="fxcol mgl20 visible" >
            <dropdown-menu
            :option="option2"
            :active="active2"
            :open="open2"
            @toggleItem="toggleItem2"
            @dropdownMenu="dropdownMenu2">
            </dropdown-menu>
          </div>
        </div>
        <div class="bottom-filter-tip">
          <div class="filter-label txtjustify">点位状态</div>
          <div class="fxcol mgl20 visible" >
            <dropdown-menu
            :option="option3"
            :active="active3"
            :open="open3"
            @toggleItem="toggleItem3"
            @dropdownMenu="dropdownMenu3">
            </dropdown-menu>
          </div>
        </div>
        <div class="bottom-filter-tip">
          <div class="filter-label txtjustify">是否锁定</div>
          <div class="fxcol mgl20 visible" >
            <dropdown-menu
            :option="option4"
            :active="active4"
            :open="open4"
            @toggleItem="toggleItem4"
            @dropdownMenu="dropdownMenu4">
            </dropdown-menu>
          </div>
        </div>
      </div>
    </section>
    <section class="submit-bar">
      <button class="btn submit-button" :class="{'btn-disabled' : active2 === ''}" :disabled="disabled1" @click="_bindingConfirm">确定</button>
      <button class="btn submit-button" @click="toCancle">取消</button>
    </section>
  </section>
</template>

<script>
import NavBar from '@components/NavBar.vue'
import SearchBox from '@components/SearchBox.vue'
import DropdownMenu from '@components/DropdownMenu.vue'
import {queryArea1, queryPointByArea1, bindingConfirm} from '@config/getData2.js'
export default {
  name: 'BindPalletPoint',
  components: {
    NavBar,
    SearchBox,
    DropdownMenu
  },
  data () {
    return {
      val1: '',
      option1: [],
      active1: '',
      open1: false,
      option2: [],
      active2: '',
      open2: false,
      option3: [{value: '00', label: '空位'}, {value: '01', label: '空托盘'}, {value: '02', label: '有箱有料'}],
      active3: '1',
      open3: false,
      option4: [{value: '1', label: '是'}, {value: '2', label: '否'}],
      active4: '1',
      open4: false,
      disabled1: false
    }
  },
  created () {
    this._queryArea1()
  },
  methods: {
    /** 查询区域 */
    async _queryArea1 () {
      let res = await queryArea1()
      if (res.code === '1') {
        this.option1 = [...res.result]
        this.option1.map(el => {
          this.$set(el, 'value', el.area_code)
          this.$set(el, 'label', el.area_name)
        })
      } else {
        this.Dialog(res.desc)
      }
    },
    /** 查询点位 */
    async _queryPointByArea1 (code) {
      let res = await queryPointByArea1(code)
      if (res.code === '1') {
        this.option2 = [...res.result]
        this.option2.map(el => {
          this.$set(el, 'value', el.point_id)
          this.$set(el, 'label', el.point_name)
        })
      } else {
        this.Dialog(res.desc)
      }
    },
    /** 确认 */
    async _bindingConfirm () {
      this.disabled1 = true
      if (this.active2 === '') {
        this.disabled1 = false
        return
      }
      try {
        let res = await bindingConfirm(this.option2[this.active2].point_code, this.val1, this.option3[this.active3].value, this.option4[this.active4].value)
        if (res.code === '1') {
          this.toast(res.desc)
          this.toCancle()
        } else {
          this.Dialog(res.desc)
        }
        this.disabled1 = false
      } catch (e) {
        this.disabled1 = false
      }
    },
    /** 取消 */
    toCancle () {
      this.val1 = ''
      this.active1 = ''
      this.active2 = ''
      this.active3 = '1'
      this.active4 = '1'
      this.disabled1 = false
    },
    toggleItem1 () {
      if (!this.open1) {
        this.open1 = true
      } else {
        this.open1 = false
      }
    },
    dropdownMenu1 (i) {
      this.active1 = i + ''
      this.open1 = false
      this._queryPointByArea1(this.option1[this.active1].area_code)
      this.active2 = ''
    },
    toggleItem2 () {
      if (!this.open2) {
        this.open2 = true
      } else {
        this.open2 = false
      }
    },
    dropdownMenu2 (i) {
      this.active2 = i + ''
      this.open2 = false
    },
    toggleItem3 () {
      if (!this.open3) {
        this.open3 = true
      } else {
        this.open3 = false
      }
    },
    dropdownMenu3 (i) {
      this.active3 = i + ''
      this.open3 = false
    },
    toggleItem4 () {
      if (!this.open4) {
        this.open4 = true
      } else {
        this.open4 = false
      }
    },
    dropdownMenu4 (i) {
      this.active4 = i + ''
      this.open4 = false
    }
  }
}
</script>
