<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { http, type PageResult } from '../api/http';

const query = reactive({ keyword: '', role: '', status: undefined as number | undefined, page: 1, size: 20 });
const page = ref<PageResult<any>>({ records: [], total: 0, page: 1, size: 20 });
const dialog = ref(false);
const form = reactive({ username: '', password: '', displayName: '', email: '', role: 'OPERATOR' });

async function load() {
  page.value = await http.get('/admin-users', { params: query });
}

async function create() {
  await http.post('/admin-users', form);
  dialog.value = false;
  await load();
}

async function update(row: any) {
  await http.patch(`/admin-users/${row.id}`, row);
  await load();
}

async function reset(row: any) {
  const res = await http.post<any>(`/admin-users/${row.id}/reset-password`);
  ElMessage.success(`新密码：${res.password}`);
}

onMounted(load);
</script>

<template>
  <div>
    <div class="page-head">
      <div><h1>运营人员</h1><p>平台后台账号与角色</p></div>
      <el-button type="primary" @click="dialog = true">新建账号</el-button>
    </div>
    <div class="toolbar">
      <el-input v-model="query.keyword" placeholder="账号 / 姓名 / 邮箱" style="width: 260px" />
      <el-select v-model="query.role" placeholder="角色" clearable style="width: 180px">
        <el-option label="SUPER_ADMIN" value="SUPER_ADMIN" /><el-option label="OPERATOR" value="OPERATOR" /><el-option label="FINANCE" value="FINANCE" /><el-option label="READONLY" value="READONLY" />
      </el-select>
      <el-button type="primary" @click="load">查询</el-button>
    </div>
    <div class="panel">
      <el-table :data="page.records">
        <el-table-column prop="username" label="账号" />
        <el-table-column prop="displayName" label="姓名" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column label="角色"><template #default="{ row }"><el-select v-model="row.role" @change="update(row)"><el-option label="SUPER_ADMIN" value="SUPER_ADMIN" /><el-option label="OPERATOR" value="OPERATOR" /><el-option label="FINANCE" value="FINANCE" /><el-option label="READONLY" value="READONLY" /></el-select></template></el-table-column>
        <el-table-column prop="status" label="状态" width="90" />
        <el-table-column label="操作" width="120"><template #default="{ row }"><el-button text @click="reset(row)">重置</el-button></template></el-table-column>
      </el-table>
    </div>
    <el-dialog v-model="dialog" title="新建运营账号" width="460px">
      <el-form label-position="top">
        <el-form-item label="账号"><el-input v-model="form.username" /></el-form-item>
        <el-form-item label="初始密码"><el-input v-model="form.password" show-password /></el-form-item>
        <el-form-item label="姓名"><el-input v-model="form.displayName" /></el-form-item>
        <el-form-item label="邮箱"><el-input v-model="form.email" /></el-form-item>
        <el-form-item label="角色"><el-select v-model="form.role"><el-option label="OPERATOR" value="OPERATOR" /><el-option label="FINANCE" value="FINANCE" /><el-option label="READONLY" value="READONLY" /><el-option label="SUPER_ADMIN" value="SUPER_ADMIN" /></el-select></el-form-item>
      </el-form>
      <template #footer><el-button @click="dialog = false">取消</el-button><el-button type="primary" @click="create">创建</el-button></template>
    </el-dialog>
  </div>
</template>
