<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="searchKey">
        <el-input
          v-model="queryParams.searchKey"
          placeholder="请输入手机号或昵称"
          clearable
          style="width: 240px"
          @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="userList">
      <el-table-column label="用户id" align="center" prop="id" width="80" />
      <el-table-column label="手机号" align="center" prop="phone" width="120" />
      <el-table-column label="昵称" align="center" prop="nickname" :show-overflow-tooltip="true" />
      <el-table-column label="性别" align="center" prop="gender" width="60">
        <template slot-scope="scope">
          <span>{{ scope.row.gender === '1' ? '男' : scope.row.gender === '2' ? '女' : '未知' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生日" align="center" prop="birthday" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="城市" align="center" prop="addr" :show-overflow-tooltip="true" />
      <el-table-column label="注册时间" align="center" prop="registerTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === '1' ? 'success' : 'danger'" size="mini">
            {{ scope.row.status === '1' ? '正常' : '黑名单' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="会员状态" align="center" prop="vipStatus" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.vipStatus === '1' ? 'vip' : '普通用户' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="播放标签" align="center" prop="playTag" width="100">
        <template slot-scope="scope">
          <el-tag type="warning" size="mini">{{ scope.row.playTag }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="本周在线时长" align="center" prop="online" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.online }}小时</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            :style="{ color: scope.row.status === '1' ? '#F56C6C' : '#67C23A' }"
            @click="handleStatusChange(scope.row)"
          >
            {{ scope.row.status === '1' ? '加入黑名单' : '取消加入黑名单' }}
          </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 { listUser, updateUserStatus, getUserTags } from "@/api/user/user";

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchKey: null
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;

        // 获取每个用户的播放标签
        this.userList.forEach(user => {
          getUserTags(user.userId).then(tagResponse => {
            if (tagResponse.code === 200 && tagResponse.data) {
              this.$set(user, 'playTag', tagResponse.data);
            } else {
              this.$set(user, 'playTag', '暂无标签');
            }
          }).catch(() => {
            this.$set(user, 'playTag', '获取失败');
          });
        });

        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 状态修改 */
    handleStatusChange(row) {
      const statusText = row.status === '1' ? "加入黑名单" : "取消加入黑名单";
      const newStatus = row.status === '1' ? '2' : '1';

      this.$modal.confirm('确认要' + statusText + '用户"' + row.nickname + '"吗？').then(function() {
        return updateUserStatus(row.id, newStatus);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(statusText + "成功");
      }).catch(() => {});
    }
  }
};
</script>

<style scoped>
.el-tag {
  border-radius: 12px;
}
</style>
