<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="'邀请人'+systemName+'号'" prop="inviteUsercode">
        <el-input
            v-model="queryParams.inviteUsercode"
            :placeholder="'请输入邀请人'+systemName+'号'"
            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="'被邀请人'+systemName+'号'" label-width="110px" prop="usercode">
        <el-input
            v-model="queryParams.usercode"
            :placeholder="'请输入被邀请人'+systemName+'号'"
            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="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-table v-loading="loading" :data="userInviteList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="邀请人" align="center">
        <el-table-column :label="systemName+'号'" 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>
      <el-table-column label="被邀请人" align="center">
        <el-table-column :label="systemName+'号'" align="center" prop="usercode"/>
        <el-table-column label="手机号" align="center" prop="mobile"/>
        <el-table-column label="收益" align="center" prop="rewardCoinTotal"/>
      </el-table-column>
<!--      <el-table-column label="所属工会" align="center" prop="unionName"/>-->
      <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>
        </template>
      </el-table-column>
    </el-table>

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

<script>
import {listUserInvite, updateUserInvite} from "@/api/cai/userInvite";
import {booleanList, isAnchorList} from "@/constant/statusMap";

export default {
  name: "UserInvite",
  components: {
  },
  data() {
    return {
      systemName: process.env.VUE_APP_SYSTEM_HOME,
      isAnchorList,booleanList,
      // 遮罩层
      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
    },
    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>
