"use client";

import React, { useState } from "react";
import Link from "next/link";
import {
  Search,
  RotateCcw,
  Plus,
  Eye,
  Edit2,
  Trash2,
  ChevronLeft,
  ChevronRight,
  FileSpreadsheet,
  FileText,
  Award,
  Sparkles,
  CheckCircle2,
  XCircle,
  FileCheck2,
} from "lucide-react";
import { toast } from "sonner";

interface CondonationClaim {
  id: string;
  rollNumber: string;
  studentName: string;
  eventName: string;
  dutyType: string;
  grantedHours: number;
  reviewedBy: string;
  status: "Approved" | "Pending" | "Rejected";
  remarks: string;
}

const initialCondonations: CondonationClaim[] = [
  {
    id: "cond-101",
    rollNumber: "2441112007",
    studentName: "Harshavardhan Rao",
    eventName: "National Culinary Challenge 2026",
    dutyType: "NCHMCT Delhi Delegation",
    grantedHours: 8.0,
    reviewedBy: "Principal Dr. V. Swaminathan",
    status: "Approved",
    remarks: "Represented institute at national level; secured gold medal in Bakery category.",
  },
  {
    id: "cond-102",
    rollNumber: "2441112008",
    studentName: "Isha Mukherjee",
    eventName: "State Banquet Catering Protocol",
    dutyType: "Raj Bhavan VIP Service",
    grantedHours: 6.0,
    reviewedBy: "Prof. Ananya Sen (HOD F&B)",
    status: "Pending",
    remarks: "Official deputation by F&B Service Department for visiting foreign dignitaries.",
  },
  {
    id: "cond-103",
    rollNumber: "2441112010",
    studentName: "Kritika Das",
    eventName: "Annual Cultural & Hospitality Fest",
    dutyType: "Organizing Committee",
    grantedHours: 4.0,
    reviewedBy: "Chef Rajesh Sharma (HOD Culinary)",
    status: "Pending",
    remarks: "Front desk coordination and guest hospitality protocol management.",
  },
  {
    id: "cond-104",
    rollNumber: "2441112019",
    studentName: "Manish Joshi",
    eventName: "Inter-College Sports Tournament",
    dutyType: "Cricket Team Captain",
    grantedHours: 6.0,
    reviewedBy: "Dean of Student Affairs",
    status: "Approved",
    remarks: "University level semi-final match participation sanction.",
  },
];

export default function DutyCondonationPage() {
  const [claims, setClaims] = useState<CondonationClaim[]>(initialCondonations);
  const [selectedStatus, setSelectedStatus] = useState("All Status");
  const [searchQuery, setSearchQuery] = useState("");
  const [selectedRows, setSelectedRows] = useState<string[]>([]);
  const [pageSize, setPageSize] = useState("10");

  // Form State
  const [editingId, setEditingId] = useState<string | null>(null);
  const [formData, setFormData] = useState({
    rollNumber: "",
    studentName: "",
    eventName: "",
    dutyType: "Institutional Representation",
    grantedHours: 4.0,
    reviewedBy: "Principal Dr. V. Swaminathan",
    status: "Pending" as "Approved" | "Pending" | "Rejected",
    remarks: "",
  });

  const filteredClaims = claims.filter((c) => {
    const matchesStatus =
      selectedStatus === "All Status" || c.status === selectedStatus;
    const matchesSearch =
      searchQuery === "" ||
      c.studentName.toLowerCase().includes(searchQuery.toLowerCase()) ||
      c.rollNumber.includes(searchQuery) ||
      c.eventName.toLowerCase().includes(searchQuery.toLowerCase()) ||
      c.dutyType.toLowerCase().includes(searchQuery.toLowerCase());
    return matchesStatus && matchesSearch;
  });

  const handleSelectAll = (e: React.ChangeEvent<HTMLInputElement>) => {
    if (e.target.checked) {
      setSelectedRows(filteredClaims.map((c) => c.id));
    } else {
      setSelectedRows([]);
    }
  };

  const handleRowSelect = (id: string) => {
    setSelectedRows((prev) =>
      prev.includes(id) ? prev.filter((r) => r !== id) : [...prev, id]
    );
  };

  const handleResetFilters = () => {
    setSelectedStatus("All Status");
    setSearchQuery("");
    toast.info("Filters reset to default");
  };

  const handleEdit = (c: CondonationClaim) => {
    setEditingId(c.id);
    setFormData({
      rollNumber: c.rollNumber,
      studentName: c.studentName,
      eventName: c.eventName,
      dutyType: c.dutyType,
      grantedHours: c.grantedHours,
      reviewedBy: c.reviewedBy,
      status: c.status,
      remarks: c.remarks,
    });
  };

  const handleDelete = (id: string) => {
    setClaims((prev) => prev.filter((c) => c.id !== id));
    toast.success("Duty condonation record removed");
  };

  const handleCancelForm = () => {
    setEditingId(null);
    setFormData({
      rollNumber: "",
      studentName: "",
      eventName: "",
      dutyType: "Institutional Representation",
      grantedHours: 4.0,
      reviewedBy: "Principal Dr. V. Swaminathan",
      status: "Pending",
      remarks: "",
    });
  };

  const handleSubmitForm = (e: React.FormEvent) => {
    e.preventDefault();
    if (!formData.rollNumber || !formData.studentName || !formData.eventName) {
      toast.error("Please fill in Student Roll Number, Name, and Event Title");
      return;
    }

    if (editingId) {
      setClaims((prev) =>
        prev.map((c) =>
          c.id === editingId
            ? {
                ...c,
                rollNumber: formData.rollNumber,
                studentName: formData.studentName,
                eventName: formData.eventName,
                dutyType: formData.dutyType,
                grantedHours: formData.grantedHours,
                reviewedBy: formData.reviewedBy,
                status: formData.status,
                remarks: formData.remarks,
              }
            : c
        )
      );
      toast.success("Duty condonation updated successfully");
    } else {
      const newClaim: CondonationClaim = {
        id: `cond-${Date.now()}`,
        rollNumber: formData.rollNumber,
        studentName: formData.studentName,
        eventName: formData.eventName,
        dutyType: formData.dutyType,
        grantedHours: formData.grantedHours,
        reviewedBy: formData.reviewedBy,
        status: formData.status,
        remarks: formData.remarks,
      };
      setClaims((prev) => [newClaim, ...prev]);
      toast.success("New duty condonation recorded");
    }
    handleCancelForm();
  };

  const getStatusBadge = (status: string) => {
    switch (status) {
      case "Approved":
        return "bg-[#DCFCE7] text-[#15803D] border-[#BBF7D0]";
      case "Pending":
        return "bg-[#FEF3C7] text-[#B45309] border-[#FDE68A]";
      case "Rejected":
        return "bg-[#FEE2E2] text-[#DC2626] border-[#FECACA]";
      default:
        return "bg-slate-100 text-slate-700 border-slate-200";
    }
  };

  return (
    <div className="space-y-6 pb-12 font-sans bg-[#F8FAFC] min-h-screen">
      {/* 1. Header Bar */}
      <div>
        <div className="flex items-center gap-2 text-xs text-[#64748B] mb-2 font-medium">
          <Link href="/admin/dashboard" className="hover:text-[#1769E8] transition-colors">
            Home
          </Link>
          <span>&gt;</span>
          <Link href="/admin/adjustments/medical" className="hover:text-[#1769E8] transition-colors">
            Attendance Adjustments
          </Link>
          <span>&gt;</span>
          <span className="text-[#071A4D] font-semibold">Duty Condonation</span>
        </div>

        <div className="flex flex-col md:flex-row md:items-center justify-between gap-4">
          <div>
            <h1 className="text-3xl font-bold font-heading text-[#071A4D] tracking-tight">
              Duty Condonation
            </h1>
            <p className="text-[15px] text-[#64748B] mt-1 font-normal">
              Sanction on-duty contact hours for students representing IHM in national competitions, symposiums, and VIP banquets.
            </p>
          </div>

          <div className="flex items-center gap-3">
            <button
              onClick={() => toast.success("Exporting condonation register to Excel...")}
              className="px-4 py-2 bg-white border border-[#E2E8F0] rounded-lg text-sm font-medium text-[#334155] hover:bg-slate-50 transition-colors flex items-center gap-2 shadow-xs cursor-pointer"
            >
              <FileSpreadsheet className="w-4 h-4 text-emerald-600" />
              <span>Export</span>
            </button>
            <button
              onClick={() => toast.info("Generating duty condonation report...")}
              className="px-4 py-2 bg-white border border-[#E2E8F0] rounded-lg text-sm font-medium text-[#334155] hover:bg-slate-50 transition-colors flex items-center gap-2 shadow-xs cursor-pointer"
            >
              <FileText className="w-4 h-4 text-purple-600" />
              <span>Generate Report</span>
            </button>
            <button
              onClick={() => {
                setEditingId(null);
                setFormData({
                  rollNumber: "",
                  studentName: "",
                  eventName: "",
                  dutyType: "Institutional Representation",
                  grantedHours: 4.0,
                  reviewedBy: "Principal Dr. V. Swaminathan",
                  status: "Pending",
                  remarks: "",
                });
              }}
              className="px-5 py-2.5 bg-[#1769E8] hover:bg-[#1255be] text-white rounded-lg text-sm font-semibold transition-colors flex items-center gap-2 shadow-sm cursor-pointer"
            >
              <Plus className="w-4 h-4" />
              <span>+ New Condonation</span>
            </button>
          </div>
        </div>
      </div>

      {/* 2. Filter Bar */}
      <div className="bg-white rounded-xl border border-[#E5EAF0] shadow-xs p-4">
        <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-12 gap-3.5 items-end">
          <div className="lg:col-span-3">
            <label className="block text-[11px] font-semibold text-[#64748B] uppercase tracking-wider mb-1.5">
              Sanction Status
            </label>
            <select
              value={selectedStatus}
              onChange={(e) => setSelectedStatus(e.target.value)}
              className="w-full bg-white border border-[#E2E8F0] text-sm text-[#0F172A] rounded-lg px-3 py-2 font-medium focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all cursor-pointer"
            >
              <option>All Status</option>
              <option>Pending</option>
              <option>Approved</option>
              <option>Rejected</option>
            </select>
          </div>

          <div className="lg:col-span-7">
            <label className="block text-[11px] font-semibold text-[#64748B] uppercase tracking-wider mb-1.5">
              Search
            </label>
            <div className="relative">
              <Search className="w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-[#94A3B8]" />
              <input
                type="text"
                value={searchQuery}
                onChange={(e) => setSearchQuery(e.target.value)}
                placeholder="Search by student name, roll number, competition, or duty event..."
                className="w-full pl-9 pr-3 py-2 bg-white border border-[#E2E8F0] text-sm text-[#0F172A] placeholder:text-[#94A3B8] rounded-lg focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all"
              />
            </div>
          </div>

          <div className="lg:col-span-2 flex items-center gap-2">
            <button
              onClick={() => toast.success(`Found ${filteredClaims.length} records`)}
              className="flex-1 bg-[#1769E8] hover:bg-[#1255be] text-white py-2 rounded-lg text-sm font-semibold transition-colors flex items-center justify-center gap-1.5 shadow-xs cursor-pointer"
            >
              <Search className="w-4 h-4" />
              <span>Search</span>
            </button>
            <button
              onClick={handleResetFilters}
              className="border border-[#E2E8F0] hover:bg-slate-50 text-[#64748B] p-2 rounded-lg text-sm font-medium transition-colors flex items-center justify-center cursor-pointer"
              title="Reset Filters"
            >
              <RotateCcw className="w-4 h-4" />
            </button>
          </div>
        </div>
      </div>

      {/* 3. Main Split Layout */}
      <div className="grid grid-cols-1 lg:grid-cols-12 gap-6 items-start">
        {/* Left (8 cols): Table */}
        <div className="lg:col-span-8 bg-white rounded-xl border border-[#E5EAF0] shadow-xs overflow-hidden">
          <div className="p-4 border-b border-[#E5EAF0] flex items-center justify-between">
            <div className="flex items-center gap-2.5">
              <Award className="w-5 h-5 text-[#1769E8]" />
              <h2 className="font-heading font-bold text-lg text-[#071A4D]">
                Duty Condonation Register ({filteredClaims.length})
              </h2>
            </div>
            <div className="flex items-center gap-2">
              <select
                value={pageSize}
                onChange={(e) => setPageSize(e.target.value)}
                className="text-xs bg-white border border-[#E2E8F0] rounded-lg px-2.5 py-1.5 font-medium text-[#475569] outline-none cursor-pointer"
              >
                <option value="10">10 per page</option>
                <option value="25">25 per page</option>
              </select>
            </div>
          </div>

          <div className="overflow-x-auto">
            <table className="w-full text-left text-xs border-collapse">
              <thead>
                <tr className="bg-[#F8FAFC] border-b border-[#E5EAF0] text-[#64748B] font-semibold">
                  <th className="py-3 px-3.5 w-10 text-center">
                    <input
                      type="checkbox"
                      checked={
                        selectedRows.length === filteredClaims.length &&
                        filteredClaims.length > 0
                      }
                      onChange={handleSelectAll}
                      className="rounded border-[#CBD5E1] text-[#1769E8] focus:ring-0 cursor-pointer"
                    />
                  </th>
                  <th className="py-3 px-2 w-8 text-center text-[#64748B]">#</th>
                  <th className="py-3 px-3.5 font-bold text-[#071A4D]">Roll No</th>
                  <th className="py-3 px-3.5 font-bold text-[#071A4D]">Student Name</th>
                  <th className="py-3 px-3.5 font-bold text-[#071A4D]">Event &amp; Duty Type</th>
                  <th className="py-3 px-3.5 font-bold text-[#071A4D] text-center">Hours</th>
                  <th className="py-3 px-3.5 font-bold text-[#071A4D]">Sanctioning Authority</th>
                  <th className="py-3 px-3.5 font-bold text-[#071A4D] text-center">Status</th>
                  <th className="py-3 px-4 font-bold text-[#071A4D] text-right">Actions</th>
                </tr>
              </thead>
              <tbody className="divide-y divide-[#E5EAF0]">
                {filteredClaims.length === 0 ? (
                  <tr>
                    <td colSpan={9} className="py-12 text-center text-slate-400">
                      No duty condonations match your criteria.
                    </td>
                  </tr>
                ) : (
                  filteredClaims.map((c, idx) => (
                    <tr
                      key={c.id}
                      className={`hover:bg-[#F8FAFC] transition-colors ${
                        selectedRows.includes(c.id) ? "bg-[#EFF6FF]/40" : ""
                      }`}
                    >
                      <td className="py-3.5 px-3.5 text-center">
                        <input
                          type="checkbox"
                          checked={selectedRows.includes(c.id)}
                          onChange={() => handleRowSelect(c.id)}
                          className="rounded border-[#CBD5E1] text-[#1769E8] focus:ring-0 cursor-pointer"
                        />
                      </td>
                      <td className="py-3.5 px-2 text-center text-[#94A3B8] font-medium">
                        {idx + 1}
                      </td>
                      <td className="py-3.5 px-3.5 font-mono font-bold text-xs">
                        <span className="bg-[#F1F6FC] text-[#1769E8] px-2 py-0.5 rounded border border-[#D0E2FF]">
                          {c.rollNumber}
                        </span>
                      </td>
                      <td className="py-3.5 px-3.5">
                        <div className="font-bold text-[#071A4D] text-sm">
                          {c.studentName}
                        </div>
                      </td>
                      <td className="py-3.5 px-3.5">
                        <div className="font-semibold text-[#334155]">
                          {c.eventName}
                        </div>
                        <div className="text-[11px] text-[#64748B]">
                          {c.dutyType}
                        </div>
                      </td>
                      <td className="py-3.5 px-3.5 text-center font-mono font-bold text-[#071A4D]">
                        +{c.grantedHours.toFixed(1)}h
                      </td>
                      <td className="py-3.5 px-3.5 text-[#475569] text-xs">
                        {c.reviewedBy}
                      </td>
                      <td className="py-3.5 px-3.5 text-center">
                        <span
                          className={`inline-flex items-center px-2.5 py-0.5 rounded-full text-[11px] font-bold border ${getStatusBadge(
                            c.status
                          )}`}
                        >
                          {c.status}
                        </span>
                      </td>
                      <td className="py-3.5 px-4 text-right">
                        <div className="flex items-center justify-end gap-1.5">
                          <button
                            onClick={() =>
                              toast.info(`Condonation note: ${c.remarks}`)
                            }
                            className="p-1.5 text-[#64748B] hover:text-[#1769E8] hover:bg-[#EFF6FF] rounded-md transition-colors cursor-pointer"
                            title="View Details"
                          >
                            <Eye className="w-3.5 h-3.5" />
                          </button>
                          <button
                            onClick={() => handleEdit(c)}
                            className="p-1.5 text-[#64748B] hover:text-[#1769E8] hover:bg-[#EFF6FF] rounded-md transition-colors cursor-pointer"
                            title="Edit Record"
                          >
                            <Edit2 className="w-3.5 h-3.5" />
                          </button>
                          <button
                            onClick={() => handleDelete(c.id)}
                            className="p-1.5 text-[#64748B] hover:text-rose-600 hover:bg-rose-50 rounded-md transition-colors cursor-pointer"
                            title="Delete Record"
                          >
                            <Trash2 className="w-3.5 h-3.5" />
                          </button>
                        </div>
                      </td>
                    </tr>
                  ))
                )}
              </tbody>
            </table>
          </div>

          <div className="p-4 border-t border-[#E5EAF0] flex flex-col sm:flex-row items-center justify-between gap-3 text-xs text-[#64748B]">
            <div>
              Showing 1 to {filteredClaims.length} of {claims.length} records
            </div>
            <div className="flex items-center gap-1">
              <button className="p-1.5 border border-[#E2E8F0] rounded-md hover:bg-slate-50 transition-colors cursor-pointer">
                <ChevronLeft className="w-3.5 h-3.5" />
              </button>
              <button className="px-3 py-1 bg-[#1769E8] text-white font-bold rounded-md text-xs">
                1
              </button>
              <button className="p-1.5 border border-[#E2E8F0] rounded-md hover:bg-slate-50 transition-colors cursor-pointer">
                <ChevronRight className="w-3.5 h-3.5" />
              </button>
            </div>
          </div>
        </div>

        {/* Right (4 cols): Add/Edit Form + Legend */}
        <div className="lg:col-span-4 space-y-5">
          <div className="bg-white rounded-xl border border-[#E5EAF0] shadow-xs p-5">
            <div className="flex items-center justify-between pb-3 mb-4 border-b border-[#E5EAF0]">
              <div className="flex items-center gap-2">
                <Award className="w-4 h-4 text-[#1769E8]" />
                <h3 className="font-heading font-bold text-base text-[#071A4D]">
                  {editingId ? "Edit Condonation" : "Sanction Duty Hours"}
                </h3>
              </div>
              {editingId && (
                <span className="text-[10px] font-bold px-2 py-0.5 bg-amber-50 text-amber-700 border border-amber-200 rounded-full">
                  Editing
                </span>
              )}
            </div>

            <form onSubmit={handleSubmitForm} className="space-y-4 text-xs">
              <div className="grid grid-cols-2 gap-3">
                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    NCHM Roll No <span className="text-rose-500">*</span>
                  </label>
                  <input
                    type="text"
                    required
                    value={formData.rollNumber}
                    onChange={(e) => setFormData({ ...formData, rollNumber: e.target.value })}
                    placeholder="e.g. 2441112007"
                    className="w-full px-3 py-2 border border-[#E2E8F0] rounded-lg text-sm text-[#0F172A] placeholder:text-[#94A3B8] focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all font-mono"
                  />
                </div>

                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Granted Hours <span className="text-rose-500">*</span>
                  </label>
                  <input
                    type="number"
                    step="0.5"
                    min="1"
                    max="50"
                    value={formData.grantedHours}
                    onChange={(e) =>
                      setFormData({
                        ...formData,
                        grantedHours: parseFloat(e.target.value) || 0,
                      })
                    }
                    className="w-full px-3 py-2 border border-[#E2E8F0] rounded-lg text-sm text-[#0F172A] focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all font-mono"
                  />
                </div>
              </div>

              <div>
                <label className="block font-semibold text-[#334155] mb-1">
                  Student Full Name <span className="text-rose-500">*</span>
                </label>
                <input
                  type="text"
                  required
                  value={formData.studentName}
                  onChange={(e) => setFormData({ ...formData, studentName: e.target.value })}
                  placeholder="e.g. Harshavardhan Rao"
                  className="w-full px-3 py-2 border border-[#E2E8F0] rounded-lg text-sm text-[#0F172A] placeholder:text-[#94A3B8] focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all"
                />
              </div>

              <div>
                <label className="block font-semibold text-[#334155] mb-1">
                  Event / Competition Title <span className="text-rose-500">*</span>
                </label>
                <input
                  type="text"
                  required
                  value={formData.eventName}
                  onChange={(e) => setFormData({ ...formData, eventName: e.target.value })}
                  placeholder="e.g. National Culinary Challenge 2026"
                  className="w-full px-3 py-2 border border-[#E2E8F0] rounded-lg text-sm text-[#0F172A] placeholder:text-[#94A3B8] focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all"
                />
              </div>

              <div className="grid grid-cols-2 gap-3">
                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Deputation Role
                  </label>
                  <input
                    type="text"
                    value={formData.dutyType}
                    onChange={(e) => setFormData({ ...formData, dutyType: e.target.value })}
                    placeholder="e.g. Chef Delegation"
                    className="w-full px-3 py-2 border border-[#E2E8F0] rounded-lg text-sm text-[#0F172A] placeholder:text-[#94A3B8] focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all"
                  />
                </div>

                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Sanctioning Officer
                  </label>
                  <select
                    value={formData.reviewedBy}
                    onChange={(e) => setFormData({ ...formData, reviewedBy: e.target.value })}
                    className="w-full px-3 py-2 border border-[#E2E8F0] rounded-lg text-sm text-[#0F172A] focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all bg-white cursor-pointer"
                  >
                    <option>Principal Dr. V. Swaminathan</option>
                    <option>HOD Culinary (Chef Rajesh)</option>
                    <option>HOD F&amp;B Service (Prof. Ananya)</option>
                    <option>Dean of Student Affairs</option>
                  </select>
                </div>
              </div>

              <div>
                <label className="block font-semibold text-[#334155] mb-1">
                  Event Justification / Notes
                </label>
                <textarea
                  rows={2}
                  value={formData.remarks}
                  onChange={(e) => setFormData({ ...formData, remarks: e.target.value })}
                  placeholder="Official institutional event citation..."
                  className="w-full px-3 py-2 border border-[#E2E8F0] rounded-lg text-sm text-[#0F172A] placeholder:text-[#94A3B8] focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all resize-none"
                />
              </div>

              <div>
                <label className="block font-semibold text-[#334155] mb-1">
                  Sanction Decision
                </label>
                <select
                  value={formData.status}
                  onChange={(e) =>
                    setFormData({
                      ...formData,
                      status: e.target.value as "Approved" | "Pending" | "Rejected",
                    })
                  }
                  className="w-full px-3 py-2 border border-[#E2E8F0] rounded-lg text-sm text-[#0F172A] focus:ring-2 focus:ring-[#1769E8]/20 focus:border-[#1769E8] outline-none transition-all bg-white cursor-pointer"
                >
                  <option value="Pending">Pending Verification</option>
                  <option value="Approved">Approved (Grant Credit Hours)</option>
                  <option value="Rejected">Rejected</option>
                </select>
              </div>

              <div className="flex items-center gap-2 pt-3 border-t border-[#E5EAF0]">
                <button
                  type="button"
                  onClick={handleCancelForm}
                  className="flex-1 py-2 px-3 border border-[#E2E8F0] text-[#64748B] hover:bg-slate-50 rounded-lg text-xs font-semibold transition-colors cursor-pointer"
                >
                  Cancel
                </button>
                <button
                  type="submit"
                  className="flex-1 py-2 px-3 bg-[#1769E8] hover:bg-[#1255be] text-white rounded-lg text-xs font-semibold transition-colors flex items-center justify-center gap-1.5 shadow-xs cursor-pointer"
                >
                  <Award className="w-3.5 h-3.5" />
                  <span>{editingId ? "Update" : "Save Condonation"}</span>
                </button>
              </div>
            </form>
          </div>

          {/* Institutional On-Duty Norms Card */}
          <div className="bg-white rounded-xl border border-[#E5EAF0] shadow-xs p-4 text-xs">
            <div className="flex items-center gap-2 font-bold text-[#071A4D] mb-2.5 pb-2 border-b border-[#E5EAF0]">
              <Sparkles className="w-4 h-4 text-amber-500" />
              <span>NCHMCT On-Duty Condonation Rules</span>
            </div>
            <div className="space-y-2 text-[#475569]">
              <div className="flex items-center justify-between">
                <span className="font-medium">Institutional Cap</span>
                <span className="font-mono font-bold text-[#071A4D]">Max 5.0% of Term</span>
              </div>
              <div className="flex items-center justify-between">
                <span className="font-medium">Principal Approval</span>
                <span className="font-mono font-bold text-[#1769E8]">Mandatory Sign-off</span>
              </div>
              <div className="flex items-center justify-between">
                <span className="font-medium">Cumulative Shortage Relief</span>
                <span className="font-mono font-bold text-[#071A4D]">75% Threshold Relief</span>
              </div>
              <div className="flex items-center justify-between">
                <span className="font-medium">Direct APR Credit</span>
                <span className="font-mono font-bold text-emerald-600">Calculated</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}
