"use client"

import { useState } from "react"
import Modal from "../Modal"

export default function FilmDetailsSection({ reportId, data }: any) {
  const [films, setFilms] = useState(data || [])
  const [showModal, setShowModal] = useState(false)
  const [formData, setFormData] = useState({ time: new Date().toISOString(), width: "", rollNumber: "", productCode: "", palletNumber: "" })

  const handleAdd = async () => {
    const updated = [...films, formData]
    setFilms(updated)
    await fetch(`/api/reports/${reportId}`, {
      method: "PATCH",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ filmDetails: updated })
    })
    setShowModal(false)
    setFormData({ time: new Date().toISOString(), width: "", rollNumber: "", productCode: "", palletNumber: "" })
  }

  return (
    <div className="bg-white p-6 rounded-xl shadow-sm border border-gray-200">
      <div className="flex justify-between items-center mb-4">
        <h2 className="text-xl font-bold text-gray-800">Film Details</h2>
        <button onClick={() => setShowModal(true)} className="bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition-colors">
          + Add New Film
        </button>
      </div>

      <div className="overflow-x-auto">
        <table className="w-full">
          <thead className="bg-gray-50">
            <tr>
              <th className="px-4 py-2 text-left text-sm font-medium text-gray-500">Time</th>
              <th className="px-4 py-2 text-left text-sm font-medium text-gray-500">Width</th>
              <th className="px-4 py-2 text-left text-sm font-medium text-gray-500">Roll Number</th>
              <th className="px-4 py-2 text-left text-sm font-medium text-gray-500">Product Code</th>
              <th className="px-4 py-2 text-left text-sm font-medium text-gray-500">Pallet Number</th>
            </tr>
          </thead>
          <tbody className="divide-y">
            {films.map((f: any, i: number) => (
              <tr key={i}>
                <td className="px-4 py-2 text-sm">{new Date(f.time).toLocaleString()}</td>
                <td className="px-4 py-2 text-sm">{f.width}</td>
                <td className="px-4 py-2 text-sm">{f.rollNumber}</td>
                <td className="px-4 py-2 text-sm">{f.productCode}</td>
                <td className="px-4 py-2 text-sm">{f.palletNumber}</td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>

      {showModal && (
        <Modal onClose={() => setShowModal(false)}>
          <h3 className="text-lg font-bold mb-4">Add New Film</h3>
          <div className="space-y-4">
            <div>
              <label className="block text-sm font-medium mb-1">Width</label>
              <input type="text" value={formData.width} onChange={(e) => setFormData({...formData, width: e.target.value})} className="w-full px-3 py-2 border rounded-lg" />
            </div>
            <div>
              <label className="block text-sm font-medium mb-1">Roll Number</label>
              <input type="text" value={formData.rollNumber} onChange={(e) => setFormData({...formData, rollNumber: e.target.value})} className="w-full px-3 py-2 border rounded-lg" />
            </div>
            <div>
              <label className="block text-sm font-medium mb-1">Product Code</label>
              <input type="text" value={formData.productCode} onChange={(e) => setFormData({...formData, productCode: e.target.value})} className="w-full px-3 py-2 border rounded-lg" />
            </div>
            <div>
              <label className="block text-sm font-medium mb-1">Pallet Number</label>
              <input type="text" value={formData.palletNumber} onChange={(e) => setFormData({...formData, palletNumber: e.target.value})} className="w-full px-3 py-2 border rounded-lg" />
            </div>
            <button onClick={handleAdd} className="w-full bg-blue-600 text-white py-2 rounded-lg hover:bg-blue-700">Add</button>
          </div>
        </Modal>
      )}
    </div>
  )
}
