// 上架单打印
<template>
  <div>
    <bill-editor ref="detail" :list="detailtList" :defData="detailData" @ok="onOk"> </bill-editor>
  </div>
</template>

<script>
import BillEditor from "../../../components/bill-editor";
import { detailConfig } from "./config/detail";
import { getInstockLabelPrintDataNew, querySubList, getRqcInstockLabelPrintData, getRqcDetailsPrintData } from "./api";
import { toPrintServer, tempType } from "../../../utils/printServer";
import { getCargoOwner } from "../../../utils";

export default {
  components: {
    "bill-editor": BillEditor,
  },

  data() {
    return {
      detailtList: null,
      detailData: null,
      visible: false,
      selectedMainItem: null,
      selectedRows: {},
      headerId: [],
    };
  },

  created() {
    this.handleView();
  },

  methods: {
    queryRefFromName(name) {
      return this.$refs.detail.$refs[name][0];
    },

    onOk() {
      this.$nextTick(() => {
        let warehouse = this.$store.state.user.warehouse;
        let ref = this.queryRefFromName("main");
        ref.$refs.form.form.setFieldsValue({
          warehouseId: warehouse.warehouseId,
          warehouseCode: warehouse.warehouseCode,
          warehouseName: warehouse.warehouseName,
        });
        this.getCargoOwnerFirst();
      });
    },

    handleView() {
      this.detailtList = detailConfig(this);
    },
    // 单据打印
    documentPrint() {
      let ref = this.queryRefFromName("main");
      if (ref.selectedRowKeys.length === 0) {
        this.$message.warning(this.$lang("请先勾选需要打印的上架单主商品"));
        return;
      }
      let incloudHids = [];
      let params = {};
      params.warehouseId = this.selectedMainItem.warehouseId;

      this.headerId.forEach((tt) => {
        incloudHids.push(tt.taskId);
      });
      params.taskId = incloudHids.join(",");
      getInstockLabelPrintDataNew(params).then((res) => {
        if (!res.success) {
          this.$exception(res);
          return;
        }
        const printParms = {
          consignorId: this.selectedMainItem.mtlOwnerId,
        };
        const warehouseId = this.selectedMainItem.warehouseId;
        //调用打印方法()
        toPrintServer(res.data.data, warehouseId, tempType.INSTOCK_PRINT_TEMP, printParms).then((res) => {
          if (!res.success) {
            this.$exception(res);
            return;
          }
          this.$message.success(res.message);
        });
        this.queryRefFromName("main").reload();
        ref.data = [];
      });
    },
    // //汇总单打印
    // headerPrint() {
    //   let ref = this.queryRefFromName("main");
    //   if (ref.selectedRowKeys.length === 0) {
    //     this.$message.warning(this.$lang("请先勾选需要打印的通知单主商品"));
    //     return;
    //   }
    //   let incloudHids = [];
    //   let params = {};
    //   this.headerId.forEach((tt) => {
    //     incloudHids.push(tt.rqcHeaderId);
    //   });
    //   params.incloudHids = incloudHids;
    //   getInstockLabelPrintDataNew(params).then((res) => {
    //     if (!res.success) {
    //       this.$exception(res);
    //       return;
    //     }
    //     const printParms = {
    //       consignorId: this.selectedMainItem.mtlOwnerId,
    //     };
    //     const warehouseId = this.selectedMainItem.warehouseId;
    //     //调用打印方法()
    //     toPrintServer(res.data.data, warehouseId, tempType.ZJ_HZ, printParms).then((res) => {
    //       if (!res.success) {
    //         this.$exception(res);
    //         return;
    //       }
    //       this.$message.success(res.message);
    //     });
    //     this.queryRefFromName("main").reload();
    //     ref.data = [];
    //   });
    // },
    // 标签补打
    labelPrint() {
      let ref = this.queryRefFromName("list");

      if (ref.selectedRowKeys.length === 0) {
        this.$message.warning(this.$lang("请先勾选需要打印的通知单商品明细"));
        return;
      }
      let includeIds = [];
      let params = {};
      this.selectedRows.forEach((tt) => {
        includeIds.push(tt.rqcLineId);
        // params = tt
      });
      params.includeIds = includeIds;
      params.rqcHeaderId = this.selectedMainItem.rqcHeaderId;

      getRqcInstockLabelPrintData(params).then((res) => {
        if (!res.success) {
          this.$exception(res);
          return;
        }
        if (!res.data) {
          this.$message.warning(this.$lang("当前明细不需要进行标签补打"));
        } else {
          const printParms = {
            consignorId: this.selectedMainItem.mtlOwnerId,
          };
          const warehouseId = this.selectedMainItem.warehouseId;
          //调用打印方法()
          toPrintServer(res.data.data, warehouseId, tempType.SJ_BQ, printParms).then((res) => {
            if (!res.success) {
              this.$exception(res);
              return;
            }
            this.$message.success(res.message);
          });
        }
        this.queryRefFromName("main").reload();
        ref.data = [];
      });
    },
    // 打印明细单
    detailsPrint() {
      let ref = this.queryRefFromName("list");
      let includeIds = [];
      let params = {};
      if (ref.selectedRowKeys.length) {
        this.selectedRows.forEach((tt) => {
          includeIds.push(tt.rqcLineId);
          // params = tt
        });
        params.includeIds = includeIds;
        params.rqcHeaderId = this.selectedMainItem.rqcHeaderId;
        this.printList(params);
      } else {
        if (ref.data) {
          ref.data.forEach((tt) => {
            includeIds.push(tt.rqcLineId);
            // params = tt
          });
          params.includeIds = includeIds;
          params.rqcHeaderId = this.selectedMainItem.rqcHeaderId;
          this.printList(params);
        }
      }
    },
    //明细打印调取接口
    printList(params) {
      let ref = this.queryRefFromName("list");
      getRqcDetailsPrintData(params).then((res) => {
        if (!res.success) {
          this.$exception(res);
          return;
        }
        const printParms = {
          consignorId: this.selectedMainItem.mtlOwnerId,
        };
        const warehouseId = this.selectedMainItem.warehouseId;
        //调用打印方法()
        toPrintServer(res.data.data, warehouseId, tempType.ZJ_MX, printParms).then((res) => {
          if (!res.success) {
            this.$exception(res);
            return;
          }
          this.$message.success(res.message);
        });
        this.queryRefFromName("main").reload();
        ref.data = [];
      });
    },

    handleQuerySubList(params) {
      this.$refs.detail.$refs.list[0].loading = true;
      this.$refs.detail.$refs.list[0].selectedRowKeys = [];
      return querySubList(params).then((res) => {
        this.$refs.detail.$refs.list[0].loading = false;
        if (!res.success) {
          this.$exception(res);
          return;
        }
        if (!res.data) return [];
        return res.data.list.map((item) => {
          return {
            ...item,
            taskId: item.taskInboundId,
            oddPieceQty: item.oddPieceQty,
            pieceQty: item.pieceQty,
          };
        });
      });
    },

    showSplitBill(item) {
      this.splitVisible = true;
    },

    toErrorBill() {
      let ref = this.queryRefFromName("list");
      if (ref.selectedRowKeys.length === 0) {
        this.$message.warning(this.$lang("请先勾选"));
        return;
      }
      this.$confirm({
        content: this.$lang("是否确定提交?"),
        onOk: () => {
          let params = ref.data.filter((t) => ref.selectedRowKeys.some((k) => k == t[ref.rowKey]));
          return this.handleErrorBill(params).then((res) => {
            if (!res) return;
            ref.data = ref.data.filter((t) => !ref.selectedRowKeys.some((k) => k == t[ref.rowKey]));
            ref.selectedRowKeys = [];
          });
        },
      });
    },

    handleReset() {
      this.detailData = null;
    },

    handleErrorBill(params) {
      return doException(params).then((res) => {
        if (!res.success) {
          this.$exception(res);
          return false;
        }
        this.$message.success(this.$lang("操作成功"));
        return true;
      });
    },

    //获取货主信息
    getCargoOwnerFirst() {
      getCargoOwner().then((res) => {
        if (!res.success) {
          this.$exception(res);
          return;
        }
        this.$nextTick(() => {
          if (res.data.list.length > 0) {
            let ref = this.queryRefFromName("main");
            ref.$refs.form.form.setFieldsValue({
              cargoOwnerId: res.data.list[0].companyId,
              cargoOwnerCode: res.data.list[0].companyCode,
              cargoOwnerName: res.data.list[0].companyName,
            });
          }
        });
        // }
      });
    },
  },
};
</script>

<style></style>
