<template>
  <div class="page">
    <topBar v-if="topList.length > 0" :top-list="topList" :moren="moren" />
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button
            size="small"
            v-hasButtons="['memberRetailRegionExport']"
            class="thebtn2"
            @click="handleExport"
          >
            {{ '导出' }}</el-button
          >
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          v-loading="loading"
          height="730px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          :span-method="objectSpanMethod"
          @selection-change="handleSelectionChange"
        >
          <el-table-column align="center" prop="province" :label="$t('省')" />
          <el-table-column align="center" prop="city" :label="$t('市')" />
          <el-table-column align="center" prop="county" :label="$t('区')" />
        </el-table>
      </div>
    </div>
    <el-dialog
      :title="title"
      :visible.sync="ruleShow"
      width="50%"
      center
      :close-on-click-modal="false"
      :before-close="handleClose"
    >
      <el-form ref="forms" :rules="rules" :model="table" label-width="100px">
        <el-form-item :label="$t('会员编号')" prop="memberCode">
          <el-input
            style="width: 460px"
            clearable
            :disabled="types == 2 ? true : false"
            v-model="table.memberCode"
            @input="getmemberinfo"
            :placeholder="'请输入'"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('收益区域')" prop="addressd">
          <el-cascader
            style="width: 460px"
            @change="flChange1"
            v-model="table.addressd"
            :options="areaData"
            :placeholder="'请选择省市区'"
            :props="regionParams"
          />
        </el-form-item>
        <el-form-item :label="$t('生效结束时间')" prop="addressd">
          <el-date-picker
            v-model="table.effectiveEndDate"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            @change="changeTime3"
          >
          </el-date-picker>
        </el-form-item>
        <div style="padding-top: 30px; display: flex; justify-content: center">
          <el-button
            type="text"
            size="small"
            class="button-style"
            @click="handleClose"
          >
            {{ '取消' }}</el-button
          >
          <el-button
            v-preventReClick="3000"
            type="text"
            size="small"
            class="button-style"
            style="margin-left: 40px; background-color: #c8161d"
            @click="submitBtn()"
          >
            {{ '确认' }}
          </el-button>
        </div>
      </el-form>
    </el-dialog>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="research"
    />
  </div>
</template>

<script>
import topBar from "@/components/topBar";
import { getbdmenuList, getPubenumsList } from "@/api/system";
import { getgradeRanglist, getAwardsListChiose } from "@/api/level";
import * as con from "@/api/configManage.js";
import * as api from "@/api/sms.js";
import { Message } from "element-ui";
import { deleteAgreement } from "@/api/configManage";
import ImageUpload from "@/components/ImageUpload";
import { areaList } from "@/api/site";
export default {
  name: "Qxpz",
  components: {
    topBar,
    ImageUpload,
  },
  data() {
    return {
      modifiedTime: [],
      modifiedTime1: [],
      effectiveTime: [],
      regionParams: {
        label: "name",
        value: "id",
        children: "children",
        expandTrigger: "hover",
      },
      title: '添加',
      forms: "",
      pickerOptions: {
        disabledDate(time) {
          const now = new Date();
          now.setHours(0, 0, 0, 0); // Reset to start of today
          return time.getTime() < now.getTime(); // Disable only dates before today
        },
      },
      rules: {
        memberCode: [
          { required: true, message: '请选择发布位置', trigger: "blur" },
        ],
      },
      table: {
        memberCode: "",
        pkMember: "",
        addressd: ["", "", ""],
        effectiveEndDate: "",
      },
      ruleShow: false,
      moren: "cuMemberRetailRegion",
      topList: [
        {
          name: this.$t("未配置区域"),
          path: "cuMemberRetailRegion",
        },
      ],
      defaultParams: {
        label: "label",
        value: "value",
        children: "enumList",
      },
      multipleSelection: [],
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        effective: "0",
      },
      gradeRangList: [],
      awardsList: [],
      statusList: [
        { name: '立即发送', id: "1" },
        { name: '定时发送', id: "2" },
      ],
      statusList1: [
        { name: '未发送', id: "0" },
        { name: '已发送', id: "1" },
      ],
      menuOptions: [],
      loading: false,
      tableData: [],
      total: 0,
      types: "",
      formId: "",
      getPostionList: [],
      address: "",
      areaData: [],
      pkIds: "",
    };
  },
  created() {
    this.research();
    this.getPostion();
    this.getAllArea();
  },
  methods: {
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then((_) => {
        this.download(
          "/member/manage/cu-member-retail-region/export-region-area",
          { ...this.queryParams },
          `${this.$t("未配置区域")}}${new Date().getTime()}.xlsx`
        );
      });
    },
    getmemberinfo(member) {
      con.getQueryMemberInfo({ memberCode: member }).then((res) => {
        this.table.pkMember = res.data.pkId;
      });
    },
    getAllArea() {
      areaList().then((res) => {
        this.areaData = res.data;
      });
    },
    flChange(value) {
      this.queryParams.county = "";
      this.queryParams.province = "";
      this.queryParams.city = "";

      this.queryParams.province = value[0];
      this.queryParams.city = value[1];
      this.queryParams.county = value[2];
    },
    flChange1(value) {
      this.table.county = "";
      this.table.province = "";
      this.table.city = "";
      this.table.province = value[0];
      this.table.city = value[1];
      this.table.county = value[2];
    },
    getPostion() {
      api.getPostion().then((res) => {
        this.getPostionList = res.data;
      });
    },
    addRules() {
      this.types = 1;
      this.ruleShow = true;
      this.title = '添加';
    },
    submitBtn() {
      // 添加角色
      this.ruleSubmit();
    },
    updateShow(item) {
      this.types = 2;
      this.title = '修改';
      this.table = item;
      let arr = [item.province + "", item.city + "", (item.county || "") + ""];
      arr = arr.filter((item) => item != "");
      this.$set(this.table, "addressd", arr);
      this.$forceUpdate();
      console.log(this.table.addressd, "table");
      this.pkIds = item.pkId;
      this.ruleShow = true;
    },
    delRole(item) {
      this.$confirm(this.$t("确认要删除该配置吗?"), '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        con.delRetailRegion(item.pkId).then((res) => {
          if (res.code === 200) {
            Message({
              message: res.msg,
              type: "success",
            });
            this.research();
          } else {
            Message({
              message: res.msg,
              type: "error",
            });
          }
        });
      });
    },
    ruleSubmit() {
      const datas = {};
      const that = this;
      this.$refs["forms"].validate((valid) => {
        if (!valid) {
          return false;
        } else {
          if (this.types === 1) {
            datas.pkMember = this.table.pkMember;
            datas.province = this.table.addressd[0];
            datas.city = this.table.addressd[1];
            datas.county = this.table.addressd[2];
            datas.effectiveEndDate = this.table.effectiveEndDate;
            con.retailRegionAdd(datas).then((res) => {
              if (res.code === 200) {
                Message({
                  message: res.msg,
                  type: "success",
                });
                that.ruleShow = false;
                that.table = {
                  pkMember: "",
                  addressd: ["", "", ""],
                };
                that.research();
              }
            });
          }
          if (this.types === 2) {
            datas.pkMember = this.table.pkMember;
            datas.pkId = this.pkIds;
            datas.province = this.table.addressd[0];
            datas.city = this.table.addressd[1];
            datas.county = this.table.addressd[2];
            datas.effectiveEndDate = this.table.effectiveEndDate;
            con.retailRegionUpdate(datas).then((res) => {
              if (res.code === 200) {
                Message({
                  message: res.msg,
                  type: "success",
                });
                that.ruleShow = false;
                that.table = {
                  pkMember: "",
                  addressd: ["", "", ""],
                };
                that.research();
              }
            });
          }
        }
      });
    },
    statusChange(value) {
      if (this.table.status.length > 1) {
        this.table.status.splice(0, 1);
      }
    },
    handleClose() {
      this.ruleShow = false;
      this.table = {
        pkMember: "",
        addressd: ["", "", ""],
      };
    },
    changeBox(row) {
      console.error(row);
    },
    addFrom() {},
    addRule() {},
    changeTime(value) {
      this.queryParams.startDate = value[0] ? value[0] : "";
      this.queryParams.endDate = value[1] ? value[1] : "";
    },
    changeTime1(value) {
      this.queryParams.startDateModify = value[0] ? value[0] : "";
      this.queryParams.endDateModify = value[1] ? value[1] : "";
    },
    changeTime2(value) {
      this.queryParams.effectiveStaDateStr = value[0] ? value[0] : "";
      this.queryParams.effectiveEndDateStr = value[1] ? value[1] : "";
    },
    changeTime3(value) {
      this.table.effectiveEndDate = value;
    },
    disabledDates(time) {
      const now = new Date(); // 当前时间
      const currentDateTime = new Date(
        now.getFullYear(),
        now.getMonth(),
        now.getDate(),
        now.getHours(),
        now.getMinutes(),
        now.getSeconds()
      );
      return time.getTime() < currentDateTime.getTime(); // 禁止当前时间之前的日期和时间
    },
    handleChangeMunu(value) {
      console.log("🌈", value[value.length - 1]);
      this.queryParams.menuId = value[value.length - 1];
    },
    // 菜单下拉选
    getPubenumsList() {
      getPubenumsList().then((res) => {
        this.menuOptions = res.data;
      });
    },
    // 等级范围选择
    getgradeRanglist() {
      getgradeRanglist().then((res) => {
        this.gradeRangList = res.data;
      });
    },
    // 奖衔下拉列表
    getAwardsListChiose() {
      getAwardsListChiose().then((res) => {
        this.awardsList = res.rows;
      });
    },
    research() {
      // // 日期传入改为起止两个字段
      // if ("" != this.queryParams.creationTime) {
      //   this.queryParams.creationTimeStart = this.queryParams.creationTime[0];
      //   this.queryParams.creationTimeEnd = this.queryParams.creationTime[1];
      // }
      this.loading = true;
      api.regionAreaList(this.queryParams).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
      };
      this.modifiedTime = [];
      this.modifiedTime1 = [];
      this.effectiveTime = [];
      this.address = [];
      this.research();
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // if (columnIndex === 0) {
      //   if (rowIndex % 2 === 0) {
      //     return {
      //       rowspan: 2,
      //       colspan: 1,
      //     };
      //   } else {
      //     return {
      //       rowspan: 0,
      //       colspan: 0,
      //     };
      //   }
      // }
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return "warning-row";
      } else if (rowIndex % 2 == 0) {
        return "success-row";
      }
      return "";
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-upload--picture-card {
  min-width: 80px !important;
  min-height: 80px !important;
  border: 1px solid #ccc;
  border-radius: 4px;
}
::v-deep .el-select {
  width: 100%;
}
::v-deep .el-range-editor.el-input__inner {
  width: 100%;
}
::v-deep .colors {
  padding: 5px !important;
}
::v-deep .banner .el-form-item__error {
  left: 100px;
  margin-top: 10px;
}
.avatar-uploader {
  width: 68px !important;
  height: 68px !important;
}
::v-deep .input-s .el-input {
  //width:637px;
}
.button-style {
  width: 209px;
  height: 48px;
  background: #cccccc;
  border-radius: 5px 5px 5px 5px;
  opacity: 1;
  line-height: 48px;
  padding: 0 !important;
  color: #fff;
  font-size: 18px;
}
::v-deep .el-dialog__title {
  font-weight: bold;
}
::v-deep .el-dialog__header {
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}
.button-s {
  padding: 0 2px !important;
}
::v-deep .el-button {
  padding: 10px 20px;
}
::v-deep .el-date-editor .el-range__close-icon {
  margin-top: -10px;
}
::v-deep .el-date-editor .el-range-separator {
  margin-top: -10px;
}
::v-deep .el-date-editor .el-range__icon {
  margin-top: -10px;
}
::v-deep .el-input__inner {
  //height:28px;
  //line-height: 28px;
}
::v-deep .el-form-item {
  margin-bottom: 15px;
}
::v-deep .el-form-item__label {
  color: #333;
  font-weight: inherit;
}
::v-deep .el-row {
  //margin-top: 20px;
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
.thebtn2 {
  color: #ffffff;
  background: #ffad41;
}
.openClose {
  margin-top: 10px;
  margin-right: 10px;
  color: #666666;
  text-align: center;
}
.page {
  padding: 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    background: #ffffff;
    border-radius: 8px;
    padding-bottom: 10px;
    .searchbox {
      display: flex;
      align-items: center;
      .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: 0px 0;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn1 {
          color: #ffffff;
          background: #3181e5;
          width: 68px;
          //height: 32px;
        }
      }
    }
  }
}
</style>
