<template>
  <el-dialog
    v-dialogDrag
    title="发出"
    :visible.sync="open"
    width="1200px"
    append-to-body
    :before-close="beforeClose"
    @open="dialogOpen"
  >
    <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="caseNo">
            <el-input
              ref="caseNos"
              v-model="queryParams.caseNo"
              placeholder="请输入/扫入箱号"
              clearable
              @keyup.enter.native="getCurrentBoxNum"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-table :data="list" border size="mini">
      <el-table-column label="箱号" align="center" prop="qrCode" width="150" />
      <el-table-column label="产品编码" align="center" prop="materialNo" />
      <el-table-column
        label="产品名称"
        align="center"
        prop="materialName"
        show-overflow-tooltip
      />
      <el-table-column label="数量" align="center" prop="receiptQty" />
      <el-table-column label="单位" align="center" prop="funitId" />
      <el-table-column label="仓库" align="center" prop="erpStockName" />
      <el-table-column label="区域" align="center" prop="wmsStoreAreaName" />
      <el-table-column label="货位" align="center" prop="wmsStoreName" />
      <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-hasPermi="['wms:sale-out-order-detail:create']"
        :loading="buttonLoading"
        type="primary"
        @click="handleFinish"
        >完成</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { getSalesCreateBatch } from "@/api/wms/output/salesIssue";
import { getInfoByQrCode } from "@/api/wms/base/material";
import { some } from "min-dash";

export default {
  data() {
    return {
      // 遮罩层
      loading: false,
      buttonLoading: false,
      // 总条数
      total: 0,
      // WMS 生产发料 主列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 显示搜索条件
      showSearch: true,
      searching: false,
      currentData: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        caseNo: "",
        boxQty: null,
        id: "",
        type: null,
      },
    };
  },
  methods: {
    // 查询当前物料
    getCurrentBoxNum() {
      if (!this.queryParams.caseNo) {
        this.$modal.msgWarning("请先扫入或输入箱号！");
        return;
      }
      const { caseNo } = this.queryParams;
      if (this.list.some((item) => item.qrCode == caseNo)) {
        this.$modal.msgWarning("该条码已扫入!");
        return;
      }
      const params = {
        qrCode: caseNo,
      };
      const filterItem = this.list.findIndex(
        (item) => item.batchNumber === this.queryParams.caseNo
      );
      if (filterItem > -1) {
        this.$modal.msgWarning("不能扫描相同物料，请重新扫入!");
        this.queryParams.caseNo = undefined;
        return;
      }
      this.searching = true;
      getInfoByQrCode(params)
        .then((res) => {
          const { data } = res;
          const { warnMsg } = data[0];
          if (!data) {
            this.$modal.msgWarning("该箱号不存在");
          } else {
            if (warnMsg === null) {
              this.list.push(...data);
            } else {
              this.$modal
                .confirm(warnMsg)
                .then(() => {
                  this.list.push(...data);
                })
                .catch(() => {
                  this.$modal.msg("您已取消操作");
                });
            }
          }
        })
        .finally(() => {
          this.queryParams.caseNo = undefined;
          this.searching = false;
        });
    },
    // 删除
    handleDelete(row) {
      const { qrCode } = row;
      const index = this.list.findIndex((item) => item.qrCode === qrCode);
      if (index > -1) {
        this.list.splice(index, 1);
      }
    },
    dialogOpen() {
      this.autoFocus();
    },
    autoFocus() {
      this.$nextTick(() => {
        this.$refs.caseNos.focus();
      });
    },
    // 重置
    reset() {
      this.list = [];
      this.queryParams = {
        caseNo: "",
        boxQty: "",
      };
      this.currentData = {};
      this.open = false;
      this.resetForm("form");
    },
    // 取消操作
    cancel() {
      this.reset();
      this.open = false;
      this.$emit("getList");
    },
    // 完成
    handleFinish() {
      if (this.list.length === 0) {
        this.$modal.msgWarning("您还未添加任何产品，不能执行退料操作！");
        return;
      }
      this.buttonLoading = true;
      const { id } = this.queryParams;
      this.list.forEach((i) => {
        i.id = id;
      });
      const params = this.list.map((item) => {
        const {
          qrCode: boxNo,
          materialNo: productCode,
          id: saleOrderDetailId,
          receiptQty: boxQty,
          erpStockId: stockCode,
          wmsStoreAreaId: areaCode,
          wmsStoreId: locationCode,
        } = item;
        return {
          boxNo,
          productCode,
          boxQty,
          saleOrderDetailId,
          stockCode,
          locationCode,
          areaCode,
        };
      });
      this.buttonLoading = true;
      getSalesCreateBatch(params)
        .then((res) => {
          this.$modal.msgSuccess("操作成功");
          this.$parent.$parent.getList();
          this.cancel();
        })
        .finally(() => {
          this.buttonLoading = false;
        });
    },
    beforeClose(done) {
      this.list = [];
      this.total = 0;
      done && done();
    },
  },
};
</script>

<style></style>
