<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { createUser } from '@/api/modules/system'
import type { CreateUserPayload } from '@/types'

const router = useRouter()
const submitting = ref(false)

const form = reactive<CreateUserPayload>({
  username: '',
  display_name: '',
  password: 'platform123',
  role: 'operator',
  status: 'active',
  permissions: [],
})

async function submit() {
  submitting.value = true
  try {
    await createUser(form)
    ElMessage.success('用户已创建')
    router.push('/user-settings')
  } finally {
    submitting.value = false
  }
}
</script>

<template>
  <section class="user-create">
    <h1>创建用户</h1>
    <el-form :model="form" label-width="110px" class="user-form">
      <el-form-item label="账号">
        <el-input v-model="form.username" placeholder="登录用户名" />
      </el-form-item>
      <el-form-item label="显示名称">
        <el-input v-model="form.display_name" placeholder="如：张三" />
      </el-form-item>
      <el-form-item label="初始密码">
        <el-input v-model="form.password" type="password" show-password />
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="form.role" style="width: 100%">
          <el-option label="管理员" value="admin" />
          <el-option label="普通用户" value="operator" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="form.status">
          <el-radio value="active">启用</el-radio>
          <el-radio value="disabled">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="权限说明">
        <el-alert type="info" :closable="false" show-icon>
          <template #title>
            <span v-if="form.role === 'admin'">管理员：拥有全部权限，包括用户管理、平台治理、算力节点</span>
            <span v-else>普通用户：可见服务看板、模型服务、数据治理、其他工具、平台性能、查看日志。数据集和微调模型仅创建者和被授权用户可见。</span>
          </template>
        </el-alert>
      </el-form-item>
      <el-form-item>
        <el-button @click="router.back()">返回</el-button>
        <el-button type="primary" :loading="submitting" @click="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </section>
</template>

<style scoped>
.user-create {
  padding: 24px;
}

.user-form {
  max-width: 640px;
}
</style>
