<template>
  <section>
    <nav-bar title="组盘"></nav-bar>
    <section class="content">
      <div class="filter-wraper">
        <search-box
        label="托盘码"
        v-model="val1"
        @handleChange="handleChange1"
        ></search-box>
        <search-box
        label="物料条码"
        :focused="true"
        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>
            </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>
            </tr>
          </table>
        </div>
      </div>
    </section>
    <section class="submit-bar">
      <button class="btn submit-button" :class="{'btn-disabled' : !val1 || !val2}" :disabled="disabled" @click="_rfTrayPackage">组盘</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 {rfReadTrayStatePackage, rfReadTrayStoragePackage, rfReadMatBarCodeStatePackage, rfTrayPackage} from '@config/getData2'
export default {
  name: 'GroupDisk',
  components: {
    NavBar,
    SearchBox
  },
  data () {
    return {
      val1: '',
      val2: '',
      dataList: [],
      disabled: false
    }
  },
  methods: {
    /** 读取托盘码 */
    async _rfReadTrayStatePackage (val) {
      try {
        let res = await rfReadTrayStatePackage(val)
        if (res.ErrNO === '1') {
          this._rfReadTrayStoragePackage()
        } else {
          this.toast(res.ErrMsg)
          this.val1 = ''
        }
      } catch (e) {
        this.val1 = ''
      }
    },
    /** 托盘库存信息 */
    async _rfReadTrayStoragePackage () {
      let res = await rfReadTrayStoragePackage(this.val1)
      if (res.ErrNO === '1') {
        this.dataList = [...res.StorageList]
      } else {
        this.toast(res.ErrMsg)
      }
    },
    /** 读取物料条码 */
    async _rfReadMatBarCodeStatePackage (val) {
      try {
        let res = await rfReadMatBarCodeStatePackage(val)
        if (res.ErrNO === '1') {
          this.toast(res.ErrMsg)
        } else {
          this.toast(res.ErrMsg)
          this.val2 = ''
        }
      } catch (e) {
        this.val2 = ''
      }
    },
    handleChange1 (e, type) {
      if (type) {
        this._rfReadTrayStatePackage(e)
      }
    },
    handleChange2 (e, type) {
      if (type) {
        this._rfReadMatBarCodeStatePackage(e)
      }
    },
    /** 组盘 */
    async _rfTrayPackage () {
      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 rfTrayPackage(this.val1, this.val2, '', uid)
        if (res.ErrNO === '1') {
          this.toast(res.ErrMsg)
        } else {
          this.Dialog(res.ErrMsg)
        }
        this.toCancle()
      } catch (e) {
        this.disabled = false
      }
    },
    /** 取消 */
    toCancle () {
      this.val1 = ''
      this.val2 = ''
      this.dataList = []
      this.disabled = false
    }
  }
}
</script>

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