import React, { useState } from "react";

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

  const [formData, setFormData] = useState({
    leaveName: "",
    leaveCode: "",
    totalDays: "",
    category: "Paid",
    description: "",
  });

  const [leaveTypes, setLeaveTypes] = useState([
    {
      id: 1,
      leaveName: "Casual Leave",
      leaveCode: "CL",
      totalDays: 12,
      category: "Paid",
      description: "General short-term leave",
    },
    {
      id: 2,
      leaveName: "Sick Leave",
      leaveCode: "SL",
      totalDays: 10,
      category: "Paid",
      description: "Medical leave",
    },
  ]);

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

  const addLeaveType = () => {
    if (
      !formData.leaveName ||
      !formData.leaveCode ||
      !formData.totalDays
    ) {
      alert("Please fill all required fields");
      return;
    }

    setLeaveTypes([
      ...leaveTypes,
      {
        id: Date.now(),
        ...formData,
      },
    ]);

    setFormData({
      leaveName: "",
      leaveCode: "",
      totalDays: "",
      category: "Paid",
      description: "",
    });
  };

  const deleteLeaveType = (id) => {
    setLeaveTypes(
      leaveTypes.filter((item) => item.id !== id)
    );
  };

  const filteredData = leaveTypes.filter(
    (item) =>
      item.leaveName
        .toLowerCase()
        .includes(search.toLowerCase()) ||
      item.leaveCode
        .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">
          Leave Type Management
        </h1>

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

      </div>

      {/* Form */}
      <div className="bg-white p-6 rounded-lg shadow mb-6">

        <h2 className="text-lg font-semibold mb-4">
          Add Leave Type
        </h2>

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

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

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

          <input
            type="number"
            name="totalDays"
            placeholder="Allowed Days"
            value={formData.totalDays}
            onChange={handleChange}
            className="border p-2 rounded"
          />

          <select
            name="category"
            value={formData.category}
            onChange={handleChange}
            className="border p-2 rounded"
          >
            <option value="Paid">
              Paid Leave
            </option>

            <option value="Unpaid">
              Unpaid Leave
            </option>
          </select>

        </div>

        <textarea
          rows="4"
          name="description"
          placeholder="Description"
          value={formData.description}
          onChange={handleChange}
          className="border p-2 rounded w-full mt-4"
        />

        <button
          onClick={addLeaveType}
          className="bg-blue-600 text-white px-5 py-2 rounded mt-4"
        >
          Add Leave Type
        </button>

      </div>

      {/* Search */}
      <div className="bg-white p-4 rounded-lg shadow mb-6">

        <input
          type="text"
          placeholder="Search Leave Type..."
          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">
                Leave Name
              </th>

              <th className="p-3 text-left">
                Code
              </th>

              <th className="p-3 text-left">
                Allowed Days
              </th>

              <th className="p-3 text-left">
                Category
              </th>

              <th className="p-3 text-left">
                Description
              </th>

              <th className="p-3 text-center">
                Action
              </th>
            </tr>
          </thead>

          <tbody>

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

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

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

                  <td className="p-3">
                    <span
                      className={`px-3 py-1 rounded-full text-sm ${
                        item.category === "Paid"
                          ? "bg-green-100 text-green-700"
                          : "bg-red-100 text-red-700"
                      }`}
                    >
                      {item.category}
                    </span>
                  </td>

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

                  <td className="p-3 text-center">
                    <button
                      onClick={() =>
                        deleteLeaveType(item.id)
                      }
                      className="bg-red-600 text-white px-3 py-1 rounded"
                    >
                      Delete
                    </button>
                  </td>
                </tr>
              ))
            ) : (
              <tr>
                <td
                  colSpan="6"
                  className="text-center p-6 text-gray-500"
                >
                  No Leave Types Found
                </td>
              </tr>
            )}

          </tbody>

        </table>

      </div>
    </div>
  );
};

export default LeaveType;