<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="filter-container">
      <span>手机号:</span>
      <el-input
        v-model="listQuery.mobile"
        placeholder="手机号"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="getList"
      />
      <span>状态:</span>
      <el-select
        v-model="listQuery.status"
        placeholder="邀请码状态"
        clearable
        style="width: 200px"
        class="filter-item"
        @change="getList"
      >
        <el-option label="全部" value="" />
        <el-option label="已绑定" :value="1" />
        <el-option label="未绑定" :value="0" />
        <el-option label="已过期" :value="2" />
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
      <el-button icon="el-icon-thumb" type="primary" @click="handleMakeGenerate">生成邀请码</el-button>
    </div>
    <!-- 搜索区域END -->
    <!-- 表格区域 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      border
      fit
      highlight-current-row
      cell-class-name="table-cell"
    >
      <el-table-column label="序号" :index="indexMethod" type="index" width="80px" align="center" />
      <el-table-column label="邀请码" prop="invitationCode" width="100px" align="center" />
      <el-table-column label="状态" width="100px" align="center">
        <template slot-scope="{ row }">
          <el-tag v-if="row.status === 0" size="small" type="warning">未绑定</el-tag>
          <el-tag v-if="row.status === 1" size="small" type="success">已绑定</el-tag>
          <el-tag v-if="row.status === 2" size="small" type="danger">已过期</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" width="200px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.addTime | formatTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="微信昵称" align="center">
        <template slot-scope="{row}">
          <span v-if="row.nickName">{{ row.nickName }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" width="140" align="center">
        <template slot-scope="{row}">
          <span v-if="row.mobile">{{ row.mobile }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>
      <el-table-column label="openID" width="280" align="center">
        <template slot-scope="{row}">
          <span v-if="row.openId">{{ row.openId }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>
      <el-table-column label="绑定时间" width="200px" align="center">
        <template slot-scope="{row}">
          <span v-if="row.bindTime">{{ row.bindTime | formatTime }}</span>
          <span v-else>—</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200px" align="center">
        <template slot-scope="{row}">
          <el-button v-if="row.status === 1" type="danger" size="mini" style="margin-right:5px" @click="handleUnbindGenerate(row)">解绑邀请码</el-button>
          <el-button v-else type="primary" size="mini" style="margin-right:5px" @click="handleUpdateGenerate(row)">更新邀请码</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格区域END -->
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList(listQuery)" />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { formatDate } from '@/utils'
import { mapGetters } from 'vuex'
import { getAuthUserList, makeGenerate, unbindGenerate, updateGenerate } from '@/api/auth'
export default {
  components: { Pagination },
  filters: {
    formatTime(time) {
      if (!time) {
        return ''
      }
      return formatDate(time, 'yyyy-MM-DD HH:mm:ss')
    }
  },
  data() {
    return {
      total: 0,
      authId: '', // 机构id
      listLoading: false,
      listQuery: {
        authId: '', // 机构id
        mobile: '', // 手机号
        status: '', // 邀请码状态
        pageNum: 0, // 页码
        pageSize: 10 // 分页大小
      },
      list: [],
      srcList: []
    }
  },
  computed: {
    ...mapGetters(['authUserId'])
  },
  created() {
    this.authId = this.$route.query.id
    this.getList()
  },
  methods: {
    // 获取列表数据
    getList() {
      this.listLoading = true
      this.listQuery.authId = this.authId
      getAuthUserList(this.listQuery)
        .then(res => {
          if (res.code !== 0) return
          this.list = res.data.list
          this.total = res.data.total
          console.log(res)
        })
        .finally(() => {
          this.listLoading = false
        })
    },
    // 生成邀请码
    handleMakeGenerate() {
      makeGenerate({ authId: this.authId })
        .then(res => {
          console.log(res)
          if (res.code !== 0) return
          this.$message({
            type: 'success',
            message: res.data,
            duration: 500
          })
          this.getList()
        })
    },
    // 更新邀请码
    handleUpdateGenerate(row) {
      updateGenerate({ clubUserId: row.clubUserId })
        .then(res => {
          if (res.code !== 0) return
          console.log(res)
          this.$message({
            type: 'success',
            message: res.data,
            duration: 500
          })
          this.getList()
        })
    },
    // 解绑邀请码
    handleUnbindGenerate(row) {
      unbindGenerate({ clubUserId: row.clubUserId })
        .then(res => {
          if (res.code !== 0) return
          console.log(res)
          this.$message({
            type: 'success',
            message: res.data,
            duration: 500
          })
          this.getList()
        })
    },
    indexMethod(index) {
      return index + this.listQuery.pageSize * (this.listQuery.pageNum - 1) + 1
    }
  }
}
</script>

<style lang="scss" scoped>
.filter-container {
  span {
    display: inline-block;
    margin-bottom: 10px;
    vertical-align: middle;
    font-size: 14px;
  }
  .el-button {
    display: inline-block;
    margin-bottom: 10px;
    vertical-align: middle;
  }
  .el-input,
  .el-select {
    margin-right: 10px;
    margin-left: 10px;
  }
}
.el-table .cell {
  overflow: visible;
}
.el-badge{
  margin: 0 6px;
}
</style>
