<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>
    <div class="thetopbox">
      <el-form label-width="100px">
        <el-row>
          <el-col :span="4">
            <el-form-item :label="'订单编号'" prop="orderCode">
              <el-input clearable v-model="queryParams.orderCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'会员编号'" prop="memberCode">
              <el-input clearable v-model="queryParams.memberCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'会员姓名'" prop="memberName">
              <el-input clearable v-model="queryParams.memberName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'收货人'" prop="recName">
              <el-input clearable v-model="queryParams.recName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'联系方式'" prop="recPhone">
              <el-input clearable v-model="queryParams.recPhone"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              class="dizhi"
              :label="'详细地址'"
              prop="recProvince"
            >
              <el-cascader
                v-model="queryParams.shoppingAddress"
                :options="areaData"
                :props="regionParams"
                clearable
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label-width="30px" label="" prop="recAddress">
              <el-input clearable v-model="queryParams.recAddress"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="'创建日期'" prop="teamCode">
              <el-date-picker
                v-model="queryParams.creationTime"
                type="datetimerange"
                range-separator="——"
                :start-placeholder="'开始日期'"
                :end-placeholder="'结束日期'"
                value-format="yyyy-MM-dd HH:mm:ss"
                :unlink-panels="true"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item :label="'支付日期'" prop="teamCode">
              <el-date-picker
                v-model="queryParams.payTime"
                type="datetimerange"
                range-separator="——"
                :start-placeholder="'开始日期'"
                :end-placeholder="'结束日期'"
                value-format="yyyy-MM-dd HH:mm:ss"
                :unlink-panels="true"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <div class="searchbox">
              <el-button
                class="my_search"
                @click="
                  () => {
                    getSearch();
                    getCzNum();
                  }
                "
              >
                {{ '搜索' }}</el-button
              >
              <el-button class="my_reset" @click="reChongzhi">
                {{ '重置' }}</el-button
              >
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button size="small" class="thebtn1" @click="handleExport">
            {{ '导出' }}</el-button
          >
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          v-loading="loading"
          height="640px"
          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
            align="center"
            prop="productName"
            :label="'产品名称'"
            width="200"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="specsName"
            :label="'产品规格'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="price"
            min-width="150px"
            :label="'产品单价' + `(${isLocalSymbol()})`"
          >
          </el-table-column>

          <el-table-column
            align="center"
            prop="achievement"
            min-width="150px"
            :label="`${'产品业绩'}` + `($)`"
          >
          </el-table-column>
         
          <el-table-column
            align="center"
            prop="orderStatusVal"
            min-width="100px"
            :label="'发货状态'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="logisticsCompany"
            min-width="180px"
            :label="'物流单号'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="logisticsCode"
            min-width="150px"
            :label="'物流公司'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="quantity"
            min-width="80px"
            :label="'产品数量'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="orderCode"
            :label="'订单编号'"
            min-width="180px"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="memberCode"
            :label="'会员编号'"
            min-width="120px"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="memberName"
            :label="'会员姓名'"
            min-width="120px"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="recName"
            :label="'收货人'"
            min-width="120px"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="recPhone"
            :label="'联系方式'"
            min-width="120px"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="recProvinceVal"
            :label="'省'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="recCityVal"
            :label="'市'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="recCountyVal"
            :label="'区/县'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="recAddress"
            :label="'详细地址'"
            min-width="150px"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="orderAmount"
            min-width="150px"
            :label="'订单金额' + `(${isLocalSymbol()})`"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="orderAchieve"
            min-width="150px"
            :label="'订单业绩' + `(${isLocals()})`"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="postage"
            :label="'邮费' + `(${isLocalSymbol()})`"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="remark"
            :label="'备注'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            min-width="150px"
            prop="creationTime"
            :label="'创建日期'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            min-width="150px"
            prop="payTime"
            :label="'支付日期'"
          >
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getSearch"
    />
  </div>
</template>

    <script>
import topBar from "@/components/topBar";
import * as three from "@/api/threegoods";
import * as api from "@/api/site.js";
import { isLocals, isLocalSymbol } from "@/utils/numberToCurrency";
export default {
  name: "OrderGoods",
  components: {
    topBar,
  },
  data() {
    return {
      moren: "orderGoods",
      topList: [
        {
          name: '商品订单',
          path: "orderGoods",
        },
      ],
      loading:false,
      tableData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        shoppingAddress: [],
        creationTime: [],
        payTime: [],
      },
      areaData: [],
      regionParams: {
        label: "name",
        value: "id",
        children: "children",
        expandTrigger: "hover",
      },

      total: 0,
      multipleSelection: [],
      // 需要合并项的列
      needMergeArr: [
        {
          colName: "colsele",
          mergeCheckNames: ["orderCode"],
        },
        {
          colName: "orderCode",
          mergeCheckNames: ["orderCode"],
        },

        {
          colName: "memberCode",
          mergeCheckNames: ["orderCode", "memberCode"],
        },
        {
          colName: "memberName",
          mergeCheckNames: ["orderCode", "memberName"],
        },
        {
          colName: "recName",
          mergeCheckNames: ["orderCode", "recName"],
        },
        {
          colName: "recPhone",
          mergeCheckNames: ["orderCode", "recPhone"],
        },
        {
          colName: "recProvinceVal",
          mergeCheckNames: ["orderCode", "recProvinceVal"],
        },
        {
          colName: "recCityVal",
          mergeCheckNames: ["orderCode", "recCityVal"],
        },
        {
          colName: "recCountyVal",
          mergeCheckNames: ["orderCode", "recCountyVal"],
        },
        {
          colName: "recAddress",
          mergeCheckNames: ["orderCode", "recAddress"],
        },
        {
          colName: "orderAmount",
          mergeCheckNames: ["orderCode", "orderAmount"],
        },
        {
          colName: "orderAchieve",
          mergeCheckNames: ["orderCode", "orderAchieve"],
        },
        {
          colName: "postage",
          mergeCheckNames: ["orderCode", "postage"],
        },
        {
          colName: "remark",
          mergeCheckNames: ["orderCode", "remark"],
        },
        {
          colName: "creationTime",
          mergeCheckNames: ["orderCode", "creationTime"],
        },
        {
          colName: "payTime",
          mergeCheckNames: ["orderCode", "payTime"],
        },
      ],
      rowMergeArrs: {}, // 包含需要一个或多个合并项信息的对象
    };
  },
  created() {
    this.getSearch();
    this.getOthers();
  },
  methods: {
    isLocalSymbol,
    isLocals,
    //导出
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then((_) => {
        this.download(
          "/sale/manage/sa-t-order/export",
          { ...this.queryParams },
          `${'商品订单'}${new Date().getTime()}.xlsx`
        );
      });
    },

    getOthers() {
      api.areaList().then((res) => {
        this.areaData = res.data;
      });
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        shoppingAddress: [],
        creationTime: [],
        payTime: [],
      };
      this.getSearch();
    },
    getSearch() {
      this.loading = true
      this.queryParams.recProvince = this.queryParams.shoppingAddress[0];
      this.queryParams.recCity = this.queryParams.shoppingAddress[1];
      this.queryParams.recCountry = this.queryParams.shoppingAddress[2];
      three.getOrderGoodsList(this.queryParams).then((res) => {
        this.loading = false
        this.tableData = res.rows;
        this.total = res.total;
        this.rowMergeArrs = this.rowMergeHandle(
          this.needMergeArr,
          this.tableData
        );
      });
    },
    getCzNum() {
      this.queryParams.pageNum = 1;
    },
    //解决多列行列合并，根据某一列 合并其他列
    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;
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-image-viewer__close {
  right: 500px;
}
::v-deep .dizhi .el-cascader {
  width: 100%;
}
::v-deep .el-range-editor.el-input__inner {
  width: 100%;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}
.page {
  padding: 10px 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    padding: 15px 20px 15px 0;
    background: #ffffff;
    border-radius: 8px;
    .searchbox {
      display: flex;
      align-items: center;
      margin-left: 40px;
    }
    .searchtitle {
      margin-right: 10px;
    }
    .searchbtn {
      background: #08143f;
      color: #ffffff;
    }
  }
  .main {
    //margin-top: 20px;
    background: #f9f9f9;
    //border-radius: 8px;
    //box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);

    .maintop {
      display: flex;
      // padding: 10px 0;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn1 {
          color: #ffffff;
          background: #ffad41;
          width: 68px;
          height: 32px;
        }
        .thebtn2 {
          color: #ffffff;
          background: #009b58;
          width: 68px;
          height: 32px;
        }
        .thebtn3 {
          color: #ffffff;
          background: #c8161d;
          width: 68px;
          height: 32px;
        }
      }
    }
  }
}
</style>
