import React, { useState } from "react";

const StaffAttendance = () => {
  const [search, setSearch] = useState("");

  const [formData, setFormData] = useState({
    employee: "",
    department: "",
    date: "",
    status: "Present",
  });

  const [attendance, setAttendance] = useState([
    {
      id: 1,
      employee: "Rahul Sharma",
      department: "IT",
      date: "2026-06-01",
      status: "Present",
    },
    {
      id: 2,
      employee: "Neha Verma",
      department: "Accounts",
      date: "2026-06-01",
      status: "Leave",
    },
  ]);

  const handleChange = (e) => {
    setFormData({
      ...formData,
      [e.target.name]: e.target.value,
    });
  };

  const addAttendance = () => {
    if (
      !formData.employee ||
      !formData.department ||
      !formData.date
    ) {
      alert("Please fill all required fields");
      return;
    }

    setAttendance([
      ...attendance,
      {
        id: Date.now(),
        ...formData,
      },
    ]);

    setFormData({
      employee: "",
      department: "",
      date: "",
      status: "Present",
    });
  };

  const deleteAttendance = (id) => {
    setAttendance(
      attendance.filter((item) => item.id !== id)
    );
  };

  const filteredAttendance = attendance.filter(
    (item) =>
      item.employee
        .toLowerCase()
        .includes(search.toLowerCase())
  );

  return (
    <div className="p-6 bg-gray-100 min-h-screen">

      {/* Header */}

      <div className="flex justify-between items-center mb-6">

        <h1 className="text-2xl font-bold">
          Staff Attendance
        </h1>

        <div className="bg-white px-4 py-2 rounded-lg shadow">
          Total Records : {attendance.length}
        </div>

      </div>

      {/* Stats */}

      <div className="grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">

        <div className="bg-white p-5 rounded-lg shadow">
          <h3 className="text-gray-500">
            Present
          </h3>
          <p className="text-3xl font-bold text-green-600">
            {
              attendance.filter(
                (a) => a.status === "Present"
              ).length
            }
          </p>
        </div>

        <div className="bg-white p-5 rounded-lg shadow">
          <h3 className="text-gray-500">
            Absent
          </h3>
          <p className="text-3xl font-bold text-red-600">
            {
              attendance.filter(
                (a) => a.status === "Absent"
              ).length
            }
          </p>
        </div>

        <div className="bg-white p-5 rounded-lg shadow">
          <h3 className="text-gray-500">
            Leave
          </h3>
          <p className="text-3xl font-bold text-yellow-600">
            {
              attendance.filter(
                (a) => a.status === "Leave"
              ).length
            }
          </p>
        </div>

        <div className="bg-white p-5 rounded-lg shadow">
          <h3 className="text-gray-500">
            Half Day
          </h3>
          <p className="text-3xl font-bold text-blue-600">
            {
              attendance.filter(
                (a) => a.status === "Half Day"
              ).length
            }
          </p>
        </div>

      </div>

      {/* Form */}

      <div className="bg-white p-6 rounded-lg shadow mb-6">

        <h2 className="text-lg font-semibold mb-4">
          Mark Attendance
        </h2>

        <div className="grid grid-cols-1 md:grid-cols-4 gap-4">

          <input
            type="text"
            name="employee"
            placeholder="Employee Name"
            value={formData.employee}
            onChange={handleChange}
            className="border p-2 rounded"
          />

          <input
            type="text"
            name="department"
            placeholder="Department"
            value={formData.department}
            onChange={handleChange}
            className="border p-2 rounded"
          />

          <input
            type="date"
            name="date"
            value={formData.date}
            onChange={handleChange}
            className="border p-2 rounded"
          />

          <select
            name="status"
            value={formData.status}
            onChange={handleChange}
            className="border p-2 rounded"
          >
            <option>Present</option>
            <option>Absent</option>
            <option>Leave</option>
            <option>Half Day</option>
          </select>

        </div>

        <button
          onClick={addAttendance}
          className="bg-blue-600 text-white px-5 py-2 rounded mt-4"
        >
          Save Attendance
        </button>

      </div>

      {/* Search */}

      <div className="bg-white p-4 rounded-lg shadow mb-6">

        <input
          type="text"
          placeholder="Search Employee..."
          value={search}
          onChange={(e) =>
            setSearch(e.target.value)
          }
          className="border p-2 rounded w-full md:w-1/3"
        />

      </div>

      {/* Table */}

      <div className="bg-white rounded-lg shadow overflow-x-auto">

        <table className="w-full">

          <thead className="bg-gray-100">
            <tr>
              <th className="p-3 text-left">
                Employee
              </th>
              <th className="p-3 text-left">
                Department
              </th>
              <th className="p-3 text-left">
                Date
              </th>
              <th className="p-3 text-left">
                Status
              </th>
              <th className="p-3 text-center">
                Action
              </th>
            </tr>
          </thead>

          <tbody>

            {filteredAttendance.length > 0 ? (
              filteredAttendance.map((item) => (
                <tr
                  key={item.id}
                  className="border-t hover:bg-gray-50"
                >
                  <td className="p-3">
                    {item.employee}
                  </td>

                  <td className="p-3">
                    {item.department}
                  </td>

                  <td className="p-3">
                    {item.date}
                  </td>

                  <td className="p-3">

                    <span
                      className={`px-3 py-1 rounded-full text-sm
                      ${
                        item.status === "Present"
                          ? "bg-green-100 text-green-700"
                          : item.status === "Absent"
                          ? "bg-red-100 text-red-700"
                          : item.status === "Leave"
                          ? "bg-yellow-100 text-yellow-700"
                          : "bg-blue-100 text-blue-700"
                      }`}
                    >
                      {item.status}
                    </span>

                  </td>

                  <td className="p-3 text-center">

                    <button
                      onClick={() =>
                        deleteAttendance(item.id)
                      }
                      className="bg-red-600 text-white px-3 py-1 rounded"
                    >
                      Delete
                    </button>

                  </td>
                </tr>
              ))
            ) : (
              <tr>
                <td
                  colSpan="5"
                  className="text-center p-6 text-gray-500"
                >
                  No Attendance Found
                </td>
              </tr>
            )}

          </tbody>

        </table>

      </div>

    </div>
  );
};

export default StaffAttendance;