'use client';

import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from '@/components/ui/alert-dialog';
import { SoilDataState, SoilDataAction } from './soilDataReducer';

interface DeleteConfirmDialogProps {
  state: SoilDataState;
  dispatch: React.Dispatch<SoilDataAction>;
}

export default function DeleteConfirmDialog({ state, dispatch }: DeleteConfirmDialogProps) {
  const handleConfirmDelete = () => {
    if (state.pointToDelete) {
      dispatch({ type: 'DELETE_SAMPLE_POINT', payload: state.pointToDelete });
      dispatch({ type: 'SET_POINT_TO_DELETE', payload: null });
      dispatch({ type: 'SET_SHOW_DELETE_DIALOG', payload: false });
    }
  };

  const handleCancel = () => {
    dispatch({ type: 'SET_POINT_TO_DELETE', payload: null });
    dispatch({ type: 'SET_SHOW_DELETE_DIALOG', payload: false });
  };

  const pointToDelete = state.samplePoints.find(p => p.id === state.pointToDelete);

  return (
    <AlertDialog open={state.showDeleteDialog} onOpenChange={(open) => {
      if (!open) {
        handleCancel();
      }
    }}>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>确认删除采样点</AlertDialogTitle>
          <AlertDialogDescription>
            确定要删除采样点{pointToDelete?.code}吗？
            {pointToDelete && (
              <span className="block mt-2 text-muted-foreground">
                位置：{pointToDelete.fieldName} | 采样日期：{pointToDelete.sampleDate}
              </span>
            )}
            此操作将删除该采样点的所有数据，包括分层信息和理化指标，且无法恢复。
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel onClick={handleCancel}>
            取消
          </AlertDialogCancel>
          <AlertDialogAction
            onClick={handleConfirmDelete}
            className="bg-destructive hover:bg-destructive/90"
          >
            确认删除
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}