<template>
  <section>
    <nav-bar title="托盘物料绑定"></nav-bar>
    <section class="content mgt186 mgb110">
      <div class="filter-wraper">
        <search-box
        label="托盘条码"
        v-model="val1"
        @handleChange="handleChange1"
        ></search-box>
        <search-box
        label="产品编码"
        v-model="val2"
        :seaShow="false"
        :focused="true"
        ></search-box>
        <div class="bottom-filter-tip">
          <div class="filter-label txtjustify">物料名称</div>
          <div class="fxcol mgl20">
            <input type="text" class="filter-input filter-scan-input" v-model="val3">
          </div>
        </div>
        <div class="bottom-filter-tip">
          <div class="filter-label txtjustify">物料批次</div>
          <div class="fxcol mgl20">
            <input type="text" class="filter-input filter-scan-input" v-model="val4">
          </div>
        </div>
        <div class="bottom-filter-tip">
          <div class="filter-label txtjustify">数量</div>
          <div class="fxcol mgl20">
            <input type="number" class="filter-input filter-scan-input" v-model="val5">
          </div>
        </div>
      </div>
    </section>
    <section class="submit-bar">
      <button class="btn submit-button" :class="{'btn-disabled' : val1 === '' || val2 === '' || val3 === '' || val4 === '' || val5 === ''}" :disabled="disabled" @click="toSure">确定</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 {queryInfoByVehicle, bindingMaterialConfirm} from '@config/getData2.js'
export default {
  name: 'BindMaterPoint',
  components: {
    NavBar,
    SearchBox
  },
  data () {
    return {
      val1: '',
      val2: '',
      val3: '',
      val4: '',
      val5: '',
      result: {},
      disabled: false
    }
  },
  methods: {
    handleChange1 (e, type) {
      if (type) {
        this._queryInfoByVehicle(e)
      }
    },
    /** 查询托盘条码 */
    async _queryInfoByVehicle (e) {
      let res = await queryInfoByVehicle(e)
      if (res.code === '1') {
        this.result = res.result
        this.val2 = res.result.material_code
        this.val3 = res.result.material_name
        this.val4 = res.result.pcsn
        this.val5 = `res.result.parseFloat(qty).toFixed(3)`
      } else {
        this.Dialog(res.desc)
      }
    },
    /** 确认 */
    async toSure () {
      this.disabled = true
      if (this.val1 === '' || this.val2 === '' || this.val3 === '' || this.val4 === '' || this.val5 === '') {
        this.disabled = false
        return
      }
      try {
        let res = await bindingMaterialConfirm(this.result.material_id, this.val2, this.val3, this.val4, this.val5, this.val1)
        if (res.code === '1') {
          this.toast(res.desc)
          this.toCancle()
        } else {
          this.Dialog(res.desc)
        }
        this.disabled = false
      } catch (e) {
        this.disabled = false
      }
    },
    /** 取消 */
    toCancle () {
      Object.assign(this.$data, this.$options.data())
    }
  }
}
</script>
