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

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 pageSizes = [10, 20, 30];
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 });
}

function search() {
  query.page = 1;
  load();
}

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}`);
}

function statusLabel(status?: number) {
  return status === 1 ? '正常' : '禁用';
}

onMounted(load);
</script>

<template>
  <div>
    <div class="page-head">
      <div><h1>运营权限分配</h1><p>平台后台账号、角色与运营权限维护</p></div>
      <div class="page-actions">
        <el-button type="primary" @click="dialog = true">新建账号</el-button>
      </div>
    </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 v-for="role in roleOptions" :key="role.value" :label="role.label" :value="role.value" />
      </el-select>
      <el-button type="primary" @click="search">查询</el-button>
    </div>
    <div class="panel">
      <div class="panel-title">
        <div><b>运营账号列表</b><span>角色修改即时保存，高危账号操作会写入审计日志</span></div>
      </div>
      <el-table :data="page.records" class="module-table">
        <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 v-for="role in roleOptions" :key="role.value" :label="role.label" :value="role.value" /></el-select></template></el-table-column>
        <el-table-column label="状态" width="90"><template #default="{ row }">{{ statusLabel(row.status) }}</template></el-table-column>
        <el-table-column label="操作" width="120" fixed="right" align="center"><template #default="{ row }"><div class="row-actions"><el-button text @click="reset(row)">重置</el-button></div></template></el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="query.page"
        v-model:page-size="query.size"
        layout="total, sizes, prev, pager, next"
        :page-sizes="pageSizes"
        :total="page.total"
        @current-change="load"
        @size-change="search"
      />
    </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 v-for="role in roleOptions" :key="role.value" :label="role.label" :value="role.value" /></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>
