<template>
  <div>
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button
            size="small"
            :disabled="multiple"
            class="thebtn2"
            v-hasButtons="['DeliveryImmediateAdd']"
            @click="getImmediateData"
            >{{ $t("MN_F_36") }}</el-button
          >
          <el-button
            size="small"
            :disabled="single"
            class="thebtn2"
            v-hasButtons="['DeliveryImmediateUpdateCode']"
            @click="updateCode"
            >{{ $t("MY_ORD_90") }}</el-button
          >

          <el-button
            size="small"
            v-hasButtons="['DeliveryImmediateExport']"
            @click="handleExport"
            class="thebtn1"
          >
            {{ $t("MN_T_8") }}</el-button
          >
        </div>
      </div>
      <el-table
        :data="tableData"
        height="540px"
        style="width: 100%"
        :header-cell-style="{ background: '#EEEEEE' }"
        @selection-change="handleSelectionChange"
        :span-method="objectSpanMethod"
      >
        <el-table-column prop="colsele" type="selection" width="55">
        </el-table-column>
        <el-table-column
          min-width="180px"
          align="center"
          prop="deliverCode"
          :label="$t('MN_F_37')"
        >
        </el-table-column>
        <el-table-column
          min-width="180px"
          align="center"
          prop="orderCode"
          :label="$t('MY_ORD_1')"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="orderTypeVal"
          :label="$t('MY_ORD_2')"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="deliverStatusVal"
          :label="$t('MN_F_48')"
        >
        </el-table-column>
        <el-table-column
          min-width="120px"
          align="center"
          prop="memberCode"
          :label="$t('MN_T_1')"
        >
        </el-table-column>

        <el-table-column
          min-width="120px"
          align="center"
          prop="memberName"
          :label="$t('CK_KS_14')"
        >
        </el-table-column>
        <el-table-column
          min-width="120px"
          align="center"
          prop="phone"
          :label="$t('w_0052')"
        >
        </el-table-column>

        <el-table-column
          align="center"
          min-width="120px"
          prop="productCode"
          :label="$t('MN_F_40')"
        >
        </el-table-column>
        <el-table-column
          min-width="150px"
          align="center"
          prop="productName"
          :label="$t('MN_F_39')"
        >
        </el-table-column>

        <el-table-column
          min-width="150px"
          align="center"
          prop="specsName"
          :label="$t('MN_F_41')"
        >
        </el-table-column>
        <el-table-column align="center" prop="unitName" :label="$t('MN_F_42')">
        </el-table-column>
        <el-table-column align="center" prop="quantity" :label="$t('MN_F_43')">
        </el-table-column>
        <el-table-column align="center" prop="isBarCodeVal" label="是否追溯">
        </el-table-column>
        <el-table-column align="center" prop="barCode" :label="$t('MN_F_44')">
        </el-table-column>
        <el-table-column align="center" prop="recName" :label="$t('MY_ORD_10')">
        </el-table-column>
        <el-table-column
          min-width="120px"
          align="center"
          prop="recPhone"
          :label="$t('w_0242')"
        >
        </el-table-column>
        <!-- <el-table-column
          align="center"
          prop="recProvinceVal"
          :label="$t('MY_ORD_11')"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="recCityVal"
          :label="$t('MY_ORD_12')"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="recCountyVal"
          :label="$t('MY_ORD_13')"
        >
        </el-table-column> -->
        <el-table-column
          min-width="180px"
          align="center"
          prop="recAddress"
          :formatter="formatAddress"
          :label="$t('w_0068')"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="logisticsCompany"
          :label="$t('MY_ORD_21')"
        >
        </el-table-column>
        <el-table-column
          align="center"
          min-width="120px"
          prop="logisticsCode"
          :label="$t('MY_ORD_22')"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="shippingChannelVal"
          :label="$t('MN_F_T_1125')"
        >
        </el-table-column>
        <el-table-column
          min-width="100px"
          align="center"
          prop="storehouseName"
          :label="$t('MN_F_T_251')"
        >
        </el-table-column>
        <el-table-column
          min-width="180px"
          align="center"
          prop="payTime"
          :label="$t('MN_F_T_914')"
        >
        </el-table-column>
        <el-table-column
          min-width="180px"
          align="center"
          prop="deliverTime"
          :label="$t('w_0430')"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="deliveryWayVal"
          :label="$t('w_0061')"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="operateScopeVal"
          :label="$t('MN_F_T_250')"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="tranTypeVal"
          :label="$t('w_0063')"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="secondDeliverVal"
          :label="$t('ENU_ORDER_T_23')"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="systemType"
          :label="$t('系统类型')"
        >
          <template slot-scope="scope">
            <div
              v-for="item in systemTypes"
              :key="item.value"
              v-if="scope.row.systemType == item.value"
            >
              {{ item.label }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          min-width="180px"
          align="center"
          prop="printTime"
          :label="$t('w_0436')"
        >
        </el-table-column>
        <el-table-column
          min-width="180px"
          align="center"
          prop="creationTime"
          :label="$t('PER_DA_9')"
        >
        </el-table-column>
      </el-table>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="params.pageNum"
      :limit.sync="params.pageSize"
      @pagination="getList"
      :pageSizes="pageSizes"
    />
    <!-- 立即发货 -->
    <el-dialog
      :title="$t('MN_F_36')"
      :close-on-click-modal="false"
      :visible.sync="ljfhShow"
      width="80%"
      append-to-body
      class="nowFhbox"
    >
      <div class="maintable">
        <div class="btnbox">
          <el-button size="small" class="thebtn1" @click="handleImport">
            {{ $t("MN_T_9") }}</el-button
          >
        </div>
        <el-table
          :data="fhTable"
          height="400px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          :span-method="objectSpanMethod2"
        >
          <el-table-column
            align="center"
            prop="deliverCode"
            :label="$t('MN_F_37')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="productName"
            :label="$t('MN_F_39')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="specsName"
            :label="$t('MN_F_41')"
          >
          </el-table-column
          ><el-table-column
            align="center"
            prop="quantity"
            :label="$t('MN_F_43')"
          >
          </el-table-column>

          <el-table-column align="center" prop="barCode" :label="$t('MN_F_44')">
          </el-table-column>
          <el-table-column
            align="center"
            prop="orderCode"
            :label="$t('MY_ORD_1')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="memberCode"
            :label="$t('MN_T_1')"
          >
          </el-table-column>

          <el-table-column
            align="center"
            prop="memberName"
            :label="$t('CK_KS_14')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="recName"
            :label="$t('MY_ORD_10')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="recPhone"
            :label="$t('MN_F_45')"
          >
          </el-table-column>
          <!-- <el-table-column
            align="center"
            prop="recProvinceVal"
            :label="$t('MY_ORD_11')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="recCityVal"
            :label="$t('MY_ORD_12')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="recCountyVal"
            :label="$t('MY_ORD_13')"
          >
          </el-table-column> -->
          <el-table-column
            align="center"
            prop="recAddress"
            :formatter="formatAddress"
            :label="$t('S_C_27')"
          >
          </el-table-column>

          <el-table-column
            align="center"
            prop="logisticsCompany"
            width="130px"
            :label="$t('MY_ORD_21')"
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.logisticsCompany"
                :placeholder="$t('MN_F_T_225')"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="logisticsCode"
            min-width="150px"
            :label="$t('MN_F_T_226')"
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.logisticsCode"
                :placeholder="$t('MN_F_T_227')"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            align="center"
            prop="thedel"
            :label="$t('MY_ORD_14')"
          >
            <template slot-scope="scope">
              <el-button
                @click="handleDelete(scope.row, scope.$index, fhTable)"
                type="text"
                size="small"
                style="color: #c73030"
              >
                {{ $t("CK_KS_47") }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="footer" style="text-align: center; margin-top: 20px">
          <el-button @click="ljfhShow = false">
            {{ $t("ENU_P_TYPE0") }}</el-button
          >
          <el-button type="primary" @click="sureFh">{{
            $t("w_0035")
          }}</el-button>
        </div>
      </div>
    </el-dialog>
    <el-dialog
      :title="upload.title"
      :close-on-click-modal="false"
      :visible.sync="upload.open"
      width="600px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :on-exceed="handleExceed"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          {{ $t("MN_F_T_229") }}
          <em>{{ $t("MN_F_T_230") }}</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-link type="info" style="font-size: 12px" @click="importTemplate">
            {{ $t("MN_T_10") }}</el-link
          >
        </div>
        <div class="el-upload__tip" style="color: red" slot="tip">
          {{ $t("MN_F_T_231") }}
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">{{
          $t("w_0035")
        }}</el-button>
        <el-button @click="upload.open = false">{{
          $t("ENU_P_TYPE0")
        }}</el-button>
      </div>
    </el-dialog>
    <!-- 修改单号 -->
    <el-dialog
      :title="$t('MY_ORD_90')"
      :close-on-click-modal="false"
      :visible.sync="updateCodeShow"
      width="80%"
      append-to-body
      class="nowFhbox"
    >
      <div class="maintable">
        <el-table
          :data="updateCodeList"
          height="400px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
        >
          <el-table-column
            align="center"
            prop="deliverCode"
            :label="$t('MN_F_37')"
          >
          </el-table-column>
          <!-- <el-table-column
            align="center"
            prop="productName"
            :label="$t('MN_F_39')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="specsName"
            :label="$t('MN_F_41')"
          >
          </el-table-column
          ><el-table-column
            align="center"
            prop="quantity"
            :label="$t('MN_F_43')"
          >
          </el-table-column>
          <el-table-column align="center" prop="barCode" :label="$t('MN_F_44')">
          </el-table-column>
          <el-table-column
            align="center"
            prop="orderCode"
            :label="$t('MY_ORD_1')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="memberCode"
            :label="$t('MN_T_1')"
          >
          </el-table-column>

          <el-table-column
            align="center"
            prop="memberName"
            :label="$t('CK_KS_14')"
          >
          </el-table-column> -->
          <el-table-column
            align="center"
            prop="recName"
            :label="$t('MY_ORD_10')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="recPhone"
            :label="$t('MN_F_45')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="recProvinceVal"
            :label="$t('MY_ORD_11')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="recCityVal"
            :label="$t('MY_ORD_12')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="recCountyVal"
            :label="$t('MY_ORD_13')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="recAddress"
            :label="$t('S_C_27')"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="logisticsCompany"
            width="120px"
            :label="$t('MY_ORD_21')"
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.logisticsCompany"
                :placeholder="$t('MN_F_T_225')"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="logisticsCode"
            width="120px"
            :label="$t('MN_F_T_226')"
          >
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.logisticsCode"
                :placeholder="$t('MN_F_T_227')"
              ></el-input>
            </template>
          </el-table-column>
        </el-table>
        <div class="footer" style="text-align: center; margin-top: 20px">
          <el-button @click="updateCodeShow = false">
            {{ $t("ENU_P_TYPE0") }}</el-button
          >
          <el-button type="primary" @click="sureUpdateCode">{{
            $t("w_0035")
          }}</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as del from "@/api/delivery";
import { getToken } from "@/utils/auth";
export default {
  name: "Nowdelivery",
  props: {
    params: {
      type: Object,
      default: null,
    },
  },
  data() {
    return {
      tableData: [],
      total: 0,
      idList: [],
      // 非多个禁用
      multiple: true,
      //非单个禁用
      single: true,
      ljfhShow: false,
      fhTable: [],
      updateCodeShow: false,
      updateCodeList: [],
      pageSizes: [100, 200, 500, 1000, 2000],
      // 需要合并项的列
      needMergeArr: [
        {
          colName: "colsele",
          mergeCheckNames: ["deliverCode"],
        },
        {
          colName: "deliverCode",
          mergeCheckNames: ["deliverCode"],
        },
        {
          colName: "orderCode",
          mergeCheckNames: ["deliverCode", "orderCode"],
        },
        {
          colName: "orderTypeval",
          mergeCheckNames: ["deliverCode", "orderCode", "orderTypeval"],
        },
        {
          colName: "orderTypeVal",
          mergeCheckNames: ["deliverCode", "orderCode", "orderTypeVal"],
        },
        {
          colName: "memberCode",
          mergeCheckNames: ["deliverCode", "orderCode", "memberCode"],
        },
        {
          colName: "memberName",
          mergeCheckNames: ["deliverCode", "orderCode", "memberName"],
        },
        {
          colName: "phone",
          mergeCheckNames: ["deliverCode", "orderCode", "phone"],
        },
        {
          colName: "recName",
          mergeCheckNames: ["deliverCode", "orderCode", "recName"],
        },
        {
          colName: "recPhone",
          mergeCheckNames: ["deliverCode", "orderCode", "recPhone"],
        },
        {
          colName: "recProvinceVal",
          mergeCheckNames: ["deliverCode", "orderCode", "recProvinceVal"],
        },
        {
          colName: "recCityVal",
          mergeCheckNames: ["deliverCode", "orderCode", "recCityVal"],
        },
        {
          colName: "recCountyVal",
          mergeCheckNames: ["deliverCode", "orderCode", "recCountyVal"],
        },
        {
          colName: "recAddress",
          mergeCheckNames: ["deliverCode", "orderCode", "recAddress"],
        },
        {
          colName: "operateScopeVal",
          mergeCheckNames: ["deliverCode"],
        },
        {
          colName: "tranTypeVal",
          mergeCheckNames: ["deliverCode"],
        },

        {
          colName: "remark",
          mergeCheckNames: ["deliverCode", "orderCode", "remark"],
        },
        {
          colName: "creationTime",
          mergeCheckNames: ["deliverCode", "orderCode", "creationTime"],
        },
        {
          colName: "payTime",
          mergeCheckNames: ["deliverCode", "orderCode", "payTime"],
        },
        {
          colName: "deliveryWayVal",
          mergeCheckNames: ["deliverCode"],
        },
        {
          colName: "preSaleStatusVal",
          mergeCheckNames: ["deliverCode"],
        },
        {
          colName: "printStatusVal",
          mergeCheckNames: ["deliverCode", "orderCode", "printStatusVal"],
        },
        {
          colName: "deliverStatusVal",
          mergeCheckNames: ["deliverCode"],
        },
        {
          colName: "logisticsCompany",
          mergeCheckNames: ["deliverCode"],
        },
        {
          colName: "shippingChannelVal",
          mergeCheckNames: ["deliverCode"],
        },
        {
          colName: "storehouseName",
          mergeCheckNames: ["deliverCode"],
        },

        {
          colName: "logisticsCode",
          mergeCheckNames: ["deliverCode"],
        },
        {
          colName: "secondDeliverVal",
          mergeCheckNames: ["deliverCode", "orderCode", "secondDeliverVal"],
        },
        {
          colName: "printTime",
          mergeCheckNames: ["deliverCode"],
        },
        {
          colName: "deliverTime",
          mergeCheckNames: ["deliverCode"],
        },

        {
          colName: "orderStatusVal",
          mergeCheckNames: ["deliverCode", "orderCode", "orderStatusVal"],
        },
      ],
      rowMergeArrs: {}, // 包含需要一个或多个合并项信息的对象
      needMergeArr2: [
        {
          colName: "deliverCode",
          mergeCheckNames: ["deliverCode"],
        },
        {
          colName: "orderCode",
          mergeCheckNames: ["deliverCode", "orderCode"],
        },
        {
          colName: "memberCode",
          mergeCheckNames: ["deliverCode", "orderCode", "memberCode"],
        },
        {
          colName: "memberName",
          mergeCheckNames: ["deliverCode", "orderCode", "memberName"],
        },
        {
          colName: "recName",
          mergeCheckNames: ["deliverCode", "orderCode", "recName"],
        },
        {
          colName: "recProvinceVal",
          mergeCheckNames: ["deliverCode", "orderCode", "recProvinceVal"],
        },
        {
          colName: "recCityVal",
          mergeCheckNames: ["deliverCode", "orderCode", "recCityVal"],
        },
        {
          colName: "recCountyVal",
          mergeCheckNames: ["deliverCode", "orderCode", "recCountyVal"],
        },
        {
          colName: "recAddress",
          mergeCheckNames: ["deliverCode", "orderCode", "recAddress"],
        },
        {
          colName: "orderStatusVal",
          mergeCheckNames: ["deliverCode", "orderCode", "orderStatusVal"],
        },
        {
          colName: "logisticsCompany",
          mergeCheckNames: ["deliverCode", "orderCode", "logisticsCompany"],
        },
        {
          colName: "logisticsCode",
          mergeCheckNames: ["deliverCode", "orderCode", "logisticsCode"],
        },
        {
          colName: "thedel",
          mergeCheckNames: ["deliverCode", "orderCode", "thedel"],
        },
      ],

      rowMergeArrs2: {}, //包含需要一个或多个合并项信息的对象
      upload: {
        // 是否显示弹出层（物流导入）
        open: false,
        // 弹出层标题（物流导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的物流数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url:
          process.env.VUE_APP_BASE_API +
          "/sale/manage/deliver/immediate-import",
      },
      logisticsTable: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatAddress(row) {
      return (
        row.recProvinceVal +
        row.recCityVal +
        (row.recCountyVal || "") +
        row.recAddress
      );
    },
    //下载模板
    importTemplate() {
      this.download(
        "/sale/manage/deliver/template-export",
        {},
        `${this.$t("MN_F_T_232")}${new Date().getTime()}.xlsx`
      );
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.url =
        process.env.VUE_APP_BASE_API +
        "/sale/manage/deliver/immediate-import?idList=" +
        this.idList;
      this.upload.title = this.$t("MN_T_9");
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      var fileName = response.fileName;
      if (fileName != undefined && fileName != "") {
      }
      if (response.code == 200) {
        this.logisticsTable = response.data;
        if (this.logisticsTable.length > 0) {
          this.fhTable.forEach((ele) => {
            this.logisticsTable.forEach((item) => {
              if (ele.pkId == item.pkId) {
                this.$set(ele, "logisticsCompany", item.logisticsCompany || "");
                this.$set(ele, "logisticsCode", item.logisticsCode || "");
              }
            });
          });
        }
      }

      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, this.$t("MN_F_T_233"), {
        dangerouslyUseHTMLString: true,
      });
      // this.getList();
    },
    // 上传文件数量超出后的动作
    handleExceed() {
      this.upload.isUploading = false;
      this.msgInfo(this.$t("MN_F_T_234"));
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    //解决多列行列合并，根据某一列 合并其他列
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      let needMerge = this.needMergeArr.some((item) => {
        return item.colName === column.property;
      });
      if (needMerge === true) {
        return this.mergeAction(column.property, rowIndex, column);
      }
    },
    mergeAction(val, rowIndex, colData) {
      let _row = this.rowMergeArrs[val].rowArr[rowIndex];
      let _col = _row > 0 ? 1 : 0;
      return [_row, _col];
    },
    rowMergeHandle(arr, data) {
      if (!Array.isArray(arr) && !arr.length) return false;
      if (!Array.isArray(data) && !data.length) return false;
      let needMerge = {};

      arr.forEach((mergeItem) => {
        // 创建合并管理对象
        needMerge[mergeItem.colName] = {
          rowArr: [],
          rowMergeNum: 0,
        };
        let currentMergeItemData = needMerge[mergeItem.colName];

        // 进行合并管理对象数据的遍历整理
        data.forEach((item, index) => {
          if (index === 0) {
            currentMergeItemData.rowArr.push(1);
            currentMergeItemData.rowMergeNum = 0;
          } else {
            let currentRowData = data[index];
            let preRowData = data[index - 1];

            if (
              this.colMergeCheck(
                currentRowData,
                preRowData,
                mergeItem.mergeCheckNames
              )
            ) {
              currentMergeItemData.rowArr[
                currentMergeItemData.rowMergeNum
              ] += 1;
              currentMergeItemData.rowArr.push(0);
            } else {
              currentMergeItemData.rowArr.push(1);
              currentMergeItemData.rowMergeNum = index;
            }
          }
        });
      });
      return needMerge;
    },
    colMergeCheck(currentRowData, preRowData, mergeCheckNames) {
      if (!Array.isArray(mergeCheckNames) && !mergeCheckNames.length)
        return false;
      let result = true;
      for (let index = 0; index < mergeCheckNames.length; index++) {
        const mergeCheckName = mergeCheckNames[index];
        if (currentRowData[mergeCheckName] !== preRowData[mergeCheckName]) {
          result = false;
          break;
        }
      }
      return result;
    },
    getList() {
      this.params.pkIdList = [];
      del.getDeliverList(this.params).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.rowMergeArrs = this.rowMergeHandle(
          this.needMergeArr,
          this.tableData
        );
      });
    },
    //立即发货弹框删除
    handleDelete(row, index, tab) {
      this.$confirm(`${this.$t("MN_F_T_267")}?`).then((_) => {
        const delList = [];
        this.fhTable.forEach((ele, eindex) => {
          if (
            row.deliverCode == ele.deliverCode &&
            row.orderCode == ele.orderCode
          ) {
            delList.push(eindex);
          }
        });
        tab.splice(index, delList.length);
        this.rowMergeArrs2 = this.rowMergeHandle2(this.needMergeArr2, tab);
      });
    },
    handleSelectionChange(selection) {
      this.idList = selection.map((item) => item.pkId);
      this.multiple = !selection.length;
      this.single = selection.length !== 1;
    },
    //导出
    handleExport() {
      this.params.pkIdList = this.idList;
      this.$confirm(this.$t("MN_F_T_407"), this.$t("MN_F_T_304"), {
        confirmButtonText: this.$t("w_0035"),
        cancelButtonText: this.$t("ENU_P_TYPE0"),
        type: "warning",
      }).then((_) => {
        this.download(
          "/sale/manage/deliver/export",
          // {pkIdList:this.idList},
          { ...this.params },
          `${this.$t("MN_F_T_269")}${new Date().getTime()}.xlsx`
        );
      });
    },
    getImmediateData() {
      this.ljfhShow = true;
      del
        .getImmediateData({ deliverIdList: this.idList.join() })
        .then((res) => {
          this.fhTable = res.data;
          this.rowMergeArrs2 = this.rowMergeHandle2(
            this.needMergeArr2,
            this.fhTable
          );
        });
    },
    //修改单号
    updateCode() {
      this.updateCodeShow = true;
      del.getUpdateCodeList({ deliverId: this.idList[0] }).then((res) => {
        this.updateCodeList = [res.data];
      });
    },
    sureUpdateCode() {
      del
        .changeDeliverCode({
          pkId: this.updateCodeList[0].pkId,
          logisticsCode: this.updateCodeList[0].logisticsCode,
          logisticsCompany: this.updateCodeList[0].logisticsCompany,
        })
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.updateCodeShow = false;
            this.getList();
          }
        });
    },
    sureFh() {
      if (this.fhTable.length > 0) {
        del.deliverConfirm(this.fhTable).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.ljfhShow = false;
          }
        });
      } else {
        this.$message({
          message: res.msg,
          type: "warning",
        });
        this.ljfhShow = false;
      }
    },
    //解决多列行列合并，根据某一列 合并其他列
    objectSpanMethod2({ row, column, rowIndex, columnIndex }) {
      let needMerge = this.needMergeArr2.some((item) => {
        return item.colName === column.property;
      });
      if (needMerge === true) {
        return this.mergeAction2(column.property, rowIndex, column);
      }
    },
    mergeAction2(val, rowIndex, colData) {
      let _row = this.rowMergeArrs2[val].rowArr[rowIndex];
      let _col = _row > 0 ? 1 : 0;
      return [_row, _col];
    },
    rowMergeHandle2(arr, data) {
      if (!Array.isArray(arr) && !arr.length) return false;
      if (!Array.isArray(data) && !data.length) return false;
      let needMerge = {};

      arr.forEach((mergeItem) => {
        // 创建合并管理对象
        needMerge[mergeItem.colName] = {
          rowArr: [],
          rowMergeNum: 0,
        };
        let currentMergeItemData = needMerge[mergeItem.colName];

        // 进行合并管理对象数据的遍历整理
        data.forEach((item, index) => {
          if (index === 0) {
            currentMergeItemData.rowArr.push(1);
            currentMergeItemData.rowMergeNum = 0;
          } else {
            let currentRowData = data[index];
            let preRowData = data[index - 1];

            if (
              this.colMergeCheck2(
                currentRowData,
                preRowData,
                mergeItem.mergeCheckNames
              )
            ) {
              currentMergeItemData.rowArr[
                currentMergeItemData.rowMergeNum
              ] += 1;
              currentMergeItemData.rowArr.push(0);
            } else {
              currentMergeItemData.rowArr.push(1);
              currentMergeItemData.rowMergeNum = index;
            }
          }
        });
      });
      return needMerge;
    },
    colMergeCheck2(currentRowData, preRowData, mergeCheckNames) {
      if (!Array.isArray(mergeCheckNames) && !mergeCheckNames.length)
        return false;
      let result = true;
      for (let index = 0; index < mergeCheckNames.length; index++) {
        const mergeCheckName = mergeCheckNames[index];
        if (currentRowData[mergeCheckName] !== preRowData[mergeCheckName]) {
          result = false;
          break;
        }
      }
      return result;
    },
  },
};
</script>

<style lang="scss" scoped>
.nowFhbox ::v-deep .el-dialog__body {
  padding: 0;
  padding-bottom: 20px;
}
.btnbox {
  padding: 10px;
  .thebtn1 {
    background: rgba(255, 103, 35, 1);
    color: #ffffff;
  }
}

::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
.main {
  // margin-top: 20px;
  background: #ffffff;
  border-radius: 8px;
  box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);

  .main_content {
    padding: 20px 25px;
  }

  .maintop {
    display: flex;
    // padding: 10px 20px;
    padding-bottom: 10px;
    justify-content: space-between;
    align-items: center;
    .mainbtn {
      .thebtn1 {
        color: #ffffff;
        background: rgba(243, 169, 0, 1);
      }
      .thebtn2 {
        color: #ffffff;
        background: rgba(72, 178, 253, 1);
      }
      .thebtn3 {
        color: #ffffff;
        background: rgba(83, 177, 30, 1);
      }
      .thebtn4 {
        color: #ffffff;
        background: rgba(199, 48, 48, 1);
      }
      .thebtn5 {
        color: #ffffff;
        background: rgba(0, 155, 88, 1);
      }
      .thebtn6 {
        color: #ffffff;
        background: rgba(236, 108, 0, 1);
      }
    }
  }
}
</style>
