<template>
  <el-dialog
    v-dialogDrag
    :title="title"
    :visible.sync="open"
    width="1200px"
    append-to-body
    @open="dialogOpen"
    @close="dialogClose"
  >
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
      :disabled="searching"
      @submit.native.prevent
    >
      <el-row :gutter="10">
        <el-col :span="8">
          <el-form-item label="标签码" prop="qrCode">
            <el-input
              ref="materialNos"
              v-model="queryParams.qrCode"
              placeholder="请输入/扫入标签码"
              clearable
              @keyup.enter.native="getCurrentMaterial"
            />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="16" class="text-right">
          <el-button
            type="primary"
            plain
            size="mini"
            icon="el-icon-plus"
            @click="handleAdd"
          >添加</el-button>
        </el-col> -->
      </el-row>
    </el-form>

    <el-table :data="list" border size="mini">
      <el-table-column
        label="标签码"
        align="center"
        prop="qrCode"
        width="150"
        show-overflow-tooltip
      />
      <el-table-column
        label="仓库"
        align="center"
        prop="erpStockName"
        width="150"
        show-overflow-tooltip
      />
      <el-table-column label="区域" align="center" prop="wmsStoreAreaName" />
      <el-table-column label="货位" align="center" prop="wmsStoreName" />
      <el-table-column
        label="物料编码"
        align="center"
        prop="materialNo"
        width="150"
        show-overflow-tooltip
      />
      <el-table-column label="供应商" align="center" prop="supplierCode" />
      <el-table-column label="批次数量" align="center" prop="receiptQty" />
      <el-table-column label="单位" align="center" prop="funitId" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="160"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['wms:out-stock-detail:query']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            class="text-danger"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div slot="footer" class="dialog-footer">
      <el-button
        v-if="showStatus === 1"
        v-hasPermi="['wms:out-stock-detail:create']"
        :loading="buttonLoading"
        type="primary"
        @click="handleFinishScanCode"
      >完成/扫码签收</el-button>
      <el-button
        v-else
        v-hasPermi="['wms:out-stock-detail:create']"
        :loading="buttonLoading"
        type="primary"
        @click="handleFinish"
      >完成</el-button>

    </div>

    <!--  -->
    <ScanCode ref="scanCode" @complete="scaneComplete" />
  </el-dialog>
</template>

<script>
import { getCreateBatch } from '@/api/wms/output/productionIssue'
import { getInfoByQrCode } from '@/api/wms/base/material'
import ScanCode from './ScanCode.vue'

export default {
  components: {
    ScanCode
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      buttonLoading: false,
      // 总条数
      total: 0,
      // WMS 生产发料 主列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 显示搜索条件
      showSearch: true,
      searching: false,
      // 已完成
      showStatus: '',
      currentData: [],
      masterId: '',
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        materialLots: '',
        qrCode: '',
        receiptQty: '',
        id: '',
        type: null,
        reApplyFlag: null
      }
    }
  },
  methods: {
    // 查询当前物料
    getCurrentMaterial() {
      if (!this.queryParams.qrCode) {
        this.$modal.msgWarning('请先扫入或输入标签码！')
        return
      }
      this.searching = true
      const { qrCode } = this.queryParams
      const params = {
        qrCode
      }
      getInfoByQrCode(params).then(res => {
        const { data } = res
        if (!data) {
          this.$modal.msgWarning('该标签码不存在')
        } else {
          // this.queryParams.receiptQty = data.receiptQty
          this.list.push(...data)
          // this.list = {
          //   ...this.list,
          //   ...this.queryParams
          // }
          this.queryParams.qrCode = undefined
        }
      }).finally(() => {
        this.searching = false
      })
    },
    // 删除
    handleDelete(row) {
      const { id } = row
      const index = this.list.findIndex(item => item.id === id)
      if (index > -1) {
        this.list.splice(index, 1)
      }
    },

    dialogOpen() {
      this.autoFocus()
    },
    autoFocus() {
      this.$nextTick(() => {
        this.$refs.materialNos.focus()
      })
    },
    // 重置
    reset() {
      this.list = []
      this.queryParams = {
        qrCode: '',
        receiptQty: ''
      }
      this.currentData = {}
      this.open = false
      this.resetForm('form')
    },
    // 取消操作
    cancel() {
      this.reset()
      this.open = false
    },
    // 完成
    handleFinish() {
      if (this.list.length === 0) {
        this.$modal.msgWarning('您还未添加任何物料，不能执行发料操作！')
        return
      }
      this.loading = true
      const status = 0
      const { id } = this.queryParams
      this.list.forEach(i => {
        i.id = id
      })
      const finishParams = this.list.map(item => {
        item.reApplyFlag = this.queryParams.reApplyFlag
        const {
          receiptQty: batchQty,
          erpStockId: wmsStockId,
          wmsStoreAreaId: wmsStockAreaId,
          wmsStoreId: wmsStockLocationId,
          materialNo: erpMaterialId,
          qrCode: wmsLabelMasterId,
          id: wmsProductionWorkOrderBomDetailId,
          supplierCode,
          funitId: unitCode,
          reApplyFlag
        } = item
        return {
          batchQty,
          wmsStockId,
          wmsStockAreaId,
          wmsStockLocationId,
          erpMaterialId,
          wmsLabelMasterId,
          wmsProductionWorkOrderBomDetailId,
          status,
          supplierCode,
          unitCode,
          reApplyFlag
        }
      })
      const isEmpty = finishParams.every(item =>
        item.batchQty && item.wmsStockId && item.wmsStockAreaId && item.unitCode && item.wmsStockLocationId && item.erpMaterialId && item.wmsLabelMasterId
      )
      if (!isEmpty) {
        this.$modal.msgWarning('供应商/单位不能为空')
        return
      }
      this.buttonLoading = true
      getCreateBatch(finishParams).then(res => {
        this.$modal.msgSuccess('操作成功!')
        this.$emit('scanCodeComplete')
        this.cancel()
      }).finally(() => {
        this.loading = false
        this.buttonLoading = false
      })
    },
    async handleFinishScanCode() {
      await this.handleFinish()
      this.$refs.scanCode.masterId = this.masterId
      this.$refs.scanCode.codeOpen = true
    },
    dialogClose() {
      this.list = []
      this.queryParams.qrCode = null
      this.queryParams.receiptQty = ''
      this.currentData = []
    },
    scaneComplete() {
      this.open = false
      this.$emit('completely')
    }
  }
}
</script>

    <style>

    </style>

