<template>
  <!-- 列表 -->
  <div>
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="设定装箱数" label-width="88px">
            <el-input-number
              v-model="queryParams.caseNum"
              label="装箱数量"
              :min="1"
              :step="1"
              :controls="true"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="成品SN" prop="materialSerinalNo">
            <el-input
              ref="materialSn"
              v-model="queryParams.materialSerinalNo"
              placeholder="请扫入/输入成品SN码"
              :disabled="queryParams.caseNum === list.length"
              @keyup.enter.native="handleScane"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="已扫数量" prop="totalQty">
            <el-input
              v-model="queryParams.totalQty"
              placeholder="请=输入已扫数量"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-button
            type="primary"
            plain
            style="height: 30px"
            icon="el-icon-s-release"
            size="mini"
            :disabled="queryParams.caseNum !== list.length"
            @click="handleReBox"
          >
            完成装箱/打印外箱标签
          </el-button>
        </el-col>
      </el-row>
    </el-form>

    <AutoResizer :given-height="220">
      <template #default="{ height }">
        <el-table v-loading="loading" :data="list" :height="height" border>
          <el-table-column label="箱号" align="center" prop="id" />
          <el-table-column
            label="半成品SN码"
            align="center"
            prop="materialSerinalNo"
          />
        </el-table>
      </template>
    </AutoResizer>

    <!-- 打印标签 -->
    <CommonPrintDialog ref="commonPrintDialog" @success="handleReboxAndPrint" />
  </div>
</template>

<script>
import {
  getReBoxDetail,
  reCreateBox,
  getPrintData,
} from "@/api/wms/inLibrary/productsReBox";
import CommonPrintDialog from "./CommonPrintDialog.vue";
import { printLabel } from "@/api/common";

export default {
  name: "ReBoxList",
  components: {
    CommonPrintDialog,
  },
  props: {
    activeName: {
      type: String,
      default: "",
    },
    queryParamspro: {
      type: Object,
      default: () => {},
    },
  },
  inject: ["paramsQuery"],
  data() {
    return {
      printData: [],
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 领料单列表
      list: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        materialSerinalNo: null,
        caseNum: null,
        totalQty: 0,
      },
      //   表单参数
      form: {},
    };
  },
  created() {
    this.autoFoucus();
  },
  methods: {
    getList() {
      this.handleQuery();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        wmsIncomingReceiptId: undefined,
        orderType: undefined,
        erpStockId: undefined,
        wmsStoreAreaId: undefined,
        wmsStoreId: undefined,
        inQty: undefined,
        funitId: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      //   this.getList()
    },
    autoFoucus() {
      this.$nextTick(() => {
        this.$refs.materialSn.focus();
      });
    },
    // 完成装箱
    async handleReBox() {
      if (this.list.length === 0) {
        this.$modal.msgWarning("暂无可装箱的成品~");
        return;
      }

      this.$refs.commonPrintDialog.visible = true;
    },
    // 选完模板打印
    async handleReboxAndPrint(templateData) {
      this.loading = true;
      try {
        const result = await reCreateBox(this.list);
        this.list.length = 0;
        this.$emit("complete");
        this.queryParams.caseNum = null;
        this.queryParams.materialSerinalNo = null;
        this.$modal.msgSuccess("重新装箱完成,正在打印...");
        const { data } = result;
        const params = {
          id: data,
        };
        const reBoxDetailsResult = await getPrintData(params);
        this.printData.push(reBoxDetailsResult.data);

        // 打印参数
        const printParams = {
          printJson: JSON.stringify(this.printData),
          fileUrl: templateData?.BtReportUrl,
        };

        await printLabel(printParams); // 打印
        this.printData.length = 0;
      } catch (error) {
        console.log(error);
      } finally {
        this.$refs.commonPrintDialog.visible = false;
        this.loading = false;
      }
    },

    handleScane() {
      if (this.queryParams.caseNum === 0) {
        this.$modal.msgWarning("请先输入正确的装箱数量~");
        return;
      }
      if (!this.queryParams.materialSerinalNo) {
        this.$modal.msgWarning("请先扫入/输入正确的SN码~");
        return;
      }
      // 防重
      if (
        this.list.some(
          (i) => i.materialSerinalNo === this.queryParams.materialSerinalNo
        )
      ) {
        this.$modal.msgWarning("已存在相同的成品SN码，请重新输入~");
        this.queryParams.materialSerinalNo = "";
        this.autoFoucus();
        return;
      }
      const params = {
        materialSerinalNo: this.queryParams.materialSerinalNo,
      };
      getReBoxDetail(params)
        .then((res) => {
          //   添加到list
          this.list.unshift(JSON.parse(JSON.stringify(res.data)));
          this.queryParams.totalQty = this.list.length;
        })
        .finally(() => {
          this.materialSerinalNo = "";
          this.autoFoucus();
        });
    },
  },
};
</script>

<style></style>
