'use client';

import { useState, use } from 'react';
import Link from 'next/link';
import {
  Battery, ArrowLeft, X, BatteryCharging, Activity, Gauge, MapPin, Bike, User, History,
  Calendar, DollarSign, CheckCircle, Clock, ArrowRightLeft, Handshake, TrendingUp, Edit,
  RefreshCw
} from 'lucide-react';

interface BMSData {
  voltage: number;
  current: number;
  soc: number;
  temperature: number;
  cycles: number;
  health: number;
  timestamp: string;
}

interface OwnershipLog {
  id: string;
  batteryId: string;
  fromOwner: string;
  fromOwnerType: 'company' | 'biker' | 'hub' | 'swap-station';
  toOwner: string;
  toOwnerType: 'company' | 'biker' | 'hub' | 'swap-station';
  fromBikeId?: string;
  fromBikeModel?: string;
  toBikeId?: string;
  toBikeModel?: string;
  fromHubId?: string;
  fromHubName?: string;
  toHubId?: string;
  toHubName?: string;
  fromStationId?: string;
  fromStationName?: string;
  toStationId?: string;
  toStationName?: string;
  action: 'assigned' | 'rented' | 'swapped' | 'returned' | 'retired';
  rentAmount?: number;
  notes?: string;
  timestamp: string;
}

interface Battery {
  id: string;
  serialNumber: string;
  brand: string;
  model: string;
  type: 'lithium-ion' | 'lifepo4' | 'lead-acid';
  capacity: number;
  voltage: number;
  purchaseDate: string;
  purchasePrice: number;
  warrantyExpiry: string;
  status: 'available' | 'in-use' | 'maintenance' | 'retired' | 'charging';
  currentSoc: number;
  health: number;
  cycleCount: number;
  assignedBikeId?: string;
  assignedBikeModel?: string;
  assignedBikePlate?: string;
  assignedBikerId?: string;
  assignedBikerName?: string;
  assignedBikerPhone?: string;
  currentHubId?: string;
  currentHubName?: string;
  currentStationId?: string;
  currentStationName?: string;
  lastMaintenance?: string;
  nextMaintenance?: string;
  bmsData?: BMSData;
  monthlyRent?: number;
  ownershipLogs: OwnershipLog[];
}

const mockBattery: Battery = {
  id: 'BAT-001',
  serialNumber: 'SN-2024-00001',
  brand: 'EVE Energy',
  model: 'Li-Ion 60V50Ah',
  type: 'lithium-ion',
  capacity: 50,
  voltage: 60,
  purchaseDate: '2024-01-15',
  purchasePrice: 45000,
  warrantyExpiry: '2027-01-15',
  status: 'in-use',
  currentSoc: 78,
  health: 95,
  cycleCount: 156,
  assignedBikeId: 'EV001',
  assignedBikeModel: 'Etron ET50',
  assignedBikePlate: 'Dhaka Metro Cha-A-1234',
  assignedBikerId: 'BK-001',
  assignedBikerName: 'Rahim Ahmed',
  assignedBikerPhone: '01712345678',
  currentHubId: 'HUB-001',
  currentHubName: 'JAIBEN Head Office',
  monthlyRent: 1500,
  lastMaintenance: '2024-03-01',
  nextMaintenance: '2024-06-01',
  bmsData: { voltage: 67.2, current: -2.5, soc: 78, temperature: 32, cycles: 156, health: 95, timestamp: '2024-03-28 12:00:00' },
  ownershipLogs: [
    { id: 'OL-001', batteryId: 'BAT-001', fromOwner: 'JAIBEN Hub', fromOwnerType: 'hub', toOwner: 'Rahim Ahmed', toOwnerType: 'biker', toBikeId: 'EV001', toBikeModel: 'Etron ET50', toHubId: 'HUB-001', toHubName: 'JAIBEN Head Office', action: 'rented', rentAmount: 1500, timestamp: '2024-03-15 08:30:00' },
    { id: 'OL-002', batteryId: 'BAT-001', fromOwner: 'Rahim Ahmed', fromOwnerType: 'biker', toOwner: 'JAIBEN Hub', toOwnerType: 'hub', fromBikeId: 'EV001', fromBikeModel: 'Etron ET50', fromHubId: 'HUB-001', fromHubName: 'JAIBEN Head Office', action: 'returned', timestamp: '2024-03-28 18:00:00', notes: 'Returned for charging' },
    { id: 'OL-003', batteryId: 'BAT-001', fromOwner: 'JAIBEN Hub', fromOwnerType: 'hub', toOwner: 'Rahim Ahmed', toOwnerType: 'biker', toBikeId: 'EV001', toBikeModel: 'Etron ET50', toHubId: 'HUB-001', toHubName: 'JAIBEN Head Office', action: 'rented', rentAmount: 1500, timestamp: '2024-03-28 18:30:00' },
  ]
};

const statusColors: Record<string, string> = {
  available: 'bg-green-100 text-green-700',
  'in-use': 'bg-blue-100 text-blue-700',
  maintenance: 'bg-amber-100 text-amber-700',
  retired: 'bg-slate-100 text-slate-500',
  charging: 'bg-purple-100 text-purple-700',
};

const typeLabels: Record<string, string> = {
  'lithium-ion': 'Lithium-Ion',
  'lifepo4': 'LiFePO4',
  'lead-acid': 'Lead Acid',
};

export default function BatteryDetailPage({ params }: { params: Promise<{ id: string }> }) {
  const { id } = use(params);
  const [battery, setBattery] = useState<Battery>(mockBattery);
  const [activeTab, setActiveTab] = useState<'info' | 'bms' | 'history' | 'rent'>('info');
  const [showEditModal, setShowEditModal] = useState(false);
  const [refreshing, setRefreshing] = useState(false);

  const handleSaveEdit = (updatedBattery: Battery) => {
    setBattery(updatedBattery);
    setShowEditModal(false);
  };

  const handleRefreshBMS = () => {
    if (!battery.bmsData) return;
    setRefreshing(true);
    setTimeout(() => {
      const bms = battery.bmsData!;
      setBattery({
        ...battery,
        bmsData: {
          ...bms,
          voltage: Math.round((60 + Math.random() * 10) * 10) / 10,
          current: Math.round((-3 + Math.random() * 4) * 10) / 10,
          soc: Math.floor(Math.random() * 40) + 60,
          temperature: 25 + Math.floor(Math.random() * 15),
          cycles: bms.cycles + 1,
          health: Math.max(70, Math.min(100, bms.health + (Math.random() > 0.7 ? 1 : 0))),
          timestamp: new Date().toISOString().replace('T', ' ').substring(0, 19)
        },
        currentSoc: Math.floor(Math.random() * 40) + 60,
        health: Math.max(70, Math.min(100, battery.health + (Math.random() > 0.7 ? 1 : 0)))
      });
      setRefreshing(false);
    }, 1000);
  };

  return (
    <div className="p-4 lg:p-6">
      <div className="flex items-center gap-4 mb-6">
        <Link href="/admin/batteries" className="p-2 hover:bg-slate-100 rounded-lg">
          <ArrowLeft className="w-5 h-5 text-slate-600" />
        </Link>
        <div className="flex-1">
          <h1 className="text-2xl lg:text-3xl font-extrabold text-slate-800">Battery Details</h1>
          <p className="text-sm text-slate-500">ID: {battery.id}</p>
        </div>
        <button
          onClick={() => setShowEditModal(true)}
          className="py-2 px-4 bg-accent text-white rounded-lg font-semibold text-sm hover:bg-accent-dark flex items-center gap-2"
        >
          <Edit className="w-4 h-4" /> Edit
        </button>
      </div>



      <div className="bg-white rounded-xl shadow-sm border border-slate-100 mb-6">
        <div className="p-5 border-b border-slate-100">
          <div className="flex flex-col lg:flex-row lg:items-center gap-4">
            <div className="flex items-center gap-4">
              <div className={`w-14 h-14 rounded-xl flex items-center justify-center ${battery.type === 'lithium-ion' ? 'bg-blue-50' : battery.type === 'lifepo4' ? 'bg-green-50' : 'bg-amber-50'}`}>
                <Battery className={`w-7 h-7 ${battery.type === 'lithium-ion' ? 'text-blue-600' : battery.type === 'lifepo4' ? 'text-green-600' : 'text-amber-600'}`} />
              </div>
              <div>
                <h2 className="text-lg font-bold text-slate-800">{battery.brand} {battery.model}</h2>
                <p className="text-sm text-slate-500">{battery.serialNumber}</p>
                <span className={`inline-flex items-center gap-1 text-xs font-medium px-2 py-0.5 rounded-full mt-1 ${statusColors[battery.status]}`}>
                  {battery.status}
                </span>
              </div>
            </div>

            <div className="flex-1 grid grid-cols-2 md:grid-cols-4 gap-3 lg:ml-auto">
              <div className="text-center">
                <p className="text-xs text-slate-500">SOC</p>
                <p className={`text-lg font-bold ${battery.currentSoc > 50 ? 'text-green-600' : battery.currentSoc > 20 ? 'text-amber-600' : 'text-red-600'}`}>{battery.currentSoc}%</p>
              </div>
              <div className="text-center">
                <p className="text-xs text-slate-500">Health</p>
                <p className={`text-lg font-bold ${battery.health > 80 ? 'text-green-600' : battery.health > 60 ? 'text-amber-600' : 'text-red-600'}`}>{battery.health}%</p>
              </div>
              <div className="text-center">
                <p className="text-xs text-slate-500">Cycles</p>
                <p className="text-lg font-bold text-slate-700">{battery.cycleCount}</p>
              </div>
              <div className="text-center">
                <p className="text-xs text-slate-500">Price</p>
                <p className="text-lg font-bold text-slate-700">৳{battery.purchasePrice.toLocaleString()}</p>
              </div>
            </div>
          </div>
        </div>

        {battery.bmsData && (
          <div className="p-5 border-b border-slate-100 bg-gradient-to-r from-green-50 to-emerald-50">
            <div className="flex items-center justify-between mb-3">
              <div className="flex items-center gap-2">
                <Activity className="w-4 h-4 text-green-600" />
                <span className="font-medium text-green-800">Live BMS Data</span>
                <span className="text-xs text-green-600 bg-green-100 px-2 py-0.5 rounded-full">Real-time</span>
              </div>
              <div className="flex items-center gap-2">
                <span className="text-xs text-green-600">Updated: {battery.bmsData.timestamp}</span>
                <button onClick={handleRefreshBMS} disabled={refreshing} className="p-1.5 text-green-600 hover:bg-green-100 rounded-lg">
                  <RefreshCw className={`w-4 h-4 ${refreshing ? 'animate-spin' : ''}`} />
                </button>
              </div>
            </div>
            <div className="grid grid-cols-3 md:grid-cols-6 gap-2">
              <div className="bg-white rounded-lg p-2 text-center">
                <p className="text-xs text-slate-500">Voltage</p>
                <p className="font-semibold text-slate-700">{battery.bmsData.voltage}V</p>
              </div>
              <div className="bg-white rounded-lg p-2 text-center">
                <p className="text-xs text-slate-500">Current</p>
                <p className="font-semibold text-slate-700">{battery.bmsData.current}A</p>
              </div>
              <div className="bg-white rounded-lg p-2 text-center">
                <p className="text-xs text-slate-500">SOC</p>
                <p className="font-semibold text-green-600">{battery.bmsData.soc}%</p>
              </div>
              <div className="bg-white rounded-lg p-2 text-center">
                <p className="text-xs text-slate-500">Temp</p>
                <p className="font-semibold text-slate-700">{battery.bmsData.temperature}°C</p>
              </div>
              <div className="bg-white rounded-lg p-2 text-center">
                <p className="text-xs text-slate-500">Cycles</p>
                <p className="font-semibold text-slate-700">{battery.bmsData.cycles}</p>
              </div>
              <div className="bg-white rounded-lg p-2 text-center">
                <p className="text-xs text-slate-500">Health</p>
                <p className={`font-semibold ${battery.bmsData.health > 80 ? 'text-green-600' : 'text-amber-600'}`}>{battery.bmsData.health}%</p>
              </div>
            </div>
          </div>
        )}

        <div className="border-b border-slate-100">
          <div className="flex gap-1 p-1">
            <button onClick={() => setActiveTab('info')} className={`px-4 py-2 rounded-lg text-sm font-medium ${activeTab === 'info' ? 'bg-accent text-white' : 'text-slate-600 hover:bg-slate-50'}`}>Info</button>
            <button onClick={() => setActiveTab('bms')} className={`px-4 py-2 rounded-lg text-sm font-medium ${activeTab === 'bms' ? 'bg-accent text-white' : 'text-slate-600 hover:bg-slate-50'}`}>BMS</button>
            <button onClick={() => setActiveTab('history')} className={`px-4 py-2 rounded-lg text-sm font-medium ${activeTab === 'history' ? 'bg-accent text-white' : 'text-slate-600 hover:bg-slate-50'}`}>History</button>
            <button onClick={() => setActiveTab('rent')} className={`px-4 py-2 rounded-lg text-sm font-medium ${activeTab === 'rent' ? 'bg-accent text-white' : 'text-slate-600 hover:bg-slate-50'}`}>Rent</button>
          </div>
        </div>

        <div className="p-5">
          {activeTab === 'info' && (
            <div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Brand</p>
                <p className="font-medium text-slate-700">{battery.brand}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Model</p>
                <p className="font-medium text-slate-700">{battery.model}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Serial Number</p>
                <p className="font-medium text-slate-700">{battery.serialNumber}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Type</p>
                <p className="font-medium text-slate-700">{typeLabels[battery.type]}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Capacity</p>
                <p className="font-medium text-slate-700">{battery.capacity} Ah</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Voltage</p>
                <p className="font-medium text-slate-700">{battery.voltage} V</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Purchase Date</p>
                <p className="font-medium text-slate-700">{battery.purchaseDate}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Warranty Expiry</p>
                <p className="font-medium text-slate-700">{battery.warrantyExpiry}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Last Maintenance</p>
                <p className="font-medium text-slate-700">{battery.lastMaintenance || 'N/A'}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Next Maintenance</p>
                <p className="font-medium text-slate-700">{battery.nextMaintenance || 'N/A'}</p>
              </div>
              <div className="bg-slate-50 rounded-lg p-3">
                <p className="text-xs text-slate-500">Purchase Price</p>
                <p className="font-medium text-slate-700">৳{battery.purchasePrice.toLocaleString()}</p>
              </div>
              {battery.monthlyRent && (
                <div className="bg-slate-50 rounded-lg p-3">
                  <p className="text-xs text-slate-500">Monthly Rent</p>
                  <p className="font-medium text-green-600">৳{battery.monthlyRent}/month</p>
                </div>
              )}
            </div>
          )}

          {activeTab === 'bms' && battery.bmsData && (
            <div className="space-y-4">
              <div className="flex items-center justify-between">
                <div className="flex items-center gap-2">
                  <Activity className="w-5 h-5 text-green-600" />
                  <h4 className="font-medium text-slate-700">BMS Real-time Data</h4>
                </div>
                <span className="text-xs text-slate-400">Updated: {battery.bmsData.timestamp}</span>
              </div>
              <div className="grid grid-cols-2 md:grid-cols-3 gap-4">
                <div className="bg-slate-50 rounded-lg p-4 text-center">
                  <p className="text-xs text-slate-500 mb-1">Voltage</p>
                  <p className="text-2xl font-bold text-slate-700">{battery.bmsData.voltage}V</p>
                </div>
                <div className="bg-slate-50 rounded-lg p-4 text-center">
                  <p className="text-xs text-slate-500 mb-1">Current</p>
                  <p className="text-2xl font-bold text-slate-700">{battery.bmsData.current}A</p>
                </div>
                <div className="bg-slate-50 rounded-lg p-4 text-center">
                  <p className="text-xs text-slate-500 mb-1">SOC</p>
                  <p className="text-2xl font-bold text-green-600">{battery.bmsData.soc}%</p>
                </div>
                <div className="bg-slate-50 rounded-lg p-4 text-center">
                  <p className="text-xs text-slate-500 mb-1">Temperature</p>
                  <p className="text-2xl font-bold text-slate-700">{battery.bmsData.temperature}°C</p>
                </div>
                <div className="bg-slate-50 rounded-lg p-4 text-center">
                  <p className="text-xs text-slate-500 mb-1">Cycles</p>
                  <p className="text-2xl font-bold text-slate-700">{battery.bmsData.cycles}</p>
                </div>
                <div className="bg-slate-50 rounded-lg p-4 text-center">
                  <p className="text-xs text-slate-500 mb-1">Health</p>
                  <p className={`text-2xl font-bold ${battery.bmsData.health > 80 ? 'text-green-600' : 'text-amber-600'}`}>{battery.bmsData.health}%</p>
                </div>
              </div>
            </div>
          )}

          {activeTab === 'history' && (
            <div>
              <div className="flex items-center gap-2 mb-4">
                <History className="w-5 h-5 text-purple-600" />
                <h4 className="font-medium text-slate-700">Ownership History</h4>
              </div>
              {battery.ownershipLogs.length === 0 ? (
                <div className="text-center py-8 text-slate-400">No history records</div>
              ) : (
                <div className="space-y-3">
                  {battery.ownershipLogs.map((log, index) => (
                    <div key={log.id} className="relative pl-6 pb-4 border-l-2 border-slate-200 last:border-0">
                      <div className={`absolute -left-2 top-0 w-4 h-4 rounded-full ${log.action === 'rented' ? 'bg-green-500' : log.action === 'swapped' ? 'bg-blue-500' : log.action === 'returned' ? 'bg-amber-500' : 'bg-slate-400'}`} />
                      <div className="bg-slate-50 rounded-lg p-3">
                        <div className="flex items-center justify-between mb-2">
                          <span className={`text-xs font-medium px-2 py-0.5 rounded-full ${log.action === 'rented' ? 'bg-green-100 text-green-700' : log.action === 'swapped' ? 'bg-blue-100 text-blue-700' : log.action === 'returned' ? 'bg-amber-100 text-amber-700' : 'bg-slate-100 text-slate-500'}`}>
                            {log.action}
                          </span>
                          <span className="text-xs text-slate-400">{log.timestamp}</span>
                        </div>
                        <div className="grid grid-cols-2 gap-2 text-sm">
                          <div>
                            <p className="text-xs text-slate-500">From</p>
                            <p className="font-medium text-slate-700">{log.fromOwner}</p>
                          </div>
                          <div>
                            <p className="text-xs text-slate-500">To</p>
                            <p className="font-medium text-slate-700">{log.toOwner}</p>
                          </div>
                        </div>
                        {log.rentAmount && (
                          <p className="text-xs text-green-600 mt-2">Rent: ৳{log.rentAmount}/month</p>
                        )}
                        {log.notes && (
                          <p className="text-xs text-slate-400 mt-1 italic">{log.notes}</p>
                        )}
                      </div>
                    </div>
                  ))}
                </div>
              )}
            </div>
          )}

          {activeTab === 'rent' && (
            <div>
              <div className="flex items-center gap-2 mb-4">
                <DollarSign className="w-5 h-5 text-green-600" />
                <h4 className="font-medium text-slate-700">Rent Information</h4>
              </div>
              {battery.monthlyRent ? (
                <div className="bg-green-50 rounded-lg p-5 border border-green-100">
                  <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                      <p className="text-sm text-green-600 mb-1">Monthly Rent Amount</p>
                      <p className="text-3xl font-bold text-green-700">৳{battery.monthlyRent}</p>
                      <p className="text-xs text-green-500 mt-1">per month</p>
                    </div>
                    <div>
                      <p className="text-sm text-green-600 mb-1">Status</p>
                      <p className="text-lg font-semibold text-green-700">{battery.assignedBikerName ? 'Rented' : 'Not Rented'}</p>
                      <p className="text-xs text-green-500 mt-1">{battery.assignedBikerName ? `to ${battery.assignedBikerName}` : 'Available for rental'}</p>
                    </div>
                  </div>
                </div>
              ) : (
                <div className="bg-slate-50 rounded-lg p-5 text-center text-slate-500">
                  This battery is not set up for rental.
                </div>
              )}
            </div>
          )}
        </div>
      </div>

      {battery.status === 'in-use' && (
        <div className="bg-blue-50 rounded-xl border border-blue-200 p-5 mb-6">
          <div className="flex items-center gap-3 mb-4">
            <div className="w-10 h-10 rounded-lg bg-blue-100 flex items-center justify-center">
              <User className="w-5 h-5 text-blue-600" />
            </div>
            <div>
              <h3 className="font-semibold text-blue-800">Currently Rented to Biker</h3>
              <span className="text-xs text-blue-600">Active Rental</span>
            </div>
          </div>

          <div className="grid grid-cols-2 md:grid-cols-4 gap-4">
            <div className="bg-white rounded-lg p-3">
              <p className="text-xs text-blue-500 mb-1">Biker</p>
              <p className="font-medium text-blue-900">{battery.assignedBikerName}</p>
              <p className="text-xs text-blue-400">{battery.assignedBikerPhone}</p>
            </div>
            <div className="bg-white rounded-lg p-3">
              <p className="text-xs text-blue-500 mb-1">Bike</p>
              <p className="font-medium text-blue-900">{battery.assignedBikeModel}</p>
              <p className="text-xs text-blue-400">{battery.assignedBikePlate}</p>
            </div>
            <div className="bg-white rounded-lg p-3">
              <p className="text-xs text-blue-500 mb-1">Hub/Station</p>
              <p className="font-medium text-blue-900">{battery.currentHubName || battery.currentStationName || 'Not Assigned'}</p>
            </div>
            <div className="bg-white rounded-lg p-3">
              <p className="text-xs text-blue-500 mb-1">Monthly Rent</p>
              <p className="font-medium text-green-600">৳{battery.monthlyRent}/month</p>
            </div>
          </div>
        </div>
      )}

      {showEditModal && (
        <EditBatteryModal
          battery={battery}
          onSave={handleSaveEdit}
          onClose={() => setShowEditModal(false)}
        />
      )}
    </div>
  );
}

function EditBatteryModal({
  battery,
  onSave,
  onClose
}: {
  battery: Battery;
  onSave: (battery: Battery) => void;
  onClose: () => void;
}) {
  const [formData, setFormData] = useState<Battery>({ ...battery });

  const handleChange = (field: keyof Battery, value: any) => {
    setFormData({ ...formData, [field]: value });
  };

  return (
    <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4">
      <div className="bg-white rounded-xl shadow-xl w-full max-w-lg max-h-[90vh] overflow-y-auto">
        <div className="p-5 border-b border-slate-100 flex items-center justify-between sticky top-0 bg-white">
          <h2 className="text-lg font-bold text-slate-800">Edit Battery</h2>
          <button onClick={onClose} className="p-2 hover:bg-slate-100 rounded-lg">
            <X className="w-5 h-5 text-slate-400" />
          </button>
        </div>
        <div className="p-5 space-y-4">
          <div className="grid grid-cols-2 gap-4">
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Brand</label>
              <input
                type="text"
                value={formData.brand}
                onChange={(e) => handleChange('brand', e.target.value)}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Model</label>
              <input
                type="text"
                value={formData.model}
                onChange={(e) => handleChange('model', e.target.value)}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Serial Number</label>
              <input
                type="text"
                value={formData.serialNumber}
                onChange={(e) => handleChange('serialNumber', e.target.value)}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Type</label>
              <select
                value={formData.type}
                onChange={(e) => handleChange('type', e.target.value)}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              >
                <option value="lithium-ion">Lithium-Ion</option>
                <option value="lifepo4">LiFePO4</option>
                <option value="lead-acid">Lead Acid</option>
              </select>
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Capacity (Ah)</label>
              <input
                type="number"
                value={formData.capacity}
                onChange={(e) => handleChange('capacity', parseInt(e.target.value))}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Voltage (V)</label>
              <input
                type="number"
                value={formData.voltage}
                onChange={(e) => handleChange('voltage', parseInt(e.target.value))}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Purchase Date</label>
              <input
                type="date"
                value={formData.purchaseDate}
                onChange={(e) => handleChange('purchaseDate', e.target.value)}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Purchase Price (৳)</label>
              <input
                type="number"
                value={formData.purchasePrice}
                onChange={(e) => handleChange('purchasePrice', parseInt(e.target.value))}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Warranty Expiry</label>
              <input
                type="date"
                value={formData.warrantyExpiry}
                onChange={(e) => handleChange('warrantyExpiry', e.target.value)}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Monthly Rent (৳)</label>
              <input
                type="number"
                value={formData.monthlyRent || 0}
                onChange={(e) => handleChange('monthlyRent', parseInt(e.target.value))}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Status</label>
              <select
                value={formData.status}
                onChange={(e) => handleChange('status', e.target.value)}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              >
                <option value="available">Available</option>
                <option value="in-use">In Use</option>
                <option value="charging">Charging</option>
                <option value="maintenance">Maintenance</option>
                <option value="retired">Retired</option>
              </select>
            </div>
            <div>
              <label className="block text-sm font-medium text-slate-700 mb-1">Current SOC (%)</label>
              <input
                type="number"
                value={formData.currentSoc}
                onChange={(e) => handleChange('currentSoc', parseInt(e.target.value))}
                className="w-full px-3 py-2 border border-slate-200 rounded-lg text-sm"
              />
            </div>
          </div>
        </div>
        <div className="p-5 border-t border-slate-100 flex gap-3 sticky bottom-0 bg-white">
          <button onClick={onClose} className="flex-1 py-2.5 px-4 border border-slate-200 rounded-lg font-semibold text-sm hover:bg-slate-50">
            Cancel
          </button>
          <button
            onClick={() => onSave(formData)}
            className="flex-1 py-2.5 px-4 bg-accent text-white rounded-lg font-semibold text-sm hover:bg-accent-dark"
          >
            Save Changes
          </button>
        </div>
      </div>
    </div>
  );
}