<template>
  <section>
    <nav-bar title="呼叫次品料"></nav-bar>
    <section class="content mgt86">
      <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>
    </section>
    <section class="submit-bar">
      <button class="btn submit-button" :class="{'btn-disabled' : val1 === ''}" :disabled="disabled1" @click="_callDefective">确定</button>
      <button class="btn submit-button" @click="toCancle">取消</button>
    </section>
  </section>
</template>

<script>
import NavBar from '@components/NavBar.vue'
import DropdownMenu from '@components/DropdownMenu.vue'
import {pdaDevice, callDefective} from '@config/getData2.js'
export default {
  name: 'CallDefective',
  components: {
    NavBar,
    DropdownMenu
  },
  data () {
    return {
      option1: [],
      optionNew1: [],
      active1: '',
      open1: false,
      val1: '',
      disabled1: false
    }
  },
  created () {
    this._pdaDevice()
  },
  methods: {
    /** 查询设备 */
    async _pdaDevice () {
      let res = await pdaDevice('call_defective')
      if (res.code === '1') {
        this.option1 = [...res.result]
        this.option1.map(el => {
          this.$set(el, 'value', el.device_code)
          this.$set(el, 'label', el.device_name)
        })
      } else {
        this.Dialog(res.desc)
      }
    },
    /** 确认 */
    async _callDefective () {
      this.disabled1 = true
      if (this.val1 === '') {
        this.disabled1 = false
        return
      }
      let code = ''
      this.option1.map(el => {
        if (el.device_name === this.val1) {
          code = el.device_code
        }
      })
      try {
        let res = await callDefective(code)
        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.active1 = ''
      this.val1 = ''
      this.disabled1 = false
    },
    /** 模糊匹配 */
    selectMatchItem (lists, keyWord) {
      let resArr = []
      lists.filter((item) => {
        if (item.device_name.indexOf(keyWord) > -1) {
          resArr.push(item)
        }
      })
      return resArr
    },
    handleChange1 (e) {
      if (!e) {
        this.active1 = ''
      }
      this.optionNew1 = []
      this.optionNew1 = this.selectMatchItem(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
      }
    }
  }
}
</script>
