"use client";

import { useState, useEffect } from "react";
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Checkbox } from "@/components/ui/checkbox";
import { DashboardElement } from "../types";

interface YardWidgetConfigModalProps {
  element: DashboardElement;
  isOpen: boolean;
  onClose: () => void;
  onSave: (updates: Partial<DashboardElement>) => void;
}

export function YardWidgetConfigModal({ element, isOpen, onClose, onSave }: YardWidgetConfigModalProps) {
  const [customTitle, setCustomTitle] = useState(element.customTitle || "");
  const [showHeader, setShowHeader] = useState(element.showHeader !== false);

  useEffect(() => {
    if (isOpen) {
      setCustomTitle(element.customTitle || "");
      setShowHeader(element.showHeader !== false);
    }
  }, [isOpen, element]);

  const handleSave = () => {
    onSave({
      customTitle,
      showHeader,
    });
    onClose();
  };

  return (
    <Dialog open={isOpen} onOpenChange={onClose}>
      <DialogContent onPointerDown={(e) => e.stopPropagation()} className="sm:max-w-[500px]">
        <DialogHeader>
          <DialogTitle>야드 관리 위젯 설정</DialogTitle>
        </DialogHeader>

        <div className="space-y-4 py-4">
          {/* 위젯 제목 */}
          <div className="space-y-2">
            <Label htmlFor="customTitle">위젯 제목</Label>
            <Input
              id="customTitle"
              value={customTitle}
              onChange={(e) => setCustomTitle(e.target.value)}
              placeholder="제목을 입력하세요 (비워두면 기본 제목 사용)"
            />
            <p className="text-xs text-muted-foreground">기본 제목: 야드 관리 3D</p>
          </div>

          {/* 헤더 표시 여부 */}
          <div className="flex items-center space-x-2">
            <Checkbox
              id="showHeader"
              checked={showHeader}
              onCheckedChange={(checked) => setShowHeader(checked === true)}
            />
            <Label htmlFor="showHeader" className="cursor-pointer text-sm font-normal">
              헤더 표시
            </Label>
          </div>
        </div>

        <div className="flex justify-end gap-2">
          <Button variant="outline" onClick={onClose}>
            취소
          </Button>
          <Button onClick={handleSave}>저장</Button>
        </div>
      </DialogContent>
    </Dialog>
  );
}
