<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="蜜瓜号" prop="usercode">
        <el-input
            v-model="queryParams.usercode"
            placeholder="请输入蜜瓜号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
            v-model="queryParams.mobile"
            placeholder="请输入手机号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邀请人蜜瓜号" prop="inviteUsercode">
        <el-input
            v-model="queryParams.inviteUsercode"
            placeholder="请输入邀请人蜜瓜号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邀请人手机号" prop="inviteMobile">
        <el-input
            v-model="queryParams.inviteMobile"
            placeholder="请输入邀请人手机号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工会名称" prop="unionName">
        <el-input
            v-model="queryParams.unionName"
            placeholder="请输入邀请人手机号"
            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">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userInviteList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="蜜瓜号" align="center" prop="usercode"/>
      <el-table-column label="手机号" align="center" prop="mobile"/>
      <el-table-column label="所属工会" align="center" prop="unionName"/>
      <el-table-column label="邀请人蜜瓜号" align="center" prop="inviteUsercode"/>
      <el-table-column label="邀请人手机号" align="center" prop="inviteMobile"/>
      <el-table-column label="邀请人为会长" align="center" prop="inviteIsUnion">
        <template v-slot="scope">
          <cai-dict-tag :options="isAnchorList" :value="scope.row.inviteIsUnion"/>
        </template>
      </el-table-column>
      <el-table-column label="好友守护分成" align="center" prop="guardIncomeRate" />
      <el-table-column label="好友礼物分成" align="center" prop="giftIncomeRate" />
      <el-table-column label="好友视频分成" align="center" prop="videoIncomeRate" />
      <el-table-column label="好友充值分成" align="center" prop="payIncomeRate" />
      <el-table-column label="是否有效" align="center" prop="enableRate" >
        <template v-slot="scope">
          <cai-dict-tag :options="booleanList" :value="scope.row.enableRate"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="!scope.row.enableRate"
            @click="handleEnableRate(scope.row,true)"
            v-hasPermi="['cai:userInvite:edit']"
          >开启</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-if="scope.row.enableRate"
              @click="handleEnableRate(scope.row,false)"
              v-hasPermi="['cai:userInvite:edit']"
          >关闭</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['cai:userInvite:edit']"
          >修改</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"
    />
    <update-invite-dialog v-if="updateInviteDialogVisible" ref="updateInviteDialog" @refreshDataList="getList" />
  </div>
</template>

<script>
import {listUserInvite, updateUserInvite} from "@/api/cai/userInvite";
import {booleanList, isAnchorList} from "@/constant/statusMap";
import UpdateInviteDialog from "@/views/cai/userInvite/update-invite-dialog";

export default {
  name: "UserInvite",
  components: {
    UpdateInviteDialog
  },
  data() {
    return {
      isAnchorList,booleanList,
      updateInviteDialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户邀请表格数据
      userInviteList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        usercode: undefined,
        mobile: undefined,
        inviteUsercode: undefined,
        inviteMobile: undefined,
        unionName: undefined,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户邀请列表 */
    getList() {
      this.loading = true;
      listUserInvite(this.queryParams).then(response => {
        this.userInviteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    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
    },
    handleUpdate(row){
      this.updateInviteDialogVisible = true
      this.$nextTick(() => {
        this.$refs.updateInviteDialog.init(row)
      })
    },
    handleEnableRate(row,enableRate) {
      let message = enableRate ? '开启' : '关闭';
      this.$modal.confirm('是否确认'+message+'用户"' + row.usercode + '"的好友邀请奖励？').then(() => {
        this.loading = true;
        return updateUserInvite({
          id:row.id,
          enableRate:enableRate
        });
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    }
  }
};
</script>
