<!-- src/views/UserManagement.vue -->
<template>
  <div class="user-management">
    <div class="gva-search-box">
      <!-- 这里将放置搜索表单 -->
      <el-form :inline="true" class="search-form" :model="searchInfo">
        <el-form-item label="用户id">
          <el-input v-model="searchInfo.id" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="用户昵称">
          <el-input v-model="searchInfo.nickname" placeholder="昵称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onGetUser">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="gva-search-box">
      <!-- 这里将放置用户表格 -->
      <el-table :data="userList" border style="width: 100%">
        <el-table-column prop="id" label="id" align="left" min-width="150"></el-table-column>
        <el-table-column prop="nickname" label="用户昵称" align="left" min-width="150"></el-table-column>
        <el-table-column prop="gold" label="金币" align="left" min-width="150"></el-table-column>
        <el-table-column prop="status" label="状态" align="left" min-width="150">
          <template #default="scope">
            {{statusMap[scope.row.status]|| '未知状态' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" type="danger" >封号|解封</el-button>
            <el-button size="small" @click="openAddResourceDialog(scope.row)">添加资源</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 添加资源对话框 -->
      <add-resource-dialog
          v-model="dialogVisible"
          :user-id="selectedUserId"
          :nickname="selectedNickname"
      />
    </div>
  </div>
</template>


<script>
import AddResourceDialog from '@/view/gameuser/addResourceDialog.vue'
import {
  getUserById,
  getAccountByUid,
} from "@/api/gameUser";

export default {
  name: 'UserManagement',
  components:{
    AddResourceDialog,
  },

  data() {
    return {
      // 控制对话框
      dialogVisible: false,
      selectedUserId: null,
      selectedNickname:null,

      searchInfo :{
        id: 0,
        nickname: '',
      },
      statusMap : {
        1: '正常',
        2: '禁用',
        3:'白名单',
      },
      userList: [
        // {
        //   id: 1,
        //   nickname: '赵今麦',
        //   gold: 1501,
        //   status:0,
        // },
        // {
        //   id: 2,
        //   nickname: '高圆圆',
        //   gold: 1502,
        //   status:1,
        // },
        // {
        //   id: 3,
        //   nickname: '刘亦菲',
        //   gold: 1502,
        //   status:2,
        // },
      ]
    }
  },
  methods:{
    openAddResourceDialog(user){
      this.selectedUserId = user.id
      this.selectedNickname = user.nickname
      // console.log("open dialog.user:"+this.selectedUserId+' nickname:'+this.selectedNickname)
      this.dialogVisible = true
    },

    async onGetUser(){
      // 显示加载状态
      const loading = this.$loading({
        lock: true,
        text: '加载中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      try {
        // this.userList = [
        //   {
        //     id: 1,
        //     nickname: '赵今麦',
        //     gold: 1501,
        //     status:0,
        //   },
        //   {
        //     id: 2,
        //     nickname: '高圆圆',
        //     gold: 1502,
        //     status:1,
        //   },
        //   {
        //     id: 3,
        //     nickname: '刘亦菲',
        //     gold: 1502,
        //     status:2,
        //   },
        // ]
        const id = this.searchInfo.id // 或者从其他地方获取ID
        if (id) {
          const response = await getUserById({id:+id})
          if (response.code !== 0) {
            console.error('获取玩家数据失败:', response.msg || `请求失败，错误码: ${response.code}`)
            this.$message.error('获取用户数据失败:'+(response.msg || `请求失败，错误码: ${response.code}`))
            return
          }
          const retAccount = await getAccountByUid({id:+response.data.user.account_id})
          // console.error('获取玩家帐号数据:', retAccount)
          this.userList = [
            {
              id:response.data.user.id,
              nickname:response.data.user.nickname,
              gold:response.data.user.gold,
              status:retAccount.data.account.status,
            },
          ]
        } else {
          this.$message.warning('请输入用户ID')
        }
      }catch (error){
        console.error('获取玩家数据失败:', error)
        this.$message.error('获取用户数据失败:'+error)
      }finally {
        // 关闭加载状态
        loading.close()
      }
    }
  }
}
</script>

<style scoped>
.user-management {
  padding: 20px;
}
</style>

