<template>
  <div id="">
    <search-box-new
      ref="searchBox"
      :formData="formData"
      :formList="formList"
      @search="search"
      @init="init"
    />
    <table-list
      rowKey="userId"
      ref="tableList"
      :tableSets="tableSet"
      :tableData="tableData"
      :navText="navText"
      :loading="loading"
    >
      <template slot="customize">
        <el-button type="primary" @click="dialogVisibleVipOpen = true">
          会员卡开通
        </el-button>
        <el-button type="primary" @click="batch(1)"> 会员充值 </el-button>
        <el-button type="primary" @click="batch(2)"> 会员恢复 </el-button>
        <el-button @click="batch(3)"> 注销会员 </el-button>
        <el-button @click="exportData"> 导出excel </el-button>
      </template>
      <template slot="btn" slot-scope="props">
        <el-button
          type="text"
          @click="addClick([props.scope.row.userId])"
          :disabled="props.scope.row.vipTag !== 1"
          >充值</el-button
        ><el-button type="text" @click="cancellation(props.scope.row)">{{
          props.scope.row.vipTag == 1
            ? "注销"
            : props.scope.row.vipTag == 2
            ? "恢复"
            : ""
        }}</el-button
        ><el-button type="text" @click="record(props.scope.row)"
          >记录</el-button
        >
      </template>
    </table-list>
    <pagination
      :total="total"
      :pageSize.sync="formData.pageSize"
      :currentPage.sync="formData.pageNum"
      @search="search"
    />
    <dislog
      :dialogVisible.sync="dialogVisible"
      @search="search"
      :activeData="activeData"
    ></dislog>
    <vip-open
      :dialogVisible.sync="dialogVisibleVipOpen"
      @search="search"
    ></vip-open>
    <record
      :dialogVisible.sync="dialogRecordVisible"
      @search="search"
      :userId="activeData.userId"
    ></record>
  </div>
</template>

<script>
import { exportFn } from "@/utils/index.js";
import dislog from "./dislog.vue";
import vipOpen from "./vipOpen.vue";
import record from "./record.vue";
import searchBoxNew from "@/components/searchBoxNew";
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
export default {
  name: "MembershipCard",
  components: { searchBoxNew, tableList, pagination, dislog, record, vipOpen },
  data() {
    return {
      loading: false,
      navText: {
        title: "会员卡",
        index: 0,
        ch: "条",
        num: true,
        choice: true,
        addHide: true,
        custom: false,
        openCheckMore: true,
      },
      formList: [
        {
          prop: "vipTag",
          placeholder: "会员状态",
          scope: "select",
          options: [
            {
              label: "已开通",
              value: 1,
            },
            {
              label: "已注销",
              value: 2,
            },
          ],
        },
        {
          prop: "telphone",
          placeholder: "输入手机号码",
        },
        {
          prop: "idCard",
          placeholder: "输入身份证号",
        },
      ],
      formData: {},
      tableSet: [
        {
          label: "会员状态",
          prop: "vipTag",
          hidden: true,
          scope: "isOptions",
          options: [
            {
              label: "已开通",
              value: 1,
            },
            {
              label: "已注销",
              value: 2,
            },
          ],
        },
        {
          label: "会员来源",
          prop: "vipFrom",
          hidden: true,
          scope: "isOptions",
          options: [
            {
              label: "云学堂PC",
              value: 1,
            },
            {
              label: "云学堂H5",
              value: 2,
            },
            {
              label: "云学堂小程序",
              value: 3,
            },
            {
              label: "e管证",
              value: 4,
            },
            {
              label: "教育智慧管理系统",
              value: 5,
            },
            {
              label: "中正总平台",
              value: 6,
            },
          ],
        },
        {
          label: "关联机构",
          prop: "tenantName",
          hidden: true,
        },
        {
          label: "姓名",
          prop: "realname",
          hidden: true,
        },
        {
          label: "性别",
          prop: "sex",
          hidden: true,
          scope: "isOptions",
          options: [
            {
              label: "男",
              value: 1,
            },
            {
              label: "女",
              value: 2,
            },
          ],
        },
        {
          label: "身份证号",
          prop: "idCard",
          hidden: true,
        },
        {
          label: "手机号码",
          prop: "telphone",
          hidden: true,
        },
        {
          label: "当前积分",
          prop: "pointsValue",
          hidden: true,
        },
        {
          label: "累计充值",
          prop: "rechargeMoney",
          hidden: true,
        },
        {
          label: "累计退款",
          prop: "refundMoney",
          hidden: true,
        },
        {
          label: "开通时间",
          prop: "vipOpenTime",
          hidden: true,
          scope: "aTimeList",
        },
        {
          label: "注销时间",
          prop: "vipCloseTime",
          hidden: true,
          scope: "aTimeList",
        },
      ],
      tableData: [],
      total: 0,
      dialogVisible: false,
      dialogRecordVisible: false,
      dialogVisibleVipOpen: false,
      activeData: {},
    };
  },
  created() {
    this.search(2);
  },
  methods: {
    exportData() {
      this.$api.appuserlistExport(this.formData).then((res) => {
        if (res.msg) {
          exportFn(res.msg, `导出数据`);
        } else {
          this.$message.error("导出失败");
        }
      });
    },
    batch(i) {
      if (this.$refs.tableList.allCheckData.length === 0) {
        this.$message.error("请勾选数据");
        return;
      }
      if (i == 1) {
        if (this.$refs.tableList.allCheckData.every((k) => k.vipTag === 1)) {
          this.addClick(this.$refs.tableList.allCheckData.map((k) => k.userId));
        } else {
          this.$message.error("只能对会员状态为已开通的会员进行充值");
          return;
        }
      } else if (i == 2 || i == 3) {
        const alist = this.$refs.tableList.allCheckData.filter((item) => {
          if (i == 2) {
            return item.vipTag == 2;
          }
          if (i == 3) {
            return item.vipTag == 1;
          }
        });
        if (alist.length === 0) {
          this.$message.warning(
            `选中的数据中不存在可${i == 2 ? "恢复" : "注销"}的会员`
          );
          return;
        }
        this.$confirm(`是否${i == 2 ? "恢复" : "注销"}选中的会员`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$api[
              i == 2 ? "appuserrestoreBatchVip" : "appusercloseBatchVip"
            ]({
              userIdList: alist.map((k) => k.userId),
            }).then((res) => {
              this.$message.success("操作成功");
              this.search(3);
            });
          })
          .catch(() => {});
      }
    },
    addClick(data) {
      this.activeData = {
        userIdList: data,
      };
      this.dialogVisible = true;
    },
    search(v) {
      this.loading = true;
      if (v === 2) {
        this.formData = {
          pageSize: 10,
          pageNum: 1,
        };
        this.$nextTick(() => {
          this.$refs.tableList.clearMoreActive();
        });
      }
      if (v === 3) {
        this.$nextTick(() => {
          this.$refs.tableList.clearMoreActive();
        });
      }
      this.$api
        .appuservipUserList(this.formData)
        .then((res) => {
          this.tableData = res.rows;
          this.total = res.total;
          this.navText.index = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    init() {
      this.search(2);
    },
    //注销/恢复会员
    cancellation(row) {
      this.$alert(
        `确定${
          row.vipTag === 1 ? "注销" : row.vipTag === 2 ? "恢复" : ""
        }该会员吗？`,
        "提示",
        {
          dangerouslyUseHTMLString: true,
        }
      )
        .then(() => {
          this.$api[
            row.vipTag === 1
              ? "appusercloseBatchVip"
              : row.vipTag === 2
              ? "appuserrestoreBatchVip"
              : ""
          ]({
            userIdList: [row.userId],
          }).then((res) => {
            this.$message.success("操作成功");
            this.search(3);
          });
        })
        .catch(() => {});
    },
    record(data) {
      this.activeData = data || {};
      this.dialogRecordVisible = true;
    },
  },
};
</script>

<style lang="scss" scoped></style>
