"use client";

import React, { useState } from "react";
import Link from "next/link";
import {
  Search,
  RotateCcw,
  Plus,
  Eye,
  Edit2,
  Trash2,
  ChevronLeft,
  ChevronRight,
  FileSpreadsheet,
  FileText,
  FlaskConical,
  CheckCircle2,
  X,
  MapPin,
  Clock,
  Layers,
} from "lucide-react";
import { toast } from "sonner";

interface LabScheduleRecord {
  id: string;
  labName: string;
  location: string;
  timing: string;
  moduleName: string;
  targetGroup: string;
  instructorName: string;
  status: "Active" | "Scheduled" | "Maintenance";
}

const initialLabSchedules: LabScheduleRecord[] = [
  {
    id: "lab-1",
    labName: "Hospitality IT Lab 1",
    location: "Building B - 2nd Floor",
    timing: "Mon & Wed (11:00 AM - 01:00 PM)",
    moduleName: "Opera PMS & Fidelio Simulation",
    targetGroup: "Semester III - Grp A",
    instructorName: "Dr. Ramesh Sharma",
    status: "Active",
  },
  {
    id: "lab-2",
    labName: "Language & Communication Lab",
    location: "Building A - 1st Floor",
    timing: "Tue & Thu (02:00 PM - 03:30 PM)",
    moduleName: "French for Hotel Professionals",
    targetGroup: "Semester I - Grp B",
    instructorName: "Ms. Moumita Bose",
    status: "Active",
  },
  {
    id: "lab-3",
    labName: "Food Microbiology Lab",
    location: "Science Wing - Ground Floor",
    timing: "Wednesday (10:00 AM - 12:00 PM)",
    moduleName: "HACCP & Hygiene Testing",
    targetGroup: "Semester V - Grp A",
    instructorName: "Dr. Ramesh Sharma",
    status: "Scheduled",
  },
  {
    id: "lab-4",
    labName: "Housekeeping Model Suite",
    location: "Wing C - 3rd Floor",
    timing: "Thursday (09:00 AM - 12:00 PM)",
    moduleName: "Turn-Down & Inspection Protocol",
    targetGroup: "Semester III - Grp B",
    instructorName: "Ms. Moumita Bose",
    status: "Active",
  },
  {
    id: "lab-5",
    labName: "Beverage Sensory Lab",
    location: "Cellar Block - Room 04",
    timing: "Friday (03:00 PM - 05:00 PM)",
    moduleName: "Wine Appreciation & Mixology",
    targetGroup: "Semester V - Grp B",
    instructorName: "Prof. Sunetra Sen",
    status: "Maintenance",
  },
];

export default function LaboratorySchedulePage() {
  const [labSchedules, setLabSchedules] = useState<LabScheduleRecord[]>(initialLabSchedules);
  const [selectedLab, setSelectedLab] = useState("All Labs");
  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 [viewingLab, setViewingLab] = useState<LabScheduleRecord | null>(null);
  const [editingId, setEditingId] = useState<string | null>(null);

  const [formData, setFormData] = useState({
    labName: "Hospitality IT Lab 1",
    location: "Building B - 2nd Floor",
    timing: "Mon & Wed (11:00 AM - 01:00 PM)",
    moduleName: "",
    targetGroup: "Semester III - Grp A",
    instructorName: "Dr. Ramesh Sharma",
    status: "Active" as "Active" | "Scheduled" | "Maintenance",
  });

  const filteredLabs = labSchedules.filter((l) => {
    const matchesLab = selectedLab === "All Labs" || l.labName === selectedLab;
    const matchesStatus = selectedStatus === "All Status" || l.status === selectedStatus;
    const matchesSearch =
      searchQuery === "" ||
      l.labName.toLowerCase().includes(searchQuery.toLowerCase()) ||
      l.location.toLowerCase().includes(searchQuery.toLowerCase()) ||
      l.moduleName.toLowerCase().includes(searchQuery.toLowerCase()) ||
      l.instructorName.toLowerCase().includes(searchQuery.toLowerCase()) ||
      l.targetGroup.toLowerCase().includes(searchQuery.toLowerCase());
    return matchesLab && matchesStatus && matchesSearch;
  });

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

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

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

  const handleOpenAdd = () => {
    setEditingId(null);
    setFormData({
      labName: "Hospitality IT Lab 1",
      location: "Building B - 2nd Floor",
      timing: "Mon & Wed (11:00 AM - 01:00 PM)",
      moduleName: "",
      targetGroup: "Semester III - Grp A",
      instructorName: "Dr. Ramesh Sharma",
      status: "Active",
    });
    setIsModalOpen(true);
  };

  const handleEdit = (l: LabScheduleRecord) => {
    setEditingId(l.id);
    setFormData({
      labName: l.labName,
      location: l.location,
      timing: l.timing,
      moduleName: l.moduleName,
      targetGroup: l.targetGroup,
      instructorName: l.instructorName,
      status: l.status,
    });
    setIsModalOpen(true);
  };

  const handleDelete = (id: string) => {
    setLabSchedules((prev) => prev.filter((l) => l.id !== id));
    toast.success("Laboratory schedule removed");
  };

  const handleSubmitForm = (e: React.FormEvent) => {
    e.preventDefault();
    if (!formData.moduleName.trim()) {
      toast.error("Please enter Lab Module Name");
      return;
    }

    if (editingId) {
      setLabSchedules((prev) =>
        prev.map((l) =>
          l.id === editingId
            ? {
                ...l,
                labName: formData.labName,
                location: formData.location,
                timing: formData.timing,
                moduleName: formData.moduleName,
                targetGroup: formData.targetGroup,
                instructorName: formData.instructorName,
                status: formData.status,
              }
            : l
        )
      );
      toast.success("Laboratory schedule updated");
    } else {
      const newLab: LabScheduleRecord = {
        id: `lab-${Date.now()}`,
        labName: formData.labName,
        location: formData.location,
        timing: formData.timing,
        moduleName: formData.moduleName,
        targetGroup: formData.targetGroup,
        instructorName: formData.instructorName,
        status: formData.status,
      };
      setLabSchedules((prev) => [newLab, ...prev]);
      toast.success("New laboratory schedule allocated");
    }
    setIsModalOpen(false);
  };

  const getStatusBadge = (status: "Active" | "Scheduled" | "Maintenance") => {
    switch (status) {
      case "Active":
        return "bg-[#DCFCE7] text-[#15803D] border-[#BBF7D0]";
      case "Scheduled":
        return "bg-[#E0F2FE] text-[#0369A1] border-[#BAE6FD]";
      case "Maintenance":
        return "bg-[#FEF3C7] text-[#B45309] border-[#FDE68A]";
    }
  };

  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/timetable" className="hover:text-[#1769E8] transition-colors">
            Timetable Management
          </Link>
          <span>&gt;</span>
          <span className="text-[#071A4D] font-semibold">Laboratory Schedule</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">
              Specialized Laboratory Schedule
            </h1>
            <p className="text-[15px] text-[#64748B] mt-1 font-normal">
              PMS software simulations, language labs, microbiology, and housekeeping model suite rotas.
            </p>
          </div>

          <div className="flex items-center gap-3">
            <button
              onClick={() => toast.success("Exporting lab allocations 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 laboratory usage 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={handleOpenAdd}
              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>+ Add Lab Schedule</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-4">
            <label className="block text-[11px] font-semibold text-[#64748B] uppercase tracking-wider mb-1.5">
              Laboratory Facility
            </label>
            <select
              value={selectedLab}
              onChange={(e) => setSelectedLab(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 Labs</option>
              <option>Hospitality IT Lab 1</option>
              <option>Language & Communication Lab</option>
              <option>Food Microbiology Lab</option>
              <option>Housekeeping Model Suite</option>
              <option>Beverage Sensory Lab</option>
            </select>
          </div>

          <div className="lg:col-span-3">
            <label className="block text-[11px] font-semibold text-[#64748B] uppercase tracking-wider mb-1.5">
              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 value="Active">Active Running</option>
              <option value="Scheduled">Scheduled</option>
              <option value="Maintenance">Under Maintenance</option>
            </select>
          </div>

          <div className="lg:col-span-3">
            <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 lab, module, instructor..."
                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 ${filteredLabs.length} lab allocations`)}
              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. Full-Width Table Card */}
      <div className="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">
            <FlaskConical className="w-5 h-5 text-[#1769E8]" />
            <h2 className="font-heading font-bold text-lg text-[#071A4D]">
              Specialized Laboratories ({filteredLabs.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 === filteredLabs.length &&
                      filteredLabs.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]">Laboratory Facility</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D]">Location</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D]">Scheduled Slot</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D]">Practical Module</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D]">Batch Cohort</th>
                <th className="py-3 px-3.5 font-bold text-[#071A4D]">Faculty In-Charge</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]">
              {filteredLabs.length === 0 ? (
                <tr>
                  <td colSpan={10} className="py-12 text-center text-slate-400">
                    No specialized laboratory allocations match your criteria.
                  </td>
                </tr>
              ) : (
                filteredLabs.map((l, idx) => (
                  <tr
                    key={l.id}
                    className={`hover:bg-[#F8FAFC] transition-colors ${
                      selectedRows.includes(l.id) ? "bg-[#EFF6FF]/40" : ""
                    }`}
                  >
                    <td className="py-3.5 px-3.5 text-center">
                      <input
                        type="checkbox"
                        checked={selectedRows.includes(l.id)}
                        onChange={() => handleRowSelect(l.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">
                      <div className="font-bold text-[#071A4D] text-sm flex items-center gap-1.5">
                        <FlaskConical className="w-3.5 h-3.5 text-[#1769E8]" />
                        <span>{l.labName}</span>
                      </div>
                    </td>
                    <td className="py-3.5 px-3.5 text-[#475569]">
                      <div className="flex items-center gap-1">
                        <MapPin className="w-3 h-3 text-[#94A3B8]" />
                        <span>{l.location}</span>
                      </div>
                    </td>
                    <td className="py-3.5 px-3.5 font-mono text-[11px] text-[#334155] font-semibold">
                      {l.timing}
                    </td>
                    <td className="py-3.5 px-3.5 font-medium text-[#071A4D]">
                      {l.moduleName}
                    </td>
                    <td className="py-3.5 px-3.5 text-[#64748B]">
                      <span className="font-semibold text-[#334155]">{l.targetGroup}</span>
                    </td>
                    <td className="py-3.5 px-3.5 text-[#334155] font-semibold">
                      {l.instructorName}
                    </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(
                          l.status
                        )}`}
                      >
                        {l.status}
                      </span>
                    </td>
                    <td className="py-3.5 px-4 text-right">
                      <div className="flex items-center justify-end gap-1.5">
                        <button
                          onClick={() => setViewingLab(l)}
                          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(l)}
                          className="p-1.5 text-[#64748B] hover:text-[#1769E8] hover:bg-[#EFF6FF] rounded-md transition-colors cursor-pointer"
                          title="Edit Schedule"
                        >
                          <Edit2 className="w-3.5 h-3.5" />
                        </button>
                        <button
                          onClick={() => handleDelete(l.id)}
                          className="p-1.5 text-[#64748B] hover:text-rose-600 hover:bg-rose-50 rounded-md transition-colors cursor-pointer"
                          title="Delete Schedule"
                        >
                          <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 {filteredLabs.length} of {labSchedules.length} lab allocations
          </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>

      {/* 4. MODAL: + Add / Edit Lab Schedule */}
      {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="bg-white rounded-2xl border border-[#E5EAF0] shadow-xl max-w-lg w-full overflow-hidden">
            <div className="p-4 border-b border-[#E5EAF0] flex items-center justify-between bg-[#F8FAFC]">
              <div className="flex items-center gap-2">
                <FlaskConical className="w-5 h-5 text-[#1769E8]" />
                <h3 className="font-heading font-bold text-base text-[#071A4D]">
                  {editingId ? "Edit Lab Allocation" : "Allocate Laboratory Schedule"}
                </h3>
              </div>
              <button
                onClick={() => setIsModalOpen(false)}
                className="p-1 text-[#64748B] hover:text-[#071A4D] rounded-md transition-colors cursor-pointer"
              >
                <X className="w-5 h-5" />
              </button>
            </div>

            <form onSubmit={handleSubmitForm} className="p-5 space-y-4 text-xs">
              <div className="grid grid-cols-2 gap-3">
                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Laboratory <span className="text-rose-500">*</span>
                  </label>
                  <select
                    value={formData.labName}
                    onChange={(e) => setFormData({ ...formData, labName: 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>Hospitality IT Lab 1</option>
                    <option>Language & Communication Lab</option>
                    <option>Food Microbiology Lab</option>
                    <option>Housekeeping Model Suite</option>
                    <option>Beverage Sensory Lab</option>
                  </select>
                </div>

                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Campus Location
                  </label>
                  <input
                    type="text"
                    value={formData.location}
                    onChange={(e) => setFormData({ ...formData, location: e.target.value })}
                    placeholder="e.g. Building B - 2nd Floor"
                    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"
                  />
                </div>
              </div>

              <div>
                <label className="block font-semibold text-[#334155] mb-1">
                  Practical Module <span className="text-rose-500">*</span>
                </label>
                <input
                  type="text"
                  required
                  value={formData.moduleName}
                  onChange={(e) => setFormData({ ...formData, moduleName: e.target.value })}
                  placeholder="e.g. Opera PMS & Fidelio Simulation"
                  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">
                    Scheduled Day & Time
                  </label>
                  <input
                    type="text"
                    value={formData.timing}
                    onChange={(e) => setFormData({ ...formData, timing: e.target.value })}
                    placeholder="e.g. Mon & Wed (11:00 AM - 01:00 PM)"
                    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"
                  />
                </div>

                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Cohort Group
                  </label>
                  <input
                    type="text"
                    value={formData.targetGroup}
                    onChange={(e) => setFormData({ ...formData, targetGroup: e.target.value })}
                    placeholder="e.g. Semester III - Grp A"
                    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"
                  />
                </div>
              </div>

              <div className="grid grid-cols-2 gap-3">
                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Instructor In-Charge
                  </label>
                  <select
                    value={formData.instructorName}
                    onChange={(e) => setFormData({ ...formData, instructorName: 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>Dr. Ramesh Sharma</option>
                    <option>Ms. Moumita Bose</option>
                    <option>Prof. Sunetra Sen</option>
                  </select>
                </div>

                <div>
                  <label className="block font-semibold text-[#334155] mb-1">
                    Operational Status
                  </label>
                  <select
                    value={formData.status}
                    onChange={(e) =>
                      setFormData({
                        ...formData,
                        status: e.target.value as "Active" | "Scheduled" | "Maintenance",
                      })
                    }
                    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="Active">Active Running</option>
                    <option value="Scheduled">Scheduled</option>
                    <option value="Maintenance">Under Maintenance</option>
                  </select>
                </div>
              </div>

              <div className="flex items-center gap-2 pt-3 border-t border-[#E5EAF0]">
                <button
                  type="button"
                  onClick={() => setIsModalOpen(false)}
                  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"
                >
                  <CheckCircle2 className="w-3.5 h-3.5" />
                  <span>{editingId ? "Update Schedule" : "Save Schedule"}</span>
                </button>
              </div>
            </form>
          </div>
        </div>
      )}

      {/* 5. MODAL: View Lab Details */}
      {viewingLab && (
        <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="bg-white rounded-2xl border border-[#E5EAF0] shadow-xl max-w-md w-full overflow-hidden">
            <div className="p-4 border-b border-[#E5EAF0] flex items-center justify-between bg-[#F8FAFC]">
              <div className="flex items-center gap-2">
                <FlaskConical className="w-5 h-5 text-[#1769E8]" />
                <h3 className="font-heading font-bold text-base text-[#071A4D]">
                  Laboratory Details
                </h3>
              </div>
              <button
                onClick={() => setViewingLab(null)}
                className="p-1 text-[#64748B] hover:text-[#071A4D] rounded-md transition-colors cursor-pointer"
              >
                <X className="w-5 h-5" />
              </button>
            </div>

            <div className="p-5 space-y-4 text-xs">
              <div className="flex items-center justify-between pb-3 border-b border-[#E5EAF0]">
                <div>
                  <h4 className="font-heading font-bold text-base text-[#071A4D]">
                    {viewingLab.labName}
                  </h4>
                  <p className="text-[11px] text-[#64748B]">{viewingLab.location}</p>
                </div>
                <span
                  className={`px-2.5 py-1 rounded-full text-xs font-bold border ${getStatusBadge(
                    viewingLab.status
                  )}`}
                >
                  {viewingLab.status}
                </span>
              </div>

              <div className="space-y-2.5">
                <div className="flex items-center justify-between p-2.5 bg-[#F8FAFC] rounded-lg border border-[#E5EAF0]">
                  <span className="font-medium text-[#64748B]">Practical Module</span>
                  <span className="font-bold text-[#071A4D]">{viewingLab.moduleName}</span>
                </div>
                <div className="flex items-center justify-between p-2.5 bg-[#F8FAFC] rounded-lg border border-[#E5EAF0]">
                  <span className="font-medium text-[#64748B]">Slot Timings</span>
                  <span className="font-mono font-semibold text-[#071A4D]">{viewingLab.timing}</span>
                </div>
                <div className="flex items-center justify-between p-2.5 bg-[#F8FAFC] rounded-lg border border-[#E5EAF0]">
                  <span className="font-medium text-[#64748B]">Assigned Cohort</span>
                  <span className="font-bold text-[#1769E8]">{viewingLab.targetGroup}</span>
                </div>
                <div className="flex items-center justify-between p-2.5 bg-[#F8FAFC] rounded-lg border border-[#E5EAF0]">
                  <span className="font-medium text-[#64748B]">Instructor In-Charge</span>
                  <span className="font-bold text-[#071A4D]">{viewingLab.instructorName}</span>
                </div>
              </div>

              <div className="flex items-center justify-end gap-2 pt-3 border-t border-[#E5EAF0]">
                <button
                  type="button"
                  onClick={() => {
                    const toEdit = viewingLab;
                    setViewingLab(null);
                    handleEdit(toEdit);
                  }}
                  className="py-2 px-3 border border-[#E2E8F0] hover:bg-slate-50 text-[#334155] rounded-lg text-xs font-semibold transition-colors cursor-pointer"
                >
                  Edit Allocation
                </button>
                <button
                  type="button"
                  onClick={() => setViewingLab(null)}
                  className="py-2 px-4 bg-[#1769E8] hover:bg-[#1255be] text-white rounded-lg text-xs font-semibold transition-colors cursor-pointer"
                >
                  Close
                </button>
              </div>
            </div>
          </div>
        </div>
      )}
    </div>
  );
}
