<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
        >新增</el-button
      >
    </el-row>
    <el-table ref="tables" v-loading="loading" :data="list">
      <el-table-column
        label="会员名称"
        align="center"
        width="200"
        prop="memberName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="会员原价"
        align="center"
        width="200"
        prop="originalPrice"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="订阅周期"
        align="center"
        width="200"
        prop="subscriptionPeriod"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="会员价格"
        align="center"
        width="200"
        prop="unitPrice"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="shwoEdite(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="shwRights(scope.row.id)"
            >权益权限</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="添加会员等级" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="会员名称" required>
              <el-input
                v-model="form.memberName"
                type="input"
                placeholder="请输入会员名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="原价格" required>
              <el-input
                v-model="form.originalPrice"
                type="number"
                placeholder="请输入原价格"
              ></el-input>
            </el-form-item>
            <el-form-item label="订阅周期" prop="subscriptionPeriod" required>
              <el-select
                v-model="form.subscriptionPeriod"
                placeholder="选择订阅周期"
                clearable
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.member_subscription_period"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="会员价格" required>
              <el-input
                v-model="form.unitPrice"
                type="number"
                placeholder="请输入会员价格"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onAdd">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 编辑 -->
    <el-dialog title="编辑会员等级" :visible.sync="editOpen" width="600px" append-to-body>
      <el-form ref="form" :model="editForm" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="会员名称" required>
              <el-input
                v-model="editForm.memberName"
                type="input"
                placeholder="请输入会员名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="原价格" required>
              <el-input
                v-model="editForm.originalPrice"
                type="number"
                placeholder="请输入原价格"
              ></el-input>
            </el-form-item>
            <el-form-item label="订阅周期" prop="subscriptionPeriod" required>
              <el-select
                v-model="editForm.subscriptionPeriod"
                placeholder="选择订阅周期"
                clearable
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.member_subscription_period"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="会员价格" required>
              <el-input
                v-model="editForm.unitPrice"
                type="number"
                placeholder="请输入会员价格"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleEdit">确定修改</el-button>
        <el-button @click="cancelEdit">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 权益 -->
    <el-dialog title="配置权益" :visible.sync="rightVisible" width="700px" append-to-body>
      <el-form ref="form" :model="editForm" label-width="120px">
        <el-transfer
          :titles="['未有的', '已有的']"
          v-model="hasRight"
          :data="allRight"
          :props="{
            key: 'id',
            label: 'benefitName',
          }"
        ></el-transfer>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleEditRight">确定修改</el-button>
        <el-button @click="cancelEditRight">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  list,
  add,
  deleteList,
  updateList,
  getMemberBenefit,
  addMemberBenefit,
} from "@/api/member/index";
import { rightList } from "@/api/rights/index";

import { getDict } from "@/api/common/index";

export default {
  name: "report",
  dicts: ["member_subscription_period"],
  data() {
    return {
      allRight: [],
      hasRight: [],
      noRight: [],
      currentSelectMemberId:null,
      rightVisible: false,
      open: false,
      editOpen: false,
      form: {},
      // 遮罩层
      loading: true,
      list: [],
      queryParams: {
        pageNumber: 1,
        pageSize: 10,
        ipaddr: undefined,
        userName: undefined,
        status: undefined,
      },
      editForm: {},
    };
  },
  created() {
    this.getList();
    this.onGetDict();
  },
  mounted() {
    // console.log(dict.type.member_benefit_type)
  },
  methods: {
    handleEditRight() {
      addMemberBenefit(this.addDateRange({benefitIds:this.hasRight, memberLevelId: this.currentSelectMemberId})).then((response) => {
        this.$modal.msgSuccess("操作成功");
        this.cancelEditRight();
      });
    },
    cancelEditRight() {
      this.rightVisible = false;
      this.hasRight = []
      this.noRight = []
    },
    // 编辑
    cancelEdit() {
      this.editOpen = false;
      this.editForm = {};
    },
    handleEdit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateList(this.addDateRange(this.editForm)).then((response) => {
            this.$modal.msgSuccess("编辑成功");
            this.getList();
            this.cancelEdit();
          });
        }
      });
    },
    shwoEdite(row) {
      this.editOpen = true;
      this.editForm = JSON.parse(JSON.stringify(row));
    },

    // 新增
    handleAdd() {
      this.open = true;
    },
    cancel() {
      this.form = {};
      this.open = false;
    },
    onAdd() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          add(this.addDateRange({ ...this.form })).then((response) => {
            this.$modal.msgSuccess("添加成功");
            this.getList();
            this.cancel();
          });
        }
      });
    },

    // 获取全部数据字典
    onGetDict() {
      rightList(this.addDateRange({})).then((response) => {
        this.allRight = response.data;
      });
    },
    // 显示当前会员的权益
    shwRights(id) {
        this.currentSelectMemberId = id
      this.rightVisible = true;
      getMemberBenefit(this.addDateRange({ id })).then((response) => {
        this.hasRight = response.data.memberBenefitList.map((item) => item.id);
        // const excludeNames = this.hasRight.map(item => item.id)
        this.noRight = this.allRight.filter((item) => !this.hasRight.includes(item.id));
      });
    },

    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      list(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
        this.list = response.data;
        this.loading = false;
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          return deleteList(row.id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$modal
        .confirm("是否确认清空所有登录日志数据项？")
        .then(function () {
          return cleanLogininfor();
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("清空成功");
        })
        .catch(() => {});
    },
  },
};
</script>
