<!-- 整件拣货 -->
<template>
  <div class='wholePicking'>
    <!-- 扫描监管码 -->
    <scan-code v-if='key == 3' v-model='scanVisible' :warehouseId='warehouseId' ref='scan' :stage='stage' stagePackLevel="0"
               :scanTable='scanTableList' />
    <!-- 	{"type":"0-整件 1-零货 2-功能库房","warehouseId":"仓id"} -->
    <getInfo :bldgInfo='bldgInfo' :type='0' text='整件' />
    <p-tabs default-active-key='2' @change='callback'>
      <template slot='tabBarExtraContent'>
        <!--          <a-button type='primary' icon='printer'>打印</a-button>-->
        <!--          扫描监管码 [integer] 扫描监管码节点：0=拣货 1=外复核-->
        <p-button type='primary' icon='scan' style='margin-right: 10px' :disabled='isScan' v-show='key == 3' @click='scan'>扫描监管码</p-button>
      </template>
      <p-tab-pane key='1' tab='待索取'>
        <!-- tab0  -->
        <tab0 ref='tab0' :warehouseId.sync='warehouseId' :bldgInfo.sync='bldgInfo' />
      </p-tab-pane>
      <p-tab-pane key='2' tab='任务索取'>
        <!-- tab1  -->
        <tab1 ref='tab1' :warehouseId.sync='warehouseId' :bldgInfo.sync='bldgInfo' />
      </p-tab-pane>
      <p-tab-pane key='3' tab='任务确认'>
        <!-- tab2  -->
        <tab2 ref='tab2' :warehouseId.sync='warehouseId' :bldgInfo.sync='bldgInfo' @emitData='emitData' />
      </p-tab-pane>
      <p-tab-pane key='4' tab='标签补打'>
        <!-- tab2  -->
        <tab3 ref='tab3' :warehouseId.sync='warehouseId' :bldgInfo.sync='bldgInfo' />
      </p-tab-pane>
    </p-tabs>
  </div>
</template>

<script>
import scanCode from '../../components/scanCode' //扫描监管码
import tab0 from './components/tab0.vue'
import tab1 from './components/tab1.vue'
import tab2 from './components/tab2.vue'
import tab3 from './components/tab3.vue'
import getInfo from '../components/getInfo.vue'
import { scan_warehouse } from '../config/api.js'

export default {
  components: { tab0, tab1, tab2, tab3, getInfo, scanCode },
  data() {
    return {
      key: 1,
      bldgInfo: {},
      scanVisible: false, // 扫描监管码弹窗
      warehouseId: '',
      isScan: true, //扫描监管码是否禁用。
      scanTableList: [], // 扫描监管码数据
      stage: 1 //扫码阶段：0=入库质检 1=PC拣货 2=PC内复核 3=PC外复核 4=补录 5=PDA拣货 6=PDA内复核 7=PDA外复核
    }
  },
  computed: {
    warehouse() {
      return this.$store.state.user.warehouse
    }
  },
  watch: {
    warehouseId: function(nVal, oVal) {
      this.scanCode()
    }
  },
  created() {
    this.warehouseId = this.warehouse.warehouseId
    this.scanCode()
  },
  methods: {
    callback(key) {
      this.key = key
      this.$nextTick(() => {
        const refsHash = {
          1: 'tab0',
          2: 'tab1',
          3: 'tab2',
          4: 'tab3'
        }
        const values = this.$refs?.[refsHash[key]]?.$refs?.search?.form?.getFieldsValue() || {}
        const { bldgName, bldgCode, bldgId } = values
        this.bldgInfo = { bldgName, bldgCode, bldgId }
      })
    },
    // 扫描监管码
    scan() {
      if (this.scanTableList.length) {
        // this.scanTableList = [...selectedRows];
        this.scanVisible = true
      } else {
        this.$message.warning('该单所有商品，无需扫描药品追溯码')
      }
    },
    // 是否可扫描监管码 scanSupervisionCodeStage[integer] 扫描监管码节点：0=拣货 1=外复核
    scanCode() {
      scan_warehouse({ id: this.warehouseId }).then((res) => {
        if (res.success) {
          // 扫描监管码节点：0=整件拣货、零货拣货 1=整件外复核、零货拣货 2=整件拣货、零货内复核 3=整件外复核、零货内复核
          if (res.data.scanSupervisionCodeStage == 0 || res.data.scanSupervisionCodeStage == 2) {
            this.isScan = false
          }
        }
      })
    },
    // 接收数据 过滤isSupervise==1
    emitData(selectedRows, db, ok) {
      // 判断是否双击
      if ((selectedRows.length == 1 && selectedRows[0].isSupervise && db) || ok) {
        this.scanVisible = true
      } else {
        this.scanVisible = false
      }
      // console.log("外接收emit:", selectedRows.length==1 ,  selectedRows[0].isSupervise , db, ok,this.scanVisible)
      let temp = selectedRows.filter((item) => item.isSupervise == 1)
      this.scanTableList = temp.map((item) => {
        return {
          refBillId: item.pickTaskId, //拣选任务主表id
          refBillLineId: item.pickTaskDId, //拣选任务明细id

          packSize: item.maxSu, // 混入默认大包装参数
          itemId: item.itemId, //商品id
          containerCode: item.containerCode, //容器编号
          batchNum: item.batchNum, //批次号
          itemName: item.itemName, //商品名称
          itemNum: item.itemCode, //商品编码
          specification: item.itemSpecs, //商品规格
          pieceQty: item.qty, //数量
          uom: item.uom, //包装单位
          caseQty: item.uomQty, //件数
          oddPieceQty: item.oddPieceQty, //零散数
          minSu: item.minSu, //小包装
          middleSu: item.middleSu, //中包装
          maxSu: item.maxSu, // 大包装
          manufacturerName: item.manufacturer, // 生产厂家
          manufacturerLotNum: item.manufacturerLotNum, //批号2021.7.7
          srcCode: item.srcCode // 源单编号
        }
      })
      console.log('转换字段后数据：', this.scanTableList)
    }
  }
}
</script>

<style scoped lang='less'>
.wholePicking {
  background: white;

}
</style>
