<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleAllDelete"
          >删除</el-button
        >
      </el-col> -->
      <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="benefitName"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column
        label="权益描述"
        align="center"
        width="200"
        prop="benefitDesc"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="权益类型"
        align="center"
        width="200"
        prop="benefitType"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="权益折扣"
        align="center"
        width="200"
        prop="benefitDiscount"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column
        label="排序"
        align="center"
        width="200"
        prop="benefitOrder"
        :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-plus"
            @click="handleToSubData(scope.row)"
            >添加</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.benefitName"
                type="input"
                placeholder="请输入权益名称"
              ></el-input>
            </el-form-item>
            <!-- <el-form-item label="权益描述" required>
              <el-input
                v-model="form.benefitDesc"
                type="input"
                placeholder="请输入权益描述"
              ></el-input>
            </el-form-item>
            <el-form-item label="权益类型" prop="benefitType" required>
              <el-select
                v-model="form.benefitType"
                placeholder="选择权益类型"
                clearable
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.member_benefit_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="权益折扣" required>
              <el-input
                v-model="form.benefitDiscount"
                type="number"
                placeholder="请输入权益折扣"
              ></el-input>
            </el-form-item> -->
            <el-form-item label="排序" required>
              <el-input
                v-model="form.benefitOrder"
                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.benefitName"
                type="input"
                placeholder="请输入权益名称"
              ></el-input>
            </el-form-item>
            <!-- <el-form-item label="权益描述" required>
              <el-input
                v-model="editForm.benefitDesc"
                type="input"
                placeholder="请输入权益描述"
              ></el-input>
            </el-form-item>
            <el-form-item label="权益类型" prop="benefitType" required>
              <el-select
                v-model="editForm.benefitType"
                placeholder="选择权益类型"
                clearable
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.member_benefit_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="权益折扣" required>
              <el-input
                v-model="editForm.benefitDiscount"
                type="number"
                placeholder="请输入权益折扣"
              ></el-input>
            </el-form-item> -->
            <el-form-item label="排序" required>
              <el-input
                v-model="editForm.benefitOrder"
                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>
  </div>
</template>

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

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

export default {
  name: "report",
  dicts: ["member_benefit_type"],
  data() {
    return {
      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()
  },
  methods: {
    handleToSubData(row){
      this.$router.push({ path: '/memberRights/subRight/', query: { id: row.id } })
    },
    // 编辑
    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() {
    //     getDict(this.addDateRange({type:'memberBenefitType'})).then((response) => {

    //   });
    // },
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      rightList(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
        this.list = response.data;
        // this.total = response.total;
        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>
