import { createFileRoute, useNavigate } from "@tanstack/react-router"; import { useGetRoleList, useCreateAccount } from "@/hooks/queries"; import { useState } from "react"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { UserPlus, ArrowLeft, Save, Loader2 } from "lucide-react"; import { toast } from "sonner"; export const Route = createFileRoute("/_auth/user/create/")({ component: CreateUserComponent, loader: async ({ context }) => { context.breadcrumbs = [ { title: "Quản lý người dùng", path: "#" }, { title: "Tạo người dùng mới", path: "/user/create" }, ]; }, }); function CreateUserComponent() { const navigate = useNavigate(); const { data: roles = [], isLoading: rolesLoading } = useGetRoleList(); const createMutation = useCreateAccount(); const [formData, setFormData] = useState({ userName: "", name: "", password: "", confirmPassword: "", roleId: "", }); const [errors, setErrors] = useState>({}); // Validate username theo regex backend const validateUserName = (userName: string): boolean => { const regex = /^[a-zA-Z0-9_.]{3,20}$/; return regex.test(userName); }; const validateForm = (): boolean => { const newErrors: Record = {}; // Validate username if (!formData.userName) { newErrors.userName = "Tên đăng nhập không được để trống"; } else if (!validateUserName(formData.userName)) { newErrors.userName = "Tên đăng nhập chỉ cho phép chữ cái, số, dấu chấm và gạch dưới (3-20 ký tự)"; } // Validate name if (!formData.name.trim()) { newErrors.name = "Họ và tên không được để trống"; } // Validate password if (!formData.password) { newErrors.password = "Mật khẩu không được để trống"; } else if (formData.password.length < 6) { newErrors.password = "Mật khẩu phải có ít nhất 6 ký tự"; } // Validate confirm password if (formData.password !== formData.confirmPassword) { newErrors.confirmPassword = "Mật khẩu xác nhận không khớp"; } // Validate roleId if (!formData.roleId) { newErrors.roleId = "Vui lòng chọn vai trò"; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!validateForm()) { return; } try { await createMutation.mutateAsync({ userName: formData.userName, name: formData.name, password: formData.password, roleId: Number(formData.roleId), accessRooms: [0], // Default value, will be updated when Room API provides IDs }); toast.success("Tạo tài khoản thành công!"); navigate({ to: "/dashboard" }); // TODO: Navigate to user list page when it exists } catch (error: any) { const errorMessage = error.response?.data?.message || "Tạo tài khoản thất bại!"; toast.error(errorMessage); } }; const handleInputChange = (field: string, value: string) => { setFormData((prev) => ({ ...prev, [field]: value })); // Clear error when user starts typing if (errors[field]) { setErrors((prev) => { const newErrors = { ...prev }; delete newErrors[field]; return newErrors; }); } }; return (
{/* Header */}

Tạo người dùng mới

Thêm tài khoản người dùng mới vào hệ thống

{/* Form */}
Thông tin tài khoản Điền thông tin để tạo tài khoản người dùng mới {/* Username and Name - Grid Layout */}
handleInputChange("userName", e.target.value)} placeholder="Nhập tên đăng nhập (3-20 ký tự, chỉ chữ, số, . và _)" disabled={createMutation.isPending} className="h-10" /> {errors.userName && (

{errors.userName}

)}
handleInputChange("name", e.target.value)} placeholder="Nhập họ và tên đầy đủ" disabled={createMutation.isPending} className="h-10" /> {errors.name && (

{errors.name}

)}
{/* Password */}
handleInputChange("password", e.target.value)} placeholder="Nhập mật khẩu (tối thiểu 6 ký tự)" disabled={createMutation.isPending} className="h-10" /> {errors.password && (

{errors.password}

)}
handleInputChange("confirmPassword", e.target.value)} placeholder="Nhập lại mật khẩu" disabled={createMutation.isPending} className="h-10" /> {errors.confirmPassword && (

{errors.confirmPassword}

)}
{/* Role Selection */}
{rolesLoading ? (
) : ( )} {errors.roleId && (

{errors.roleId}

)}
{/* TODO: Add Room/Building Access selection when API is ready */} {/*

Chọn các phòng mà người dùng có quyền truy cập

// Add multi-select component here when Room API provides IDs
*/}
{/* Actions */}
); }