<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <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="['member:member: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="['member:member: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="['member:member: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="['member:member:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一主键" align="center" prop="id" />
      <el-table-column label="用户类型" align="center" prop="userType" />
      <el-table-column label="通告主身份类型,为null时就是未认证" align="center" prop="identityType" />
      <el-table-column label="联系方式" align="center" prop="tel" />
      <el-table-column label="通告主-本月通告发布剩余额度" align="center" prop="tRemaining" />
      <el-table-column label="通告主-个人累计已发布通告数量" align="center" prop="tTotalNum" />
      <el-table-column label="通告主-个人粉丝数量" align="center" prop="tFansNum" />
      <el-table-column label="通告主-是否开启报名免打扰(0否|1开启)" align="center" prop="tOpenDisturb" />
      <el-table-column label="通告主-收款码姓名" align="center" prop="tRecipientName" />
      <el-table-column label="通告主-收款码图片地址" align="center" prop="tRecipientImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.tRecipientImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="博主-剩余积分" align="center" prop="bPoints" />
      <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="['member:member:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['member:member: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"
    />

    <!-- 添加或修改会员对话框 -->
    <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="联系方式" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="通告主-本月通告发布剩余额度" prop="tRemaining">
          <el-input v-model="form.tRemaining" placeholder="请输入通告主-本月通告发布剩余额度" />
        </el-form-item>
        <el-form-item label="通告主-个人累计已发布通告数量" prop="tTotalNum">
          <el-input v-model="form.tTotalNum" placeholder="请输入通告主-个人累计已发布通告数量" />
        </el-form-item>
        <el-form-item label="通告主-个人粉丝数量" prop="tFansNum">
          <el-input v-model="form.tFansNum" placeholder="请输入通告主-个人粉丝数量" />
        </el-form-item>
        <el-form-item label="通告主-是否开启报名免打扰(0否|1开启)" prop="tOpenDisturb">
          <el-input v-model="form.tOpenDisturb" placeholder="请输入通告主-是否开启报名免打扰(0否|1开启)" />
        </el-form-item>
        <el-form-item label="通告主-收款码姓名" prop="tRecipientName">
          <el-input v-model="form.tRecipientName" placeholder="请输入通告主-收款码姓名" />
        </el-form-item>
        <el-form-item label="通告主-收款码图片地址" prop="tRecipientImage">
          <image-upload v-model="form.tRecipientImage"/>
        </el-form-item>
        <el-form-item label="博主-剩余积分" prop="bPoints">
          <el-input v-model="form.bPoints" 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 { listMember, getMember, delMember, addMember, updateMember } from "@/api/member/member";

export default {
  name: "Member",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员表格数据
      memberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userType: null,
        identityType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userType: null,
        identityType: null,
        userId: null,
        tel: null,
        tRemaining: null,
        tTotalNum: null,
        tFansNum: null,
        tOpenDisturb: null,
        tRecipientName: null,
        tRecipientImage: null,
        bPoints: null,
        creator: null,
        createTime: null,
        updater: null,
        updateTime: null,
        delFlag: 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 = "添加会员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMember(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMember(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMember(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会员编号为"' + ids + '"的数据项？').then(function() {
        return delMember(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('member/member/export', {
        ...this.queryParams
      }, `member_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
