<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="list">
      <el-table-column prop="level" align="center" label="会员等级">
      </el-table-column>
      <el-table-column prop="roleName" align="center" label="等级名称">
      </el-table-column>
      <el-table-column prop="outerRoleName" align="center" label="输出名称">
      </el-table-column>
      <el-table-column prop="threshold" align="center" label="升级消费金额">
      </el-table-column>
      <el-table-column prop="upgradeNum" align="center" label="升级推荐人数">
      </el-table-column>
      <el-table-column prop="discount" align="center" label="会员折扣">
        <template slot-scope="scope">
          <span>{{scope.row.discount}}折</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope.row)">删除</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" />
    <el-drawer :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="用户角色名称" prop="roleName" v-if="!form.id">
          <el-input v-model="form.roleName" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="用户角色标记码" prop="roleCode" v-if="!form.id">
          <el-input v-model="form.roleCode" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="角色等级" prop="level" v-if="!form.id">
          <el-input-number v-model="form.level" controls-position="right" :precision="0" :min="0" :max="100"></el-input-number>
        </el-form-item>
        <el-form-item label="会员折扣" prop="discount">
          <el-input-number v-model="form.discount" controls-position="right" :precision="1" :min="0" :max="100"></el-input-number>
        </el-form-item>
        <el-form-item label="输出的等级名称" prop="outerRoleName">
          <el-input v-model="form.outerRoleName" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="升级消费金额" prop="threshold">
          <el-input-number v-model="form.threshold" controls-position="right" :precision="2" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="升级推荐人数" prop="upgradeNum">
          <el-input-number v-model="form.upgradeNum" controls-position="right" :precision="0" :min="0"></el-input-number>
        </el-form-item>
      </el-form>
      <div class="demo-drawer__footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { businessUserRoleConfigAdd, businessUserRoleConfigEdit, businessUserRoleConfigInfo, businessUserRoleConfigDel, businessUserRoleConfigPage } from "@/api/marketing";
export default {
  data () {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 创建时间时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      rules: {
        roleName: [
          { required: true, message: '用户角色名称不能为空', trigger: 'blur' }
        ],
        roleCode: [
          { required: true, message: '用户角色标记码不能为空', trigger: 'blur' }
        ],
        level: [
          { required: true, message: '角色等级不能为空', trigger: 'blur' }
        ],
        discount: [
          { required: true, message: '会员折扣不能为空', trigger: 'blur' }
        ],
        outerRoleName: [
          { required: true, message: '输出的等级名称不能为空', trigger: 'blur' }
        ],
        threshold: [
          { required: true, message: '消费的阈值不能为空', trigger: 'blur' }
        ],
        upgradeNum: [
          { required: true, message: '升级推荐人数不能为空', trigger: 'blur' }
        ]
      },
      // 表单参数
      form: {
        roleName: undefined,
        roleCode: undefined,
        discount: undefined,
        outerRoleName: undefined,
        threshold: undefined,
        upgradeNum: undefined
      },
    }
  },
  created () {
    this.getList();
  },
  methods: {
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    // 表单重置
    reset () {
      // this.form = {};
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      businessUserRoleConfigInfo(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    getList () {
      this.loading = true;
      // this.queryParams["createTimeStart"] = undefined;
      // this.queryParams["createTimeEnd"] = undefined;
      // if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
      //   this.queryParams["createTimeStart"] = this.daterangeCreateTime[0] + ' 00:00:00';
      //   this.queryParams["createTimeEnd"] = this.daterangeCreateTime[1] + ' 23:59:59';
      // }
      businessUserRoleConfigPage(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            businessUserRoleConfigEdit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            businessUserRoleConfigAdd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id;
      this.$modal.confirm('是否确认删除会员等级名称为"' + row.roleName + '"的数据项？').then(() => {
        this.loading = true;
        return businessUserRoleConfigDel(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
  }
}
</script>

<style lang='scss' scoped>
::v-deep .el-drawer__body {
  padding: 0 20px !important;
}
.demo-drawer__footer {
  margin-left: 120px;
}
::v-deep .el-input.is-disabled .el-input__inner {
  background-color: #fff;
  color: rgb(96, 98, 102);
}
</style>