<template>
  <section>
    <nav-bar title="载具绑定"></nav-bar>
    <section class="content mgt186">
      <div class="filter-wraper">
        <div class="bottom-filter-tip">
          <div class="filter-label txtjustify">区域</div>
          <div class="fxcol mgl20 visible" >
            <dropdown-menu
              :option="optionNew1"
              :active="active1"
              :open="open1"
              :inputed="true"
              v-model="val1"
              @handleBlur="handleBlur1"
              @handleChange="handleChange1"
              @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="optionNew2"
            :active="active2"
            :open="open2"
            :inputed="true"
            v-model="val2"
            @handleBlur="handleBlur2"
            @handleChange="handleChange2"
            @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="option4"
            :active="active4"
            :open="open4"
            @toggleItem="toggleItem4"
            @dropdownMenu="dropdownMenu4">
            </dropdown-menu>
          </div>
        </div>
        <div v-show="!show" class="bottom-filter-tip">
          <div class="filter-label txtjustify">载具类型</div>
          <div class="fxcol mgl20 visible" >
            <dropdown-menu
            :option="optionNew3"
            :active="active3"
            :open="open3"
            :inputed="true"
            v-model="val3"
            @handleBlur="handleBlur3"
            @handleChange="handleChange3"
            @toggleItem="toggleItem3"
            @dropdownMenu="dropdownMenu3">
            </dropdown-menu>
          </div>
        </div>
        <search-box
        v-show="!show"
        label="载具"
        v-model="val4"
        :seaShow="false"
        ></search-box>
      </div>
    </section>
    <section class="submit-bar">
      <button class="btn submit-button" :class="{'btn-disabled' : ((val2 === '' || val3 === '' || val4 === '' || active4 === '') && show === false) || (show === true && val2 === '')}" :disabled="disabled1" @click="_vehicleBind">确定</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 {pdaRegion, pdaPoint, pdapointStatus, pdavehicleType, vehicleBind} from '@config/getData2'
export default {
  name: 'VehicleBind',
  components: {
    NavBar,
    DropdownMenu,
    SearchBox
  },
  data () {
    return {
      option1: [],
      optionNew1: [],
      active1: '',
      open1: false,
      val1: '',
      option2: [],
      optionNew2: [],
      active2: '',
      open2: false,
      val2: '',
      option3: [],
      optionNew3: [],
      active3: '',
      open3: false,
      val3: '',
      disabled1: false,
      val4: '',
      option4: [],
      active4: '',
      open4: false,
      show: false
    }
  },
  created () {
    this._pdaRegion()
    this._pdavehicleType()
    this._pdapointStatus()
  },
  methods: {
    /** 查询点位状态 */
    async _pdapointStatus () {
      let res = await pdapointStatus()
      if (res.code === '1') {
        this.option4 = [...res.result]
      } else {
        this.Dialog(res.desc)
      }
    },
    /** 查询载具类型 */
    async _pdavehicleType () {
      let res = await pdavehicleType('vehicle_bind')
      if (res.code === '1') {
        this.option3 = [...res.result]
        this.option3.map(el => {
          this.$set(el, 'value', el.value)
          this.$set(el, 'label', el.label)
        })
      } else {
        this.Dialog(res.desc)
      }
    },
    /** 查询区域 */
    async _pdaRegion () {
      let res = await pdaRegion('vehicle_bind')
      if (res.code === '1') {
        this.option1 = [...res.result]
        this.option1.map(el => {
          this.$set(el, 'value', el.region_code)
          this.$set(el, 'label', el.region_name)
        })
      } else {
        this.Dialog(res.desc)
      }
    },
    /** 模糊匹配 */
    selectMatchItem (type, lists, keyWord) {
      let resArr = []
      lists.filter((item) => {
        if (type === '1' && item.region_name.indexOf(keyWord) > -1) {
          resArr.push(item)
        }
        if (type === '2' && item.point_name.indexOf(keyWord) > -1) {
          resArr.push(item)
        }
        if (type === '3' && item.label.indexOf(keyWord) > -1) {
          resArr.push(item)
        }
      })
      return resArr
    },
    handleChange1 (e) {
      if (!e) {
        this.active1 = ''
      }
      this.optionNew1 = []
      this.optionNew1 = this.selectMatchItem('1', this.option1, e)
      this.open1 = true
    },
    handleBlur1 () {
      this.open1 = false
      this.val1 = ''
    },
    toggleItem1 () {
      if (!this.open1) {
        this.optionNew1 = this.option1
        this.active1 = ''
        this.optionNew1.map((e, i) => {
          if (e.label === this.val1) {
            this.active1 = i + ''
          }
        })
        this.open1 = true
      } else {
        this.open1 = false
      }
    },
    dropdownMenu1 (i) {
      this.active1 = i + ''
      this.open1 = false
      if (this.optionNew1.length > 0) {
        this.val1 = this.optionNew1[i].label
        this.val2 = ''
        this.active2 = ''
        this._pdaPoint(this.optionNew1[i].value)
      }
    },
    /** 查询点位 */
    async _pdaPoint (code) {
      let res = await pdaPoint('vehicle_bind', code)
      if (res.code === '1') {
        this.option2 = [...res.result]
        this.option2.map(el => {
          this.$set(el, 'value', el.point_code)
          this.$set(el, 'label', el.point_name)
        })
      } else {
        this.Dialog(res.desc)
      }
    },
    handleChange2 (e) {
      if (!e) {
        this.active2 = ''
      }
      this.optionNew2 = []
      this.optionNew2 = this.selectMatchItem('2', this.option2, e)
      this.open2 = true
    },
    handleBlur2 () {
      this.open2 = false
      this.val2 = ''
    },
    toggleItem2 () {
      if (!this.open2) {
        this.optionNew2 = this.option2
        this.active2 = ''
        this.optionNew2.map((e, i) => {
          if (e.label === this.val2) {
            this.active2 = i + ''
          }
        })
        this.open2 = true
      } else {
        this.open2 = false
      }
    },
    dropdownMenu2 (i) {
      this.active2 = i + ''
      this.open2 = false
      if (this.optionNew2.length > 0) {
        this.val2 = this.optionNew2[i].label
      }
    },
    handleChange3 (e) {
      if (!e) {
        this.active3 = ''
      }
      this.optionNew3 = []
      this.optionNew3 = this.selectMatchItem(this.option3, e)
      this.open3 = true
    },
    handleBlur3 () {
      this.open3 = false
      this.val3 = ''
    },
    toggleItem3 () {
      if (!this.open3) {
        this.optionNew3 = this.option3
        this.active3 = ''
        this.optionNew3.map((e, i) => {
          if (e.label === this.val3) {
            this.active3 = i + ''
          }
        })
        this.open3 = true
      } else {
        this.open3 = false
      }
    },
    dropdownMenu3 (i) {
      this.active3 = i + ''
      this.open3 = false
      if (this.optionNew3.length > 0) {
        this.val3 = this.optionNew3[i].label
      }
    },
    toggleItem4 () {
      if (!this.open4) {
        this.open4 = true
      } else {
        this.open4 = false
      }
    },
    dropdownMenu4 (i) {
      this.active4 = i + ''
      if (this.option4[i].value === '1') {
        this.show = true
      } else {
        this.show = false
      }
      this.open4 = false
    },
    /** 确认 */
    async _vehicleBind () {
      this.disabled1 = true
      if (((this.val2 === '' || this.val3 === '' || this.val4 === '' || this.active4 === '') && this.show === false) || (this.show === true && this.val2 === '')) {
        this.disabled1 = false
        return
      }
      try {
        let code = ''
        this.option2.map(el => {
          if (el.point_name === this.val2) {
            code = el.point_code
          }
        })
        let code3 = ''
        if (this.show === false) {
          this.option3.map(el => {
            if (el.label === this.val3) {
              code3 = el.value
            }
          })
        }
        if (this.show === true) {
          this.val4 = ''
        }
        let res = await vehicleBind(code, code3, this.val4, 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.val2 = ''
      this.val3 = ''
      this.active1 = ''
      this.active2 = ''
      this.active3 = ''
      this.disabled1 = false
      this.val4 = ''
      this.active4 = ''
      this.show = false
    }
  }
}
</script>
