<template>
  <div class="page">
    <section class="page-hero">
      <div>
        <h1>用户管理</h1>
        <p>维护租户下的用户、角色和区域数据权限范围。</p>
      </div>
      <el-tag type="primary" effect="light">用户 {{ users.length }}</el-tag>
    </section>
    <div class="toolbar">
      <el-input v-model="keyword" placeholder="搜索用户" style="width: 280px" clearable @change="handleFilterChange" />
      <el-button type="primary" round @click="dialogVisible = true">新增用户</el-button>
    </div>
    <el-card class="modern-card" shadow="never">
      <el-table :data="users" v-loading="loading" class="modern-table">
        <el-table-column prop="username" label="账号" />
        <el-table-column prop="name" label="姓名" />
        <el-table-column prop="phone" label="手机号" />
        <el-table-column label="状态">
          <template #default="{ row }">
            <el-tag :type="row.status === 'ACTIVE' ? 'success' : 'info'">{{ userStatusText(row.status) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="角色">
          <template #default="{ row }">{{ roleNames(row) }}</template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        layout="total, sizes, prev, pager, next"
        :total="total"
        :current-page="page"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        @current-change="handlePageChange"
        @size-change="handleSizeChange"
      />
    </el-card>
    <el-dialog v-model="dialogVisible" title="新增用户" width="520px">
      <el-form :model="form" label-width="90px">
        <el-form-item label="账号"><el-input v-model="form.username" /></el-form-item>
        <el-form-item label="姓名"><el-input v-model="form.name" /></el-form-item>
        <el-form-item label="密码"><el-input v-model="form.password" /></el-form-item>
        <el-form-item label="角色ID"><el-input v-model="roleIdsText" placeholder="多个用逗号分隔" /></el-form-item>
        <el-form-item label="区域ID"><el-input v-model="regionIdsText" placeholder="多个用逗号分隔" /></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="create">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue';
import { systemApi } from '../../api/modules';
import { userStatusText } from '../../utils/labels';

const users = ref<any[]>([]);
const keyword = ref('');
const loading = ref(false);
const dialogVisible = ref(false);
const roleIdsText = ref('');
const regionIdsText = ref('');
const form = reactive({ username: '', name: '', password: '' });
const page = ref(1);
const pageSize = ref(20);
const total = ref(0);

function roleNames(row: any) {
  return row.roles?.map((item: any) => item.role.name).join('、') || '-';
}

async function load() {
  loading.value = true;
  try {
    const result = await systemApi.users({
      keyword: keyword.value,
      page: page.value,
      pageSize: pageSize.value,
    });
    users.value = result.items ?? [];
    total.value = result.total ?? 0;
  } finally {
    loading.value = false;
  }
}

function handleFilterChange() {
  page.value = 1;
  void load();
}

function handlePageChange(value: number) {
  page.value = value;
  void load();
}

function handleSizeChange(value: number) {
  pageSize.value = value;
  page.value = 1;
  void load();
}

async function create() {
  await systemApi.createUser({
    ...form,
    roleIds: roleIdsText.value.split(',').map((item) => item.trim()).filter(Boolean),
    regionIds: regionIdsText.value.split(',').map((item) => item.trim()).filter(Boolean),
  });
  dialogVisible.value = false;
  await load();
}

onMounted(load);
</script>

<style scoped>
.pagination {
  margin-top: 16px;
  justify-content: flex-end;
}
</style>
