'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 { CategoryDictionary, CategoryFormData } from '../types';

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

export function CategoryFormDialog({
  open,
  editing,
  parent,
  formData,
  onOpenChange,
  onFormDataChange,
  onSave,
}: CategoryFormDialogProps) {
  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="sm:max-w-[425px]">
        <DialogHeader>
          <DialogTitle>
            {editing ? '编辑分类' : '新增分类'}
          </DialogTitle>
          <DialogDescription className="sr-only">
            {editing ? '编辑分类信息' : '添加新分类'}
          </DialogDescription>
        </DialogHeader>
        <div className="space-y-4">
          {parent && (
            <div className="p-3 bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg">
              <Label className="text-sm text-blue-900 dark:text-blue-100">上级分类</Label>
              <p className="mt-1 dark:text-gray-100">{parent.name}</p>
            </div>
          )}
          <div className="grid grid-cols-2 gap-4">
            <div>
              <Label htmlFor="code">分类编码 *</Label>
              <Input
                id="code"
                value={formData.code}
                onChange={(e) => onFormDataChange({ code: e.target.value })}
                placeholder="IND001"
              />
            </div>
            <div>
              <Label htmlFor="name">分类名称 *</Label>
              <Input
                id="name"
                value={formData.name}
                onChange={(e) => onFormDataChange({ name: e.target.value })}
                placeholder="请输入名称"
              />
            </div>
          </div>
          <div>
            <Label htmlFor="type">分类类型</Label>
            <Select
              value={formData.type}
              onValueChange={(value) => onFormDataChange({ type: value })}
              disabled={!!parent}
            >
              <SelectTrigger>
                <SelectValue />
              </SelectTrigger>
              <SelectContent>
                <SelectItem value="industry">行业类型</SelectItem>
                <SelectItem value="equipment">设备类型</SelectItem>
                <SelectItem value="crop">作物类型</SelectItem>
                <SelectItem value="operation">作业类型</SelectItem>
                <SelectItem value="other">其他</SelectItem>
              </SelectContent>
            </Select>
          </div>
          <div>
            <Label htmlFor="description">描述</Label>
            <Textarea
              id="description"
              value={formData.description}
              onChange={(e) => onFormDataChange({ description: e.target.value })}
              placeholder="请输入描述"
              rows={3}
            />
          </div>
          <div className="grid grid-cols-2 gap-4">
            <div>
              <Label htmlFor="sortOrder">排序</Label>
              <Input
                id="sortOrder"
                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 htmlFor="isActive">是否启用</Label>
              <Switch
                id="isActive"
                checked={formData.isActive}
                onCheckedChange={(checked) => onFormDataChange({ isActive: checked })}
              />
            </div>
          </div>
        </div>
        <DialogFooter>
          <Button variant="outline" onClick={() => onOpenChange(false)}>
            取消
          </Button>
          <Button onClick={onSave}>
            保存
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}