<template>
  <!-- 列表 -->
  <div>
    <AutoResizer :given-height="220">
      <template #default="{ height }">
        <el-table
          v-if="columnsList.length > 0"
          v-loading="loading"
          :data="list"
          :height="height"
          border
        >
          <el-table-column
            v-if="columnsList[0].visible"
            :key="Math.random()"
            label="产品编码"
            align="center"
            prop="productCode"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[1].visible"
            :key="Math.random()"
            label="产品名称"
            align="center"
            prop="productName"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[2].visible"
            :key="Math.random()"
            label="成品SN码"
            align="center"
            prop="materialSerinalNo"
          />
          <el-table-column
            v-if="columnsList[6].visible"
            :key="Math.random()"
            label="箱号"
            align="center"
            prop="boxNo"
          />
          <el-table-column
            v-if="columnsList[4].visible"
            :key="Math.random()"
            label="生产订单"
            align="center"
            prop="workOrderNo"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            fixed="right"
            width="100"
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                icon="el-icon-printer"
                size="mini"
                @click="handlePrintRepair(scope.row)"
                >标签补打</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
    </AutoResizer>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 打印标签 -->
    <CommonPrintDialog ref="commonPrintDialog" @success="completeRepair" />
  </div>
</template>

<script>
import {
  getSnOrBoxNoPage,
  getPrintData,
} from "@/api/wms/inLibrary/productsReBox";
import PackingLabelPrintTemplate from "./PackingLabelPrintTemplate.vue";
import { getFormColumnsList } from "@/api/system/saveColumns/index";
import { printLabel } from "@/api/common";
import CommonPrintDialog from "./CommonPrintDialog.vue";
import { mapGetters } from "vuex";
import { isArray } from "min-dash";

export default {
  name: "BoxRepaired",
  components: {
    PackingLabelPrintTemplate,
    CommonPrintDialog,
  },
  props: {
    activeName: {
      type: String,
      default: "",
    },
    queryParamspro: {
      type: Object,
      default: () => {},
    },
  },
  inject: ["paramsQuery"],
  data() {
    return {
      printData: [],
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 领料单列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //   是否显示查看弹出层
      viewOpen: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
      },
      currentRows: [],
      //   表单参数
      form: {},
      // 表单校验
      rules: {},
      // 显示隐藏列参数
      columns: [
        { key: 0, label: "产品编码", visible: true },
        { key: 1, label: "产品名称", visible: true },
        { key: 2, label: "成品SN码", visible: true },
        { key: 3, label: "原箱号", visible: true },
        { key: 4, label: "生产订单", visible: true },
        { key: 5, label: "状态", visible: true },
        { key: 6, label: "箱号", visible: true },
        { key: 7, label: "半成品SN", visible: true },
      ],
      // 显示隐藏持久化参数
      columnsList: [],
      repairRow: {},
    };
  },
  computed: {
    ...mapGetters(["userId"]),
  },
  created() {
    // 获取显隐列列表
    this.getColumnsList();
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      const params = {
        ...this.paramsQuery,
        ...this.queryParams,
      };
      getSnOrBoxNoPage(params)
        .then((response) => {
          this.list = response.data.list;
          this.total = response.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    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();
    },
    // 标签补打
    async handlePrintRepair(row) {
      this.$refs.commonPrintDialog.visible = true;
      this.repairRow = row;
    },
    // 选完模板后打印
    async completeRepair(templateData) {
      try {
        this.$modal.msgSuccess("正在进行标签补打...");
        const { boxNo } = this.repairRow;
        const params = {
          id: boxNo,
        };
        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;
      }
    },
    // 获取显示隐藏列
    getColumnsList() {
      const { componentName } = this.$route.meta || null;
      const params = {
        userId: this.userId,
        vueForm: componentName,
      };
      getFormColumnsList(params)
        .then((res) => {
          if (isArray(res.data)) {
            this.columnsList = res.data.length === 0 ? this.columns : res.data;
            // 对比默认数据和接口数据，将缺失的列添加到接口数据中
            this.columnsList = this.columns.reduce((acc, defaultColumn) => {
              const found = res.data.find(
                (apiColumn) => apiColumn.key === defaultColumn.key
              );
              if (!found) {
                acc.push({
                  ...defaultColumn,
                  vueForm: componentName,
                  userId: this.userId,
                  id: null,
                });
              }
              return acc;
            }, res.data);
          } else {
            this.columnsList = this.columns;
          }
        })
        .catch((err) => {
          if (err) {
            this.columnsList = this.columns;
          }
        })
        .finally(() => {
          this.$emit("columns", this.columnsList);
        });
    },
  },
};
</script>

<style></style>
