"use client";

import React, { useState } from "react";
import Link from "next/link";
import {
  Search,
  RotateCcw,
  Plus,
  Eye,
  Edit2,
  Trash2,
  ChevronRight,
  FileSpreadsheet,
  FileText,
  Building2,
  Sparkles,
  MapPin,
  Users,
  X,
  Save,
} from "lucide-react";
import { toast } from "sonner";

interface ExtendedRoom {
  id: string;
  name: string;
  code: string;
  capacity: number;
  roomType: "Kitchen" | "Lab" | "Restaurant" | "Classroom";
  floor: string;
  hasSmartDisplay: boolean;
  status: "Active" | "Inactive";
}

const initialRooms: ExtendedRoom[] = [
  {
    id: "rm-btk-b",
    name: "Basic Training Kitchen B",
    code: "BTK-B",
    capacity: 30,
    roomType: "Kitchen",
    floor: "Ground Floor, West Wing",
    hasSmartDisplay: true,
    status: "Active",
  },
  {
    id: "rm-qtk",
    name: "Quantity Training Kitchen",
    code: "QTK-1",
    capacity: 35,
    roomType: "Kitchen",
    floor: "Ground Floor, East Wing",
    hasSmartDisplay: false,
    status: "Active",
  },
  {
    id: "rm-atk",
    name: "Advanced Training Kitchen",
    code: "ATK-1",
    capacity: 30,
    roomType: "Kitchen",
    floor: "1st Floor, Culinary Block",
    hasSmartDisplay: true,
    status: "Active",
  },
  {
    id: "rm-bakery",
    name: "Bakery & Confectionery Lab",
    code: "BAK-1",
    capacity: 30,
    roomType: "Lab",
    floor: "1st Floor, Culinary Block",
    hasSmartDisplay: false,
    status: "Active",
  },
  {
    id: "rm-atr",
    name: "Advanced Training Restaurant",
    code: "ATR-201",
    capacity: 40,
    roomType: "Restaurant",
    floor: "2nd Floor, Hospitality Block",
    hasSmartDisplay: true,
    status: "Active",
  },
  {
    id: "rm-fo",
    name: "Front Office Operations Lab",
    code: "FO-LAB",
    capacity: 35,
    roomType: "Lab",
    floor: "1st Floor, Admin Wing",
    hasSmartDisplay: true,
    status: "Active",
  },
  {
    id: "rm-lh1",
    name: "Auditorium Lecture Hall 1",
    code: "LH-101",
    capacity: 80,
    roomType: "Classroom",
    floor: "Ground Floor, Academic Block",
    hasSmartDisplay: true,
    status: "Active",
  },
  {
    id: "rm-lh2",
    name: "Smart Lecture Hall 204",
    code: "LH-204",
    capacity: 60,
    roomType: "Classroom",
    floor: "2nd Floor, Academic Block",
    hasSmartDisplay: true,
    status: "Inactive",
  },
];

export default function RoomsManagementPage() {
  const [rooms, setRooms] = useState<ExtendedRoom[]>(initialRooms);
  const [selectedType, setSelectedType] = useState("All Facility Types");
  const [selectedFloor, setSelectedFloor] = useState("All Locations");
  const [selectedStatus, setSelectedStatus] = useState("All Status");
  const [searchQuery, setSearchQuery] = useState("");
  const [selectedRows, setSelectedRows] = useState<string[]>([]);
  const [pageSize, setPageSize] = useState("10");

  // Modal State
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [modalMode, setModalMode] = useState<"create" | "edit" | "view">("create");
  const [editingId, setEditingId] = useState<string | null>(null);
  const [viewingRoom, setViewingRoom] = useState<ExtendedRoom | null>(null);

  // Form State
  const [formData, setFormData] = useState({
    name: "",
    code: "",
    capacity: 30,
    roomType: "Kitchen" as "Kitchen" | "Lab" | "Restaurant" | "Classroom",
    floor: "Ground Floor, West Wing",
    hasSmartDisplay: true,
    status: "Active" as "Active" | "Inactive",
  });

  const filteredRooms = rooms.filter((r) => {
    const matchesType =
      selectedType === "All Facility Types" || r.roomType === selectedType;
    const matchesFloor =
      selectedFloor === "All Locations" || r.floor.includes(selectedFloor);
    const matchesStatus =
      selectedStatus === "All Status" || r.status === selectedStatus;
    const matchesSearch =
      r.name.toLowerCase().includes(searchQuery.toLowerCase()) ||
      r.code.toLowerCase().includes(searchQuery.toLowerCase()) ||
      r.floor.toLowerCase().includes(searchQuery.toLowerCase());
    return matchesType && matchesFloor && matchesStatus && matchesSearch;
  });

  const handleResetFilters = () => {
    setSelectedType("All Facility Types");
    setSelectedFloor("All Locations");
    setSelectedStatus("All Status");
    setSearchQuery("");
  };

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

  const handleSelectRow = (id: string) => {
    if (selectedRows.includes(id)) {
      setSelectedRows(selectedRows.filter((rId) => rId !== id));
    } else {
      setSelectedRows([...selectedRows, id]);
    }
  };

  const handleDelete = (id: string) => {
    const target = rooms.find((r) => r.id === id);
    if (target?.status === "Active") {
      toast.error("Cannot delete active facility without reallocating scheduled classes.");
      return;
    }
    setRooms(rooms.filter((r) => r.id !== id));
    toast.success("Facility room deleted successfully.");
  };

  const openCreateModal = () => {
    setModalMode("create");
    setEditingId(null);
    setViewingRoom(null);
    setFormData({
      name: "",
      code: "",
      capacity: 30,
      roomType: "Kitchen",
      floor: "Ground Floor, West Wing",
      hasSmartDisplay: true,
      status: "Active",
    });
    setIsModalOpen(true);
  };

  const openEditModal = (r: ExtendedRoom) => {
    setModalMode("edit");
    setEditingId(r.id);
    setViewingRoom(null);
    setFormData({
      name: r.name,
      code: r.code,
      capacity: r.capacity,
      roomType: r.roomType,
      floor: r.floor,
      hasSmartDisplay: r.hasSmartDisplay,
      status: r.status,
    });
    setIsModalOpen(true);
  };

  const openViewModal = (r: ExtendedRoom) => {
    setViewingRoom(r);
    setModalMode("view");
    setIsModalOpen(true);
  };

  const handleSubmitForm = (e: React.FormEvent) => {
    e.preventDefault();

    if (!formData.name.trim() || !formData.code.trim()) {
      toast.error("Please fill in room code and name.");
      return;
    }

    if (editingId) {
      setRooms((prev) =>
        prev.map((r) =>
          r.id === editingId
            ? {
                ...r,
                name: formData.name,
                code: formData.code.toUpperCase(),
                capacity: formData.capacity,
                roomType: formData.roomType,
                floor: formData.floor,
                hasSmartDisplay: formData.hasSmartDisplay,
                status: formData.status,
              }
            : r
        )
      );
      toast.success("Room facility updated successfully.");
    } else {
      const newRoom: ExtendedRoom = {
        id: `rm-${Date.now()}`,
        name: formData.name,
        code: formData.code.toUpperCase(),
        capacity: formData.capacity,
        roomType: formData.roomType,
        floor: formData.floor,
        hasSmartDisplay: formData.hasSmartDisplay,
        status: formData.status,
      };
      setRooms((prev) => [newRoom, ...prev]);
      toast.success("New room facility added.");
    }

    setIsModalOpen(false);
  };

  return (
    <div className="space-y-5 font-sans">
      {/* 1. Header & Breadcrumbs */}
      <div className="flex flex-col lg:flex-row lg:items-center justify-between gap-4">
        <div>
          <nav className="flex items-center gap-1.5 text-[13px] text-[#64748B] mb-1 font-medium">
            <Link href="/admin/dashboard" className="hover:text-[#1769E8] hover:underline">
              Home
            </Link>
            <ChevronRight className="w-3.5 h-3.5 text-[#94A3B8]" />
            <span>Academic Management</span>
            <ChevronRight className="w-3.5 h-3.5 text-[#94A3B8]" />
            <span className="text-[#071A4D] font-semibold">Rooms & Facilities</span>
          </nav>

          <h1 className="text-3xl font-bold font-heading text-[#071A4D] tracking-tight">
            Rooms & Facilities
          </h1>
          <p className="text-[15px] text-[#64748B] mt-1 font-normal">
            Configure kitchens, laboratories, training restaurants, and lecture halls.
          </p>
        </div>

        <div className="flex items-center gap-2.5 flex-wrap">
          <button
            onClick={() => toast.success("Exporting Room Directory...")}
            className="inline-flex items-center gap-1.5 px-3.5 py-2 text-xs font-semibold text-[#071A4D] bg-white border border-[#E5EAF0] rounded-lg hover:bg-slate-50 transition-colors shadow-2xs cursor-pointer"
          >
            <FileSpreadsheet className="w-4 h-4 text-emerald-600" />
            <span>Export</span>
          </button>

          <button
            onClick={openCreateModal}
            className="inline-flex items-center gap-1.5 px-4 py-2 text-xs font-semibold text-white bg-[#1769E8] hover:bg-[#1257C7] rounded-lg shadow-sm transition-colors cursor-pointer"
          >
            <Plus className="w-4 h-4" />
            <span>+ Add Room Facility</span>
          </button>
        </div>
      </div>

      {/* 2. Filter Bar */}
      <div className="p-4 rounded-xl bg-white border border-[#E5EAF0] shadow-xs flex flex-col lg:flex-row items-stretch lg:items-center justify-between gap-3">
        <div className="flex flex-wrap items-center gap-2.5 flex-1">
          <div className="flex flex-col">
            <label className="text-[10px] font-bold text-[#64748B] mb-0.5 uppercase tracking-wider">
              Facility Type
            </label>
            <select
              value={selectedType}
              onChange={(e) => setSelectedType(e.target.value)}
              className="h-9 px-3 rounded-lg border border-[#D8E0EA] bg-white text-xs font-medium text-[#334155] focus:outline-none focus:border-[#1769E8] cursor-pointer"
            >
              <option>All Facility Types</option>
              <option value="Kitchen">Kitchens</option>
              <option value="Lab">Labs</option>
              <option value="Restaurant">Restaurants</option>
              <option value="Classroom">Classrooms</option>
            </select>
          </div>

          <div className="flex flex-col">
            <label className="text-[10px] font-bold text-[#64748B] mb-0.5 uppercase tracking-wider">
              Status
            </label>
            <select
              value={selectedStatus}
              onChange={(e) => setSelectedStatus(e.target.value)}
              className="h-9 px-3 rounded-lg border border-[#D8E0EA] bg-white text-xs font-medium text-[#334155] focus:outline-none focus:border-[#1769E8] cursor-pointer"
            >
              <option>All Status</option>
              <option value="Active">Active</option>
              <option value="Inactive">Inactive</option>
            </select>
          </div>

          <div className="flex flex-col flex-1 min-w-[220px]">
            <label className="text-[10px] font-bold text-[#64748B] mb-0.5 uppercase tracking-wider opacity-0">
              Search
            </label>
            <div className="relative">
              <Search className="w-4 h-4 text-[#94A3B8] absolute left-3 top-1/2 -translate-y-1/2" />
              <input
                type="text"
                value={searchQuery}
                onChange={(e) => setSearchQuery(e.target.value)}
                placeholder="Search by facility name, code, or floor location..."
                className="h-9 w-full rounded-lg border border-[#D8E0EA] bg-white pl-9 pr-3 text-xs text-[#334155] placeholder:text-[#94A3B8] focus:outline-none focus:border-[#1769E8]"
              />
            </div>
          </div>
        </div>

        <div className="flex items-end gap-2 shrink-0 pt-3 lg:pt-0">
          <button
            onClick={() => {}}
            className="h-9 px-4 rounded-lg bg-[#1769E8] hover:bg-[#1257C7] text-white text-xs font-semibold flex items-center gap-1.5 shadow-xs transition-colors cursor-pointer"
          >
            <Search className="w-3.5 h-3.5" />
            <span>Search</span>
          </button>

          <button
            onClick={handleResetFilters}
            className="h-9 px-4 rounded-lg border border-[#E5EAF0] hover:bg-slate-50 text-[#334155] text-xs font-semibold flex items-center gap-1.5 transition-colors shadow-2xs cursor-pointer"
          >
            <RotateCcw className="w-3.5 h-3.5 text-[#64748B]" />
            <span>Reset</span>
          </button>
        </div>
      </div>

      {/* 3. Full Width Table - All Details in a Line */}
      <div className="rounded-xl bg-white 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">
            <Building2 className="w-4 h-4 text-[#1769E8]" />
            <h2 className="text-sm font-bold font-heading text-[#071A4D]">
              Room Facilities ({filteredRooms.length})
            </h2>
          </div>

          <select
            value={pageSize}
            onChange={(e) => setPageSize(e.target.value)}
            className="h-8 px-2.5 rounded-lg border border-[#D8E0EA] bg-white text-xs text-[#64748B] focus:outline-none focus:border-[#1769E8] cursor-pointer"
          >
            <option value="10">10 per page</option>
            <option value="25">25 per page</option>
            <option value="50">50 per page</option>
          </select>
        </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] uppercase tracking-wider font-bold text-[11px] whitespace-nowrap">
                <th className="py-3 px-3.5 text-center w-10">
                  <input
                    type="checkbox"
                    checked={selectedRows.length === filteredRooms.length && filteredRooms.length > 0}
                    onChange={handleSelectAll}
                    className="rounded border-[#D8E0EA] text-[#1769E8] focus:ring-[#1769E8]"
                  />
                </th>
                <th className="py-3 px-2 text-center w-10">#</th>
                <th className="py-3 px-4">Room Code</th>
                <th className="py-3 px-4">Facility Name</th>
                <th className="py-3 px-4">Type</th>
                <th className="py-3 px-4">Location / Floor</th>
                <th className="py-3 px-4 text-center">Capacity</th>
                <th className="py-3 px-4 text-center">Smart Display</th>
                <th className="py-3 px-4 text-center">Status</th>
                <th className="py-3 px-4 text-center">Actions</th>
              </tr>
            </thead>
            <tbody className="divide-y divide-[#E5EAF0] text-[#334155]">
              {filteredRooms.length === 0 ? (
                <tr>
                  <td colSpan={10} className="py-12 text-center text-slate-400">
                    No facility rooms match the search criteria.
                  </td>
                </tr>
              ) : (
                filteredRooms.map((r, idx) => {
                  const isSelected = selectedRows.includes(r.id);
                  return (
                    <tr
                      key={r.id}
                      className={`hover:bg-[#F8FAFC] transition-colors whitespace-nowrap ${
                        isSelected ? "bg-blue-50/40" : ""
                      }`}
                    >
                      <td className="py-3.5 px-3.5 text-center">
                        <input
                          type="checkbox"
                          checked={isSelected}
                          onChange={() => handleSelectRow(r.id)}
                          className="rounded border-[#D8E0EA] text-[#1769E8] focus:ring-[#1769E8]"
                        />
                      </td>
                      <td className="py-3.5 px-2 text-center text-slate-400 font-medium">
                        {idx + 1}
                      </td>
                      <td className="py-3.5 px-4 font-bold text-[#071A4D]">
                        <span className="font-mono bg-slate-100 px-2 py-0.5 rounded text-[11px] text-[#071A4D]">
                          {r.code}
                        </span>
                      </td>
                      <td className="py-3.5 px-4 font-semibold text-[#071A4D]">
                        {r.name}
                      </td>
                      <td className="py-3.5 px-4">
                        <span className="inline-flex items-center px-2 py-0.5 rounded-md text-[11px] font-medium bg-blue-50 text-[#1769E8]">
                          {r.roomType}
                        </span>
                      </td>
                      <td className="py-3.5 px-4 text-[#475569]">
                        {r.floor}
                      </td>
                      <td className="py-3.5 px-4 text-center font-bold text-[#071A4D]">
                        {r.capacity} Stations
                      </td>
                      <td className="py-3.5 px-4 text-center">
                        {r.hasSmartDisplay ? (
                          <span className="inline-flex items-center px-2 py-0.5 rounded-full text-[10px] font-medium bg-emerald-50 text-emerald-700">
                            Available
                          </span>
                        ) : (
                          <span className="text-[#94A3B8]">None</span>
                        )}
                      </td>
                      <td className="py-3.5 px-4 text-center">
                        {r.status === "Active" ? (
                          <span className="inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-[11px] font-semibold bg-[#DCFCE7] text-[#15803D]">
                            <span className="w-1.5 h-1.5 rounded-full bg-[#15803D]"></span>
                            Active
                          </span>
                        ) : (
                          <span className="inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-[11px] font-semibold bg-[#E2E8F0] text-[#475569]">
                            <span className="w-1.5 h-1.5 rounded-full bg-[#475569]"></span>
                            Inactive
                          </span>
                        )}
                      </td>
                      <td className="py-3.5 px-4 text-center">
                        <div className="flex items-center justify-center gap-1.5">
                          <button
                            onClick={() => openViewModal(r)}
                            className="p-1.5 rounded-lg text-[#1769E8] hover:bg-blue-50 transition-colors"
                            title="View facility details"
                          >
                            <Eye className="w-4 h-4" />
                          </button>

                          <button
                            onClick={() => openEditModal(r)}
                            className="p-1.5 rounded-lg text-[#1769E8] hover:bg-blue-50 transition-colors"
                            title="Edit room facility"
                          >
                            <Edit2 className="w-4 h-4" />
                          </button>

                          <button
                            onClick={() => handleDelete(r.id)}
                            className="p-1.5 rounded-lg text-[#DC2626] hover:bg-red-50 transition-colors"
                            title="Delete facility"
                          >
                            <Trash2 className="w-4 h-4" />
                          </button>
                        </div>
                      </td>
                    </tr>
                  );
                })
              )}
            </tbody>
          </table>
        </div>

        {/* Pagination Footer */}
        <div className="p-3.5 px-4 border-t border-[#E5EAF0] flex items-center justify-between text-xs text-[#64748B]">
          <div>
            Showing 1 to {filteredRooms.length} of {rooms.length} facilities
          </div>
          <div className="flex items-center gap-1">
            <button
              disabled
              className="px-2.5 py-1 rounded border border-[#E5EAF0] text-slate-300 cursor-not-allowed"
            >
              &lt;
            </button>
            <button className="px-3 py-1 rounded bg-[#1769E8] text-white font-bold">
              1
            </button>
            <button
              disabled
              className="px-2.5 py-1 rounded border border-[#E5EAF0] text-slate-300 cursor-not-allowed"
            >
              &gt;
            </button>
          </div>
        </div>
      </div>

      {/* 4. POPUP MODAL: CREATE / EDIT / VIEW ROOM */}
      {isModalOpen && (
        <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50 backdrop-blur-xs p-4 animate-in fade-in duration-150">
          <div className="w-full max-w-lg bg-white rounded-2xl shadow-2xl border border-[#E5EAF0] overflow-hidden flex flex-col max-h-[90vh]">
            <div className="px-6 py-4 border-b border-[#E5EAF0] flex items-center justify-between bg-slate-50/60">
              <div className="flex items-center gap-2.5">
                <div className="w-9 h-9 rounded-xl bg-blue-50 text-[#1769E8] flex items-center justify-center">
                  <Building2 className="w-5 h-5" />
                </div>
                <div>
                  <h3 className="font-heading font-bold text-base text-[#071A4D]">
                    {modalMode === "create" && "Add Room Facility"}
                    {modalMode === "edit" && "Edit Room Facility"}
                    {modalMode === "view" && "Facility Room Overview"}
                  </h3>
                  <p className="text-[12px] text-[#64748B]">
                    {modalMode === "create" && "Configure laboratory, kitchen, or classroom space."}
                    {modalMode === "edit" && "Update facility capacity and equipment specs."}
                    {modalMode === "view" && "Complete details of this campus facility."}
                  </p>
                </div>
              </div>
              <button
                onClick={() => setIsModalOpen(false)}
                className="w-8 h-8 rounded-lg text-slate-400 hover:text-slate-700 hover:bg-slate-100 flex items-center justify-center transition-colors cursor-pointer"
              >
                <X className="w-4 h-4" />
              </button>
            </div>

            <div className="p-6 overflow-y-auto space-y-4">
              {modalMode === "view" && viewingRoom ? (
                <div className="space-y-4 text-xs">
                  <div className="grid grid-cols-2 gap-4 p-4 rounded-xl bg-slate-50 border border-slate-100">
                    <div>
                      <span className="text-[10px] uppercase font-bold text-[#64748B] block">Room Code</span>
                      <span className="text-sm font-bold font-mono text-[#071A4D]">{viewingRoom.code}</span>
                    </div>
                    <div>
                      <span className="text-[10px] uppercase font-bold text-[#64748B] block">Facility Type</span>
                      <span className="text-sm font-semibold text-[#071A4D]">{viewingRoom.roomType}</span>
                    </div>
                    <div className="col-span-2">
                      <span className="text-[10px] uppercase font-bold text-[#64748B] block">Room Name</span>
                      <span className="text-sm font-bold text-[#071A4D]">{viewingRoom.name}</span>
                    </div>
                    <div className="col-span-2">
                      <span className="text-[10px] uppercase font-bold text-[#64748B] block">Floor Location</span>
                      <span className="text-xs font-medium text-[#334155]">{viewingRoom.floor}</span>
                    </div>
                    <div>
                      <span className="text-[10px] uppercase font-bold text-[#64748B] block">Capacity</span>
                      <span className="text-xs font-bold text-[#071A4D]">{viewingRoom.capacity} Stations</span>
                    </div>
                    <div>
                      <span className="text-[10px] uppercase font-bold text-[#64748B] block">Smart Display</span>
                      <span className="text-xs font-medium text-[#334155]">{viewingRoom.hasSmartDisplay ? "Equipped" : "None"}</span>
                    </div>
                    <div>
                      <span className="text-[10px] uppercase font-bold text-[#64748B] block">Status</span>
                      <span className={`inline-block mt-0.5 px-2 py-0.5 rounded-full font-semibold text-[10px] ${
                        viewingRoom.status === "Active"
                          ? "bg-emerald-100 text-emerald-700"
                          : "bg-slate-200 text-slate-700"
                      }`}>
                        {viewingRoom.status}
                      </span>
                    </div>
                  </div>
                </div>
              ) : (
                <form id="room-form" onSubmit={handleSubmitForm} className="space-y-4">
                  <div className="grid grid-cols-2 gap-3">
                    <div>
                      <label className="block text-xs font-bold text-[#334155] mb-1">
                        Room Code <span className="text-[#DC2626]">*</span>
                      </label>
                      <input
                        type="text"
                        required
                        value={formData.code}
                        onChange={(e) => setFormData({ ...formData, code: e.target.value })}
                        placeholder="e.g. BTK-B"
                        className="h-10 w-full px-3 text-xs uppercase font-mono rounded-lg border border-[#D8E0EA] bg-white text-[#334155] placeholder:text-[#94A3B8] focus:outline-none focus:border-[#1769E8] focus:ring-1 focus:ring-[#1769E8]/20"
                      />
                    </div>
                    <div>
                      <label className="block text-xs font-bold text-[#334155] mb-1">
                        Facility Type <span className="text-[#DC2626]">*</span>
                      </label>
                      <select
                        value={formData.roomType}
                        onChange={(e) =>
                          setFormData({
                            ...formData,
                            roomType: e.target.value as ExtendedRoom["roomType"],
                          })
                        }
                        className="h-10 w-full px-3 text-xs rounded-lg border border-[#D8E0EA] bg-white text-[#334155] focus:outline-none focus:border-[#1769E8] focus:ring-1 focus:ring-[#1769E8]/20 cursor-pointer"
                      >
                        <option value="Kitchen">Kitchen</option>
                        <option value="Lab">Lab</option>
                        <option value="Restaurant">Restaurant</option>
                        <option value="Classroom">Classroom</option>
                      </select>
                    </div>
                  </div>

                  <div>
                    <label className="block text-xs font-bold text-[#334155] mb-1">
                      Facility Name <span className="text-[#DC2626]">*</span>
                    </label>
                    <input
                      type="text"
                      required
                      value={formData.name}
                      onChange={(e) => setFormData({ ...formData, name: e.target.value })}
                      placeholder="e.g. Basic Training Kitchen B"
                      className="h-10 w-full px-3 text-xs rounded-lg border border-[#D8E0EA] bg-white text-[#334155] placeholder:text-[#94A3B8] focus:outline-none focus:border-[#1769E8] focus:ring-1 focus:ring-[#1769E8]/20"
                    />
                  </div>

                  <div className="grid grid-cols-2 gap-3">
                    <div>
                      <label className="block text-xs font-bold text-[#334155] mb-1">
                        Station Capacity
                      </label>
                      <input
                        type="number"
                        min="5"
                        max="200"
                        value={formData.capacity}
                        onChange={(e) => setFormData({ ...formData, capacity: Number(e.target.value) })}
                        className="h-10 w-full px-3 text-xs rounded-lg border border-[#D8E0EA] bg-white text-[#334155] focus:outline-none focus:border-[#1769E8] focus:ring-1 focus:ring-[#1769E8]/20"
                      />
                    </div>
                    <div>
                      <label className="block text-xs font-bold text-[#334155] mb-1">
                        Smart Display
                      </label>
                      <select
                        value={formData.hasSmartDisplay ? "true" : "false"}
                        onChange={(e) =>
                          setFormData({ ...formData, hasSmartDisplay: e.target.value === "true" })
                        }
                        className="h-10 w-full px-3 text-xs rounded-lg border border-[#D8E0EA] bg-white text-[#334155] focus:outline-none focus:border-[#1769E8] focus:ring-1 focus:ring-[#1769E8]/20 cursor-pointer"
                      >
                        <option value="true">Equipped</option>
                        <option value="false">None</option>
                      </select>
                    </div>
                  </div>

                  <div>
                    <label className="block text-xs font-bold text-[#334155] mb-1">
                      Floor / Block Location
                    </label>
                    <input
                      type="text"
                      value={formData.floor}
                      onChange={(e) => setFormData({ ...formData, floor: e.target.value })}
                      placeholder="e.g. Ground Floor, West Wing"
                      className="h-10 w-full px-3 text-xs rounded-lg border border-[#D8E0EA] bg-white text-[#334155] focus:outline-none focus:border-[#1769E8] focus:ring-1 focus:ring-[#1769E8]/20"
                    />
                  </div>

                  <div>
                    <label className="block text-xs font-bold text-[#334155] mb-2">
                      Status
                    </label>
                    <div className="flex items-center gap-5 text-xs">
                      <label className="flex items-center gap-1.5 cursor-pointer text-[#334155]">
                        <input
                          type="radio"
                          name="roomStatus"
                          checked={formData.status === "Active"}
                          onChange={() => setFormData({ ...formData, status: "Active" })}
                          className="text-[#1769E8] focus:ring-[#1769E8]"
                        />
                        <span className="font-medium">Active</span>
                      </label>

                      <label className="flex items-center gap-1.5 cursor-pointer text-[#334155]">
                        <input
                          type="radio"
                          name="roomStatus"
                          checked={formData.status === "Inactive"}
                          onChange={() => setFormData({ ...formData, status: "Inactive" })}
                          className="text-[#1769E8] focus:ring-[#1769E8]"
                        />
                        <span className="font-medium">Inactive</span>
                      </label>
                    </div>
                  </div>
                </form>
              )}
            </div>

            <div className="px-6 py-3.5 border-t border-[#E5EAF0] flex items-center justify-end gap-2.5 bg-slate-50/50">
              <button
                type="button"
                onClick={() => setIsModalOpen(false)}
                className="px-4 py-2 text-xs font-semibold text-[#64748B] bg-white hover:bg-slate-100 border border-[#E5EAF0] rounded-lg transition-colors cursor-pointer"
              >
                {modalMode === "view" ? "Close" : "Cancel"}
              </button>

              {modalMode !== "view" && (
                <button
                  type="submit"
                  form="room-form"
                  className="px-5 py-2 text-xs font-bold text-white bg-[#1769E8] hover:bg-[#1257C7] rounded-lg transition-colors flex items-center gap-1.5 shadow-xs cursor-pointer"
                >
                  <Save className="w-3.5 h-3.5" />
                  <span>{modalMode === "create" ? "Add Facility" : "Save Changes"}</span>
                </button>
              )}
            </div>
          </div>
        </div>
      )}
    </div>
  );
}
