<template>
  <section>
    <nav-bar title="盘点"></nav-bar>
    <section class="content">
      <div class="filter-wraper">
        <search-box
        ref="myInput1"
        label="托盘码"
        v-model="val1"
        @handleChange="handleChange1"
        ></search-box>
        <search-box
        ref="myInput2"
        label="物料条码"
        v-model="val2"
        @handleChange="handleChange2"
        ></search-box>
      </div>
      <div class="grid-wraper">
        <div class="left_fixed">
          <table class="layout-t left_layout_t">
            <tr>
              <th>单据编号</th>
            </tr>
            <tr v-for="(e, i) in dataList" :key="i">
              <td>{{e.BillID}}</td>
            </tr>
          </table>
        </div>
        <div class="slide">
          <table class="layout-t">
            <tr>
              <th>物料编码</th>
              <th>物料名称</th>
              <th>批次</th>
              <th>库存数量</th>
              <th>盘点数量</th>
            </tr>
            <tr v-for="(e, i) in dataList" :key="i">
              <td>{{e.MatCode}}</td>
              <td>{{e.MatName}}</td>
              <td>{{e.BatchNumber}}</td>
              <td>{{e.MatNum}}</td>
              <td>{{e.FactNumOut}}</td>
            </tr>
          </table>
        </div>
      </div>
    </section>
    <section class="submit-bar">
      <button class="btn submit-button" :class="{'btn-disabled' : !val1 || !val2}" :disabled="disabled" @click="_RFStorageInventory">盘点</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 {RFReadTrayStateInventory, RFReadTrayStorageInventory, RFReadMatBarCodeStateInventory, RFStorageInventory} from '@config/getData1'
export default {
  name: 'PickGoods',
  components: {
    NavBar,
    SearchBox
  },
  data () {
    return {
      val1: '',
      val2: '',
      dataList: [],
      disabled: false
    }
  },
  mounted () {
    this.$refs.myInput1.handleScan()
  },
  methods: {
    /** 读取托盘码 */
    async _RFReadTrayStateInventory (val) {
      try {
        let res = await RFReadTrayStateInventory(val)
        if (res.ErrNO === '1') {
          this._RFReadTrayStorageInventory()
        } else {
          this.toast(res.ErrMsg)
          this.val1 = ''
          this.$refs.myInput1.handleScan()
        }
      } catch (e) {
        this.val1 = ''
        this.$refs.myInput1.handleScan()
      }
    },
    /** 托盘待盘点信息 */
    async _RFReadTrayStorageInventory () {
      let res = await RFReadTrayStorageInventory(this.val1)
      if (res.ErrNO === '1') {
        this.dataList = [...res.StorageList]
        this.val2 = ''
        this.$refs.myInput2.handleScan()
      } else {
        this.toast(res.ErrMsg)
        this.val1 = ''
        this.$refs.myInput1.handleScan()
      }
    },
    /** 读取物料条码 */
    async _RFReadMatBarCodeStateInventory (val) {
      try {
        let res = await RFReadMatBarCodeStateInventory(this.val1, val)
        if (res.ErrNO === '1') {
          this._RFStorageInventory()
        } else {
          this.toast(res.ErrMsg)
          this.val2 = ''
          this.$refs.myInput2.handleScan()
        }
      } catch (e) {
        this.val2 = ''
        this.$refs.myInput2.handleScan()
      }
    },
    handleChange1 (e, type) {
      if (type && e.substring(0, 2) !== 'ZD') {
        this.$refs.myInput1.handleScan()
        setTimeout(() => {
          this.val1 = ''
        }, 200)
        return
      }
      if (type) {
        this._RFReadTrayStateInventory(e)
      }
    },
    handleChange2 (e, type) {
      if (type && e.substring(0, 2) === 'ZD') {
        this.val1 = e
        setTimeout(() => {
          this.val2 = ''
        }, 200)
        this._RFReadTrayStateInventory(this.val1)
        return
      }
      if (type && this.val1 === '' && e.substring(0, 2) !== 'ZD') {
        this.$refs.myInput1.handleScan()
        setTimeout(() => {
          this.val2 = ''
        }, 200)
        return
      }
      if (type && this.val1 !== '') {
        this._RFReadMatBarCodeStateInventory(e)
      }
    },
    /** 盘点 */
    async _RFStorageInventory () {
      this.disabled = true
      if (!this.val1 || !this.val2) {
        this.disabled = false
        return
      }
      try {
        let uid = this.$store.getters.userInfo !== '' ? JSON.parse(this.$store.getters.userInfo).UserPID : ''
        let res = await RFStorageInventory(this.val1, this.val2, uid)
        if (res.ErrNO === '1') {
          this.toast(res.ErrMsg)
          this._RFReadTrayStorageInventory()
        } else {
          this.Dialog(res.ErrMsg)
          this.val2 = ''
          this.$refs.myInput2.handleScan()
        }
        this.disabled = false
      } catch (e) {
        this.disabled = false
        this.val2 = ''
        this.$refs.myInput2.handleScan()
      }
    },
    /** 取消 */
    toCancle () {
      this.val1 = ''
      this.val2 = ''
      this.dataList = []
      this.disabled = false
      this.$refs.myInput1.handleScan()
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '~@style/mixin'
.grid-wraper
  height calc(100% - 2.15rem)
</style>
