<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="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="pkGrade">
              <el-select
                clearable
                :placeholder="'请选择'"
                v-model="queryParams.pkGrade"
              >
                <el-option
                  v-for="item in gradeRangList"
                  :key="item.pkId"
                  :label="item.gradeName"
                  :value="item.pkId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'真实奖衔'" prop="pkAwards">
              <el-select
                clearable
                :placeholder="'请选择'"
                v-model="queryParams.pkAwards"
              >
                <el-option
                  v-for="item in awardsList"
                  :key="item.awardsValue"
                  :label="item.awardsName"
                  :value="item.awardsValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="4">
            <div class="searchbox">
              <el-button class="my_search" @click="getSearch">
                {{ '搜索' }}</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="mini"
            class="thebtn1"
            v-hasButtons="['lotteryExport']"
            @click="handleExport"
          >
            {{ '导出' }}</el-button
          >
          <el-button size="small" class="thebtn2" @click="clickCz(1)"
            >充值已购买次数</el-button
          >
          <el-button
            size="mini"
            v-hasButtons="['lotteryTime']"
            class="thebtn2"
            @click="clickCz(2)"
          >
            {{ '充值未购买次数' }}</el-button
          >
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          v-loading="loading"
          height="700px"
          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="100px"
            align="center"
            prop="actName"
            :label="'活动名称'"
          >
          </el-table-column>
          <el-table-column
            width="120px"
            align="center"
            prop="memberCode"
            :label="'会员编号'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="memberName"
            :label="'会员姓名'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="gradeName"
            :label="'结算等级'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="awardsName"
            :label="'真实奖衔'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="totalNum"
            :label="'总次数'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="totalUsedNum"
            :label="'已购买次数'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="totalSurplusNum"
            :label="'未购买次数'"
          >
          </el-table-column>

          <el-table-column
            align="center"
            prop="availableNum"
            :label="'可用总次数'"
          >
          </el-table-column>
          <el-table-column
            min-width="120px"
            align="center"
            prop="availableUsedNum"
            :label="'已用可用总次数'"
          >
          </el-table-column>
          <el-table-column
            min-width="120px"
            align="center"
            prop="availableSurplusNum"
            :label="'剩余可用总次数'"
          >
          </el-table-column>
          <el-table-column
            width="120px"
            align="center"
            prop="recMemberCode"
            :label="'推荐编号'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="recMemberName"
            :label="'推荐姓名'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="orderCode"
            :label="'来源订单编号'"
            width="120px"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="sourceMemberCode"
            :label="'来源会员编号'"
            width="120px"
          >
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getSearch"
    />

    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      width="40%"
      center
      :before-close="handleClose"
      :title="dialogTitle"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="'会员编号'" prop="memberCode">
              <el-input
                clearable
                v-model="form.memberCode"
                :placeholder="'请输入'"
                @change="changeMemberName"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'会员姓名'" prop="memberName">
              <el-input
                clearable
                v-model="form.memberName"
                disabled
                :placeholder="'请输入'"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              :label="ifgm == 1 ? '已购买次数' : '未购买次数'"
              prop="num"
            >
              <el-input
                clearable
                v-model="form.num"
                :placeholder="'请输入'"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item :label="'活动名称'" prop="pkBaseId">
              <el-select
                clearable
                :placeholder="'请选择'"
                v-model="form.pkBaseId"
              >
                <el-option
                  v-for="item in activetyList"
                  :key="item.pkId"
                  :label="item.actName"
                  :value="item.pkId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">{{ '取消' }}</el-button>
        <el-button type="primary" @click="onSubmit('form')">{{
          '确认'
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import topBar from "@/components/topBar";
import { getgradeRanglist, getAwardsListChiose } from "@/api/level";
import { getMemberName } from "@/api/financial";
import * as gif from "@/api/newgift";
export default {
  name: "LotteryDetailsList",
  components: {
    topBar,
  },
  data() {
    return {
      moren: "lotteryDetailsList",
      topList: [
        {
          name: '抽奖明细',
          path: "lotteryDetailsList",
        },
      ],
      tableData: [],
      loading:false,
      queryParams: {
        pageNum: 1,
        pageSize: 50,
      },

      total: 0,
      multipleSelection: [],
      gradeRangList: [],
      awardsList: [],
      needMergeArr: [
        {
          colName: "colsele",
          mergeCheckNames: ["actName"],
        },
        {
          colName: "actName",
          mergeCheckNames: ["actName"],
        },
        {
          colName: "memberCode",
          mergeCheckNames: ["actName", "memberCode"],
        },
        {
          colName: "memberName",
          mergeCheckNames: ["actName", "memberName"],
        },
        {
          colName: "gradeName",
          mergeCheckNames: ["actName", "gradeName"],
        },
        {
          colName: "awardsName",
          mergeCheckNames: ["actName", "awardsName"],
        },
        {
          colName: "totalNum",
          mergeCheckNames: ["actName", "totalNum"],
        },

        {
          colName: "totalUsedNum",
          mergeCheckNames: ["actName", "totalUsedNum"],
        },
        {
          colName: "totalSurplusNum",
          mergeCheckNames: ["actName", "totalSurplusNum"],
        },

        {
          colName: "availableNum",
          mergeCheckNames: ["actName", "availableNum"],
        },
        {
          colName: "availableUsedNum",
          mergeCheckNames: ["actName", "availableUsedNum"],
        },

        {
          colName: "availableSurplusNum",
          mergeCheckNames: ["actName", "availableSurplusNum"],
        },
      ],
      rowMergeArrs: {}, // 包含需要一个或多个合并项信息的对象
      dialogVisible: false,
      form: {
        memberName: "",
        mmeberCode: "",
        num: "",
        signType: "",
      },
      rules: {
        mmeberCode: [
          { required: true, message: '请输入', trigger: "blur" },
        ],
        num: [{ required: true, message: '请输入', trigger: "blur" }],
        signType: [
          { required: true, message: '请选择', trigger: "change" },
        ],
      },
      activetyList: [],
      dialogTitle: "",
      ifgm: "",
    };
  },
  created() {
    this.getSearch();
    this.getOthers();
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.ifgm == 1) {
            this.$router.push({
              path: "lotteryDetailsListSign",
              query: {
                data: JSON.stringify(this.form),
                index: 744,
              },
            });
          } else {
            this.$router.push({
              path: "lotteryDetailsListSign",
              query: {
                data: JSON.stringify(this.form),
                index: 745,
              },
            });
          }
          
          // let url = "";
          // if (this.ifgm == 1) {
          //   url = gif.purchasedTimesNotexamine;
          // } else {
          //   url = gif.unpurchasedTimesNotexamine;
          // }
          // url(this.form).then((res) => {
          //   if (res.code == 200) {
          //     this.$message({
          //       message: res.msg,
          //       type: "success",
          //     });
          //     this.dialogVisible = false;
          //     this.$refs[formName].resetFields();
          //     // 获取列表
          //     this.getSearch();
          //   }
          // });
        } else {
          return false;
        }
      });
    },
    changeMemberName() {
      getMemberName({ memberCode: this.form.memberCode }).then((res) => {
        this.form.memberName = res.memberName;
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs["form"].clearValidate();
      this.$refs["form"].resetFields();
    },
    clickCz(index) {
      this.form = {
        memberName: "",
        mmeberCode: "",
        num: "",
        signType: "",
      };
      //1已购买 2未购买
      this.ifgm = index;
      if (index == 1) {
        this.dialogTitle = "充值已购买次数";
      } else {
        this.dialogTitle = "充值未购买次数";
      }
      this.dialogVisible = true;
    },
    //解决多列行列合并，根据某一列 合并其他列
    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;
    },
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        this.download(
          "/activity/manage/draw/export-number",
          {
            ...this.queryParams,
          },
          `${'抽奖明细'}_${new Date().getTime()}.xlsx`
        );
        // return api.exportClassIfy(queryParams);
      });
      // .then((response) => {
      //   this.download(response);
      // });
    },
    getOthers() {
      // 真实奖衔下拉选
      getAwardsListChiose().then((res) => {
        this.awardsList = res.rows;
      });
      // 结算等级下拉
      getgradeRanglist().then((res) => {
        this.gradeRangList = res.data;
      });
      //活动列表下拉选
      gif.getActivityFindAll({actType:17}).then((res) => {
        this.activetyList = res.data;
      });
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
      };
      this.getSearch();
    },
    getSearch() {
      this.loading = true
      gif.getDrawDetailList(this.queryParams).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.rowMergeArrs = this.rowMergeHandle(
          this.needMergeArr,
          this.tableData
        );
        this.loading = false
      });
    },
  },
};
</script>

  <style lang="scss" scoped>
::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: #f3a900;

          border-radius: 4px 4px 4px 4px;
        }
        .thebtn2 {
          color: #ffffff;
          border-radius: 4px 4px 4px 4px;
          background: #48b2fd;
        }
      }
    }
  }
}
</style>
