<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="权益值(权益类型为是否时：0代表不支持，1代表支持；权益类型为数量限制时：0代表不限制，其他数字代表具体限制数量)" prop="rightsValue">
        <el-input
          v-model="queryParams.rightsValue"
          placeholder="请输入权益值(权益类型为是否时：0代表不支持，1代表支持；权益类型为数量限制时：0代表不限制，其他数字代表具体限制数量)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['rights:rights:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['rights:rights:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['rights:rights:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['rights:rights:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rightsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一主键" align="center" prop="id" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="用户类型" align="center" prop="userType" />
      <el-table-column label="会员卡id" align="center" prop="cardId" />
      <el-table-column label="权益编码" align="center" prop="rightsCode" />
      <el-table-column label="权益类型" align="center" prop="rightsType" />
      <el-table-column label="权益限制周期(数据字典：dl_rights_cycle)" align="center" prop="rightsCycle" />
      <el-table-column label="权益值(权益类型为是否时：0代表不支持，1代表支持；权益类型为数量限制时：0代表不限制，其他数字代表具体限制数量)" align="center" prop="rightsValue" />
      <el-table-column label="剩余值" align="center" prop="remaining" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['rights:rights:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['rights:rights:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="会员卡id" prop="cardId">
          <el-input v-model="form.cardId" placeholder="请输入会员卡id" />
        </el-form-item>
        <el-form-item label="权益编码" prop="rightsCode">
          <el-input v-model="form.rightsCode" placeholder="请输入权益编码" />
        </el-form-item>
        <el-form-item label="权益限制周期(数据字典：dl_rights_cycle)" prop="rightsCycle">
          <el-input v-model="form.rightsCycle" placeholder="请输入权益限制周期(数据字典：dl_rights_cycle)" />
        </el-form-item>
        <el-form-item label="权益值(权益类型为是否时：0代表不支持，1代表支持；权益类型为数量限制时：0代表不限制，其他数字代表具体限制数量)" prop="rightsValue">
          <el-input v-model="form.rightsValue" placeholder="请输入权益值(权益类型为是否时：0代表不支持，1代表支持；权益类型为数量限制时：0代表不限制，其他数字代表具体限制数量)" />
        </el-form-item>
        <el-form-item label="剩余值" prop="remaining">
          <el-input v-model="form.remaining" placeholder="请输入剩余值" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRights, getRights, delRights, addRights, updateRights } from "@/api/member/rights";

export default {
  name: "Rights",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1表格数据
      rightsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userType: null,
        rightsType: null,
        rightsValue: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1列表 */
    getList() {
      this.loading = true;
      listRights(this.queryParams).then(response => {
        this.rightsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        userType: null,
        cardId: null,
        rightsCode: null,
        rightsType: null,
        rightsCycle: null,
        rightsValue: null,
        remaining: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRights(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRights(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRights(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会员权益(定时任务重置剩余值)，用户id、会员卡id、权益对应1编号为"' + ids + '"的数据项？').then(function() {
        return delRights(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('rights/rights/export', {
        ...this.queryParams
      }, `rights_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
