<template>
  <div class="app-container">
    <div class="input">
      <el-form label-width="250px">
        <el-form-item label="物流单号/SKU">
          <el-input
            v-model="inputNumber"
            type="text"
            class="inputbox"
            @keyup.enter.native="inputComplete"
          />
        </el-form-item>
        <el-form-item label="当前检验订单">
          <el-input v-model="trackingNumber" readonly style="width:300px" />
        </el-form-item>
        <el-form-item label="需要检验件数">
          <el-input v-model="qty" readonly style="width:300px" />
        </el-form-item>
        <el-form-item label="已检验件数">
          <el-input v-model="checkQty" readonly style="width:300px" />
        </el-form-item>
      </el-form>
      <el-table :data="sdata">
        <el-table-column prop="SkuNo" label="SKU编号" />
        <el-table-column prop="Qty" label="件数" />
        <el-table-column prop="CheckQty" label="检验件数" />
      </el-table>
    </div>
  </div>
</template>
<script>

import { GetStockCheckView, CheckPickDetail } from '@/api/ds/pick/pickcheck'
export default {
  name: 'PickCheck',
  data() {
    return {
      sdata: [],
      inputNumber: '',
      trackingNumber: '',
      qty: 0,
      checkQty: 0
    }
  },

  methods: {
    inputComplete(e) {
      debugger
      if (this.inputNumber != '') {
        if (this.inputNumber.indexOf('SKU-') >= 0) {
          if (this.trackingNumber == '') {
            this.$alert('请扫描跟踪号')
          } else {
            var f = this.sdata.filter(x => x.SkuNo == this.inputNumber && x.CheckQty + 1 <= x.Qty)
            if (f.length > 0) {
              CheckPickDetail(this.trackingNumber, f[0].SkuId).then(r => {
                f[0].CheckQty += 1
                this.checkQty++
                var e2 = this.sdata.filter(x => x.CheckQty < x.Qty)
                if (e2.length == 0) {
                  this.$alert(this.trackingNumber + '检验完成')
                  this.inputNumber = ''
                  this.trackingNumber = ''
                  this.sdata = []
                  this.checkQty = 0
                  this.qty = 0
                }
              })
            } else {
              this.$alert(this.inputNumber + '未找到待检验库存')
            }
          }
        } else {
          var isScan = false
          if (this.trackingNumber != '') {
            this.$confirm('当前订单未检验完成，是否切换订单').then(r => {
              isScan = true
            })
          } else {
            isScan = true
          }
          if (isScan) {
            GetStockCheckView(this.inputNumber).then(r => {
              this.trackingNumber = this.inputNumber
              this.sdata = r
              this.inputNumber = ''
              this.qty = 0
              this.checkQty = 0
              for (var i = 0; i < this.sdata.length; i++) {
                this.qty += this.sdata[i].Qty
                this.checkQty += this.sdata[i].CheckQty
              }
            })
          }
        }
      }
    }
  }
}
</script>
