Files
cunkebao_v3/SuperAdmin/app/dashboard/projects/new/page.tsx

207 lines
6.6 KiB
TypeScript
Raw Normal View History

2025-04-09 09:45:06 +08:00
"use client"
import type React from "react"
import { useState } from "react"
import { useRouter } from "next/navigation"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Textarea } from "@/components/ui/textarea"
import { Label } from "@/components/ui/label"
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
import { ArrowLeft } from "lucide-react"
2025-04-09 09:45:06 +08:00
import Link from "next/link"
2025-04-15 17:00:25 +08:00
import { toast, Toaster } from "sonner"
import { apiRequest } from "@/lib/api-utils"
2025-04-09 09:45:06 +08:00
export default function NewProjectPage() {
const router = useRouter()
const [isSubmitting, setIsSubmitting] = useState(false)
const [formData, setFormData] = useState({
name: "",
account: "",
password: "",
confirmPassword: "",
phone: "",
nickname: "",
description: "",
status: "1" // 默认启用
})
const handleChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const { id, value } = e.target
setFormData(prev => ({
...prev,
[id]: value
}))
2025-04-09 09:45:06 +08:00
}
const handleStatusChange = (value: string) => {
setFormData(prev => ({
...prev,
status: value
}))
}
const handleSubmit = async (e: React.FormEvent) => {
2025-04-09 09:45:06 +08:00
e.preventDefault()
if (formData.password !== formData.confirmPassword) {
toast.error("两次输入的密码不一致")
return
}
2025-04-09 09:45:06 +08:00
setIsSubmitting(true)
try {
const result = await apiRequest('/company/add', 'POST', {
name: formData.name,
account: formData.account,
password: formData.password,
memo: formData.description,
phone: formData.phone,
username: formData.nickname,
status: parseInt(formData.status),
})
if (result.code === 200) {
toast.success("项目创建成功")
router.push("/dashboard/projects")
} else {
toast.error(result.msg || "创建失败")
}
} catch (error) {
2025-04-15 17:00:25 +08:00
toast.error("网络错误,请稍后重试")
} finally {
2025-04-09 09:45:06 +08:00
setIsSubmitting(false)
}
2025-04-09 09:45:06 +08:00
}
return (
<div className="space-y-6">
2025-04-15 17:00:25 +08:00
<Toaster richColors position="top-center" />
2025-04-09 09:45:06 +08:00
<div className="flex items-center gap-2">
<Button variant="outline" size="icon" asChild>
<Link href="/dashboard/projects">
<ArrowLeft className="h-4 w-4" />
</Link>
</Button>
<h1 className="text-2xl font-bold"></h1>
</div>
<form onSubmit={handleSubmit}>
<Card>
<CardHeader>
<CardTitle></CardTitle>
<CardDescription></CardDescription>
</CardHeader>
<CardContent className="space-y-6">
<div className="space-y-2">
<Label htmlFor="name"></Label>
<Input
id="name"
placeholder="请输入项目名称"
required
value={formData.name}
onChange={handleChange}
/>
2025-04-09 09:45:06 +08:00
</div>
<div className="grid gap-6 md:grid-cols-2">
<div className="space-y-2">
<Label htmlFor="account"></Label>
<Input
id="account"
placeholder="请输入登录的账号"
required
value={formData.account}
onChange={handleChange}
/>
2025-04-09 09:45:06 +08:00
</div>
<div className="space-y-2">
<Label htmlFor="nickname"></Label>
<Input
id="nickname"
placeholder="用于账号登录后显示的用户名,可以填真实姓名"
required
value={formData.nickname}
onChange={handleChange}
/>
</div>
<div className="space-y-2">
<Label htmlFor="phone"></Label>
<Input
id="phone"
type="number"
placeholder="手机号可用于登录"
required
value={formData.phone}
onChange={handleChange}
/>
</div>
<div className="space-y-2">
<Label htmlFor="status"></Label>
<Select value={formData.status} onValueChange={handleStatusChange}>
<SelectTrigger>
<SelectValue placeholder="请选择状态" />
</SelectTrigger>
<SelectContent>
<SelectItem value="1"></SelectItem>
<SelectItem value="0"></SelectItem>
</SelectContent>
</Select>
</div>
2025-04-09 09:45:06 +08:00
<div className="space-y-2">
<Label htmlFor="password"></Label>
<Input
id="password"
type="password"
placeholder="请设置初始密码"
required
value={formData.password}
onChange={handleChange}
/>
2025-04-09 09:45:06 +08:00
</div>
<div className="space-y-2">
<Label htmlFor="confirmPassword"></Label>
<Input
id="confirmPassword"
type="password"
placeholder="请再次输入密码"
required
value={formData.confirmPassword}
onChange={handleChange}
/>
2025-04-09 09:45:06 +08:00
</div>
</div>
<div className="space-y-2">
<Label htmlFor="description"></Label>
<Textarea
id="description"
placeholder="请输入项目介绍(选填)"
rows={4}
value={formData.description}
onChange={handleChange}
/>
2025-04-09 09:45:06 +08:00
</div>
</CardContent>
<CardFooter className="flex justify-end gap-2">
<Button variant="outline" asChild>
<Link href="/dashboard/projects"></Link>
</Button>
<Button type="submit" disabled={isSubmitting}>
{isSubmitting ? "创建中..." : "创建项目"}
</Button>
</CardFooter>
</Card>
</form>
</div>
)
}