'use client';

import React from 'react';
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogFooter } from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { Switch } from '@/components/ui/switch';
import { CategoryFormData, CategoryDictionary } from '../types';

interface CategoryFormProps {
  open: boolean;
  editing: CategoryDictionary | null;
  formData: CategoryFormData;
  onFormDataChange: (data: Partial<CategoryFormData>) => void;
  onOpenChange: (open: boolean) => void;
  onSave: () => void;
}

export function CategoryForm({
  open,
  editing,
  formData,
  onFormDataChange,
  onOpenChange,
  onSave,
}: CategoryFormProps) {
  const handleSave = () => {
    if (!formData.code.trim() || !formData.name.trim() || !formData.value.trim() || !formData.label.trim()) {
      return false;
    }
    onSave();
    return true;
  };

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="max-w-2xl">
        <DialogHeader>
          <DialogTitle>
            {editing ? '编辑字典' : '新增字典'}
          </DialogTitle>
          <DialogDescription className="sr-only">
            {editing ? '编辑数据字典' : '添加新数据字典'}
          </DialogDescription>
        </DialogHeader>
        <div className="space-y-4">
          <div className="grid grid-cols-2 gap-4">
            <div>
              <Label>编码 *</Label>
              <Input
                value={formData.code}
                onChange={(e) => onFormDataChange({ code: e.target.value })}
                placeholder="GENDER_MALE"
                disabled={editing?.isSystem}
              />
            </div>
            <div>
              <Label>名称 *</Label>
              <Input
                value={formData.name}
                onChange={(e) => onFormDataChange({ name: e.target.value })}
                placeholder="性别-男"
              />
            </div>
          </div>
          <div>
            <Label>字典分类 *</Label>
            <Select
              value={formData.category}
              onValueChange={(value) => onFormDataChange({ category: value })}
              disabled={editing?.isSystem}
            >
              <SelectTrigger>
                <SelectValue />
              </SelectTrigger>
              <SelectContent>
                <SelectItem value="gender">性别</SelectItem>
                <SelectItem value="status">状态</SelectItem>
                <SelectItem value="unit">单位</SelectItem>
                <SelectItem value="weather">天气</SelectItem>
                <SelectItem value="soil_type">土壤类型</SelectItem>
                <SelectItem value="irrigation_method">灌溉方式</SelectItem>
                <SelectItem value="fertilizer_type">肥料类型</SelectItem>
                <SelectItem value="pesticide_type">农药类型</SelectItem>
                <SelectItem value="task_status">任务状态</SelectItem>
                <SelectItem value="task_priority">任务优先级</SelectItem>
                <SelectItem value="approval_status">审批状态</SelectItem>
                <SelectItem value="operation_type">作业类型</SelectItem>
                <SelectItem value="other">其他</SelectItem>
              </SelectContent>
            </Select>
          </div>
          <div className="grid grid-cols-2 gap-4">
            <div>
              <Label>值 *</Label>
              <Input
                value={formData.value}
                onChange={(e) => onFormDataChange({ value: e.target.value })}
                placeholder="male"
                disabled={editing?.isSystem}
              />
              <p className="text-xs text-muted-foreground mt-1">
                程序中使用的值，建议使用英文
              </p>
            </div>
            <div>
              <Label>标签 *</Label>
              <Input
                value={formData.label}
                onChange={(e) => onFormDataChange({ label: e.target.value })}
                placeholder="男"
              />
              <p className="text-xs text-muted-foreground mt-1">
                界面上显示的文本
              </p>
            </div>
          </div>
          <div>
            <Label>描述</Label>
            <Textarea
              value={formData.description}
              onChange={(e) => onFormDataChange({ description: e.target.value })}
              placeholder="请输入描述"
              rows={2}
            />
          </div>
          <div className="grid grid-cols-2 gap-4">
            <div>
              <Label>排序</Label>
              <Input
                type="number"
                value={formData.sortOrder}
                onChange={(e) => onFormDataChange({ sortOrder: parseInt(e.target.value) || 0 })}
              />
            </div>
            <div className="flex items-center justify-between pt-6">
              <Label>是否启用</Label>
              <Switch
                checked={formData.isActive}
                onCheckedChange={(checked) => onFormDataChange({ isActive: checked })}
              />
            </div>
          </div>
        </div>
        <DialogFooter>
          <Button variant="outline" onClick={() => onOpenChange(false)}>
            取消
          </Button>
          <Button onClick={handleSave}>
            保存
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}