<template>
  <div>
    <bill-editor ref="detail" :list="detailtList" :defData="detailData" @selectChange="handleSelectChange" @ok="onOk">
    </bill-editor>
  </div>
</template>

<script>
import BillEditor from "../../../components/bill-editor";
import { detailConfig } from "./config/detail";
import { getRqcNoticePrintData, querySubList, getDicts, doException } 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: {},
      tempType: "",
    };
  },

  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);
    },
    handleSelectChange(selectedRowKeys, selectedRows) {},
    // 通知单打印
    printReceipt() {
      let ref = this.queryRefFromName("list");
      console.log(ref);
      let warehouse = this.$store.state.user.warehouse;
      let includeIds = [];
      let params = {};
      console.log(ref.selectedRowKeys);
      //   return;

      if (ref.selectedRowKeys.length) {
        ref.selectedRowKeys.forEach((tt) => {
          includeIds.push(tt);
          //   params = tt;
          params = ref.data.find((i) => i.taskRqcId == tt);
        });
        params.includeIds = includeIds;
        this.print(params);
        return;
      } else {
        if (ref.data.length) {
          ref.data.forEach((tt) => {
            includeIds.push(tt.taskRqcId);
            params = tt;
          });
          params.includeIds = includeIds;
          this.print(params);
          return;
        } else {
          this.$message.warning(this.$lang("请先勾选商品明细!"));
        }
      }
    },
    print(params) {
      //打印通知单
      let ref = this.queryRefFromName("list");
      let warehouse = this.$store.state.user.warehouse;
      getRqcNoticePrintData(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.YS_TZ, printParms).then((res) => {
          if (!res.success) {
            this.$exception(res);
            return;
          }
          this.$message.success(res.message);
        });
        this.queryRefFromName("main").reload();
        // this.handleQuerySubList({ rcvHeaderId: ref.selectedRowKeys[0] }).then((res) => {
        //   ref.data = res
        // })
        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.list) return [];
        return res.data.list.map((item) => {
          return {
            ...item,
            taskId: item.taskInboundId,
            oddCaseQty: item.caseQty,
            oddPieceQty: item.oddPieceQty,
            pieceQty: item.pieceQty,
          };
        });
      });
    },

    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>
