"use client";

import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select";
import { Textarea } from "@/components/ui/textarea";
import { cn } from "@/lib/utils";
import {
  ArrowDownUp,
  CalendarDays,
  ChevronDown,
  ChevronUp,
  ListFilter,
  MoreVertical,
  Search,
  ShoppingBag,
  X,
} from "lucide-react";

type ExpenseRow = {
  id: string;
  date: string;
  warehouse: string;
  category: string;
  amount: string;
  details: string;
};

const EXPENSE_ROWS: ExpenseRow[] = [
  {
    id: "1",
    date: "02/15/2024",
    warehouse: "Warehouse 002",
    category: "Gadgets",
    amount: "12",
    details: "iPhone Pro Max 14",
  },
  {
    id: "2",
    date: "02/13/2024",
    warehouse: "Warehouse 008",
    category: "Home Accesories",
    amount: "7",
    details: "Oven",
  },
  {
    id: "3",
    date: "02/10/2024",
    warehouse: "Warehouse 005",
    category: "Office Accesories",
    amount: "9",
    details: "Printer",
  },
  {
    id: "4",
    date: "02/09/2024",
    warehouse: "Warehouse 003",
    category: "Gadgets",
    amount: "15",
    details: "Smart Watch",
  },
  {
    id: "5",
    date: "02/08/2024",
    warehouse: "Warehouse 007",
    category: "Home Accesories",
    amount: "5",
    details: "Micro Wave",
  },
  {
    id: "6",
    date: "02/08/2024",
    warehouse: "Warehouse 009",
    category: "Office Accesories",
    amount: "10",
    details: "Keyboard",
  },
  {
    id: "7",
    date: "02/06/2024",
    warehouse: "Warehouse 006",
    category: "Home Accesories",
    amount: "13",
    details: "Hair Dryer",
  },
];

export default function ExpensesPageBody() {
  const hasData = EXPENSE_ROWS.length > 0;
  const [showCreateModal, setShowCreateModal] = useState(false);
  const primaryButtonClass =
    "h-9 rounded-lg bg-[#003E6B] px-5 text-xs font-semibold text-white shadow-none hover:bg-[#003256] md:h-10 md:px-6";

  return (
    <div className="min-h-0 space-y-6 bg-[#F8F9FA] p-5 md:p-6">
      <div className="flex flex-wrap items-center justify-between gap-3">
        <h1 className="text-xl font-semibold leading-[1.35] text-[#45464E]">Expense List</h1>
        <Button type="button" className={cn(primaryButtonClass, "shrink-0")} onClick={() => setShowCreateModal(true)}>
          Create Expenses
        </Button>
      </div>

      <section className="rounded-xl border border-[#F1F3F9] bg-white p-5 shadow-[0_1px_2px_rgba(16,24,40,0.04)] md:p-6">
        <div className="flex flex-col gap-4">
          <div
            className={cn(
              "flex flex-col gap-4 md:flex-row md:items-center md:justify-between",
              !hasData && "mb-4"
            )}
          >
            <h2
              className={cn(
                "text-base font-medium md:text-lg",
                hasData ? "font-semibold text-[#1C1D22]" : "text-[#ABAFB1]"
              )}
            >
              Created Expense List
            </h2>
            <div className="flex flex-wrap items-center gap-2">
              <div
                className={cn(
                  "flex h-10 w-full items-center gap-2 rounded-md border px-3 md:w-[260px]",
                  hasData ? "border-[#F1F3F9]" : "border-[#F1F3F9] bg-[#FAFBFC]"
                )}
              >
                <Search
                  className={cn("size-4 shrink-0", hasData ? "text-[#ABAFB1]" : "text-[#CFD3D4]")}
                  aria-hidden
                />
                <Input
                  type="search"
                  placeholder="Search"
                  disabled={!hasData}
                  className={cn(
                    "h-full border-0 p-0 text-[11px] focus-visible:ring-0",
                    hasData
                      ? "text-[#45464E] placeholder:text-[#CFD3D4]"
                      : "cursor-not-allowed bg-transparent text-[#CFD3D4] placeholder:text-[#E1E2E9]"
                  )}
                  aria-label="Search expenses"
                />
              </div>
              <Button
                type="button"
                variant="outline"
                className={cn(
                  "h-10 shrink-0 rounded-md border px-3 text-[11px] font-medium shadow-none",
                  hasData
                    ? "border-[#F1F3F9] text-[#667085] hover:bg-[#F5F7FA]"
                    : "border-[#F1F3F9] bg-[#FAFBFC] text-[#CFD3D4]"
                )}
                aria-label="Filter expenses"
              >
                <ListFilter className="mr-1.5 size-4" aria-hidden />
                Filters
              </Button>
              <Button
                type="button"
                variant="outline"
                className={cn(
                  "h-10 shrink-0 rounded-md border px-3 text-[11px] font-medium shadow-none",
                  hasData
                    ? "border-[#F1F3F9] text-[#667085] hover:bg-[#F5F7FA]"
                    : "border-[#F1F3F9] bg-[#FAFBFC] text-[#CFD3D4]"
                )}
                aria-label="Sort expenses"
              >
                <ArrowDownUp className="mr-1.5 size-4" aria-hidden />
                Filters
              </Button>
            </div>
          </div>

          {hasData ? (
            <>
              <div className="overflow-x-auto rounded-md border border-[#F1F3F9]">
                <table className="w-full min-w-[930px]">
                  <thead>
                    <tr className="border-b border-[#F1F3F9] bg-white">
                      <th className="w-10 px-2 py-3 text-left">
                        <input type="checkbox" className="size-3.5 rounded border border-[#B9C0D4]" aria-label="Select all expenses" />
                      </th>
                      <th className="px-2 py-3 text-left text-[10px] font-semibold uppercase tracking-wide text-[#667085]">Date</th>
                      <th className="px-2 py-3 text-left text-[10px] font-semibold uppercase tracking-wide text-[#667085]">Warehouse</th>
                      <th className="px-2 py-3 text-left text-[10px] font-semibold uppercase tracking-wide text-[#667085]">Category</th>
                      <th className="px-2 py-3 text-left text-[10px] font-semibold uppercase tracking-wide text-[#667085]">Amount</th>
                      <th className="px-2 py-3 text-left text-[10px] font-semibold uppercase tracking-wide text-[#667085]">Details</th>
                      <th className="w-20 px-2 py-3 text-left text-[10px] font-semibold uppercase tracking-wide text-[#667085]">Action</th>
                    </tr>
                  </thead>
                  <tbody>
                    {EXPENSE_ROWS.map((row) => (
                      <tr key={row.id} className="border-b border-[#F7F8FB] last:border-b-0">
                        <td className="px-2 py-3">
                          <input type="checkbox" className="size-3.5 rounded border border-[#B9C0D4]" aria-label={`Select expense ${row.id}`} />
                        </td>
                        <td className="px-2 py-3 text-[13px] text-[#1C1D22]">{row.date}</td>
                        <td className="px-2 py-3 text-[13px] text-[#1C1D22]">{row.warehouse}</td>
                        <td className="px-2 py-3 text-[13px] text-[#1C1D22]">{row.category}</td>
                        <td className="px-2 py-3 text-[13px] text-[#1C1D22]">{row.amount}</td>
                        <td className="px-2 py-3 text-[13px] text-[#1C1D22]">{row.details}</td>
                        <td className="px-2 py-3">
                          <button
                            type="button"
                            className="inline-flex items-center justify-center rounded p-1 text-[#1C1D22] hover:bg-[#F5F7FA]"
                            aria-label="More actions"
                          >
                            <MoreVertical className="size-3.5" aria-hidden />
                          </button>
                        </td>
                      </tr>
                    ))}
                  </tbody>
                </table>
              </div>
              <div className="mt-3 flex items-center justify-between">
                <Button
                  type="button"
                  variant="outline"
                  className="h-8 rounded-md border-[#E5E7EB] px-4 text-[10px] font-medium text-[#8B8D97]"
                >
                  Previous
                </Button>
                <p className="text-[10px] text-[#CFD3D4]">page 1 of 10</p>
                <Button
                  type="button"
                  variant="outline"
                  className="h-8 rounded-md border-[#E5E7EB] px-4 text-[10px] font-medium text-[#8B8D97]"
                >
                  Next
                </Button>
              </div>
            </>
          ) : (
            <div className="flex min-h-[360px] flex-col items-center justify-center gap-5 py-12">
              <div className="flex size-[112px] items-center justify-center rounded-full border border-[#E1E2E9] bg-[#EEF2F6]">
                <ShoppingBag className="size-11 text-[#98A2B3]" strokeWidth={1.35} aria-hidden />
              </div>
              <div className="max-w-md space-y-1.5 text-center">
                <p className="text-[22px] font-bold leading-[1.35] text-[#1C1D22]">No Expense List Yet?</p>
                <p className="text-sm font-normal leading-relaxed text-[#8B8D97]">Add More Expense</p>
              </div>
              <Button type="button" className={cn(primaryButtonClass, "h-10 px-7")} onClick={() => setShowCreateModal(true)}>
                Create Expenses
              </Button>
            </div>
          )}
        </div>
      </section>

      {showCreateModal ? (
        <div className="fixed inset-0 z-[70] grid place-items-center bg-[#10182880] p-4">
          <section className="w-full max-w-[860px] rounded-xl bg-white p-5 shadow-[0_20px_45px_rgba(16,24,40,0.22)] md:p-6">
            <div className="mb-6 flex items-center justify-between gap-3">
              <h2 className="text-[22px] font-semibold text-[#101828]">Create Expenses</h2>
              <Button
                type="button"
                variant="outline"
                size="icon"
                onClick={() => setShowCreateModal(false)}
                className="size-8 rounded-lg border-0 bg-[#EEF4FA] text-[#667085] shadow-none hover:bg-[#E5EDF6]"
              >
                <X className="size-4" />
              </Button>
            </div>

            <form className="space-y-4">
              <div className="grid gap-4 md:grid-cols-2">
                <div className="relative">
                  <Input
                    type="text"
                    defaultValue="12/12/2020"
                    className="h-10 rounded-md border-0 bg-[#F6F8FB] px-3 pr-10 text-[13px] text-[#344054] shadow-none placeholder:text-[#98A2B3] focus-visible:ring-1 focus-visible:ring-[#003E6B]/25"
                  />
                  <CalendarDays className="pointer-events-none absolute right-3 top-1/2 size-4 -translate-y-1/2 text-[#667085]" />
                </div>

                <Select defaultValue="w-2">
                  <SelectTrigger className="h-10 rounded-md border-0 bg-[#F6F8FB] px-3 text-[13px] text-[#344054] shadow-none focus:ring-1 focus:ring-[#003E6B]/25">
                    <SelectValue placeholder="Select Warehouse" />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem value="w-2">Warehouse 002</SelectItem>
                    <SelectItem value="w-3">Warehouse 003</SelectItem>
                    <SelectItem value="w-8">Warehouse 008</SelectItem>
                  </SelectContent>
                </Select>
              </div>

              <div className="grid gap-4 md:grid-cols-2">
                <Select defaultValue="gadgets">
                  <SelectTrigger className="h-10 rounded-md border-0 bg-[#F6F8FB] px-3 text-[13px] text-[#344054] shadow-none focus:ring-1 focus:ring-[#003E6B]/25">
                    <SelectValue placeholder="Select Category" />
                  </SelectTrigger>
                  <SelectContent>
                    <SelectItem value="gadgets">Gadgets</SelectItem>
                    <SelectItem value="home">Home Accesories</SelectItem>
                    <SelectItem value="office">Office Accesories</SelectItem>
                  </SelectContent>
                </Select>

                <div className="grid grid-cols-[1fr_44px_28px] overflow-hidden rounded-md">
                  <Input
                    type="number"
                    defaultValue={0}
                    className="h-10 rounded-none rounded-l-md border-0 bg-[#F6F8FB] px-3 text-[13px] text-[#344054] shadow-none [appearance:textfield] focus-visible:ring-1 focus-visible:ring-[#003E6B]/25 [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
                  />
                  <div className="grid place-items-center border-l border-[#E4E7EC] bg-[#EEF2F6] text-[#667085]">$</div>
                  <div className="grid border-l border-[#E4E7EC] bg-[#EEF2F6]">
                    <button
                      type="button"
                      className="grid place-items-center border-b border-[#E4E7EC] text-[#667085] hover:bg-[#E5EBF2]"
                      aria-label="Increase amount"
                    >
                      <ChevronUp className="size-3.5" />
                    </button>
                    <button
                      type="button"
                      className="grid place-items-center text-[#667085] hover:bg-[#E5EBF2]"
                      aria-label="Decrease amount"
                    >
                      <ChevronDown className="size-3.5" />
                    </button>
                  </div>
                </div>
              </div>

              <Textarea
                rows={5}
                placeholder="Details"
                className="resize-none rounded-md border-0 bg-[#F6F8FB] px-3 py-3 text-[13px] text-[#344054] placeholder:text-[#98A2B3] focus-visible:ring-1 focus-visible:ring-[#003E6B]/25"
              />

              <div className="flex items-center justify-center gap-4 pt-1">
                <Button
                  type="button"
                  onClick={() => setShowCreateModal(false)}
                  className="h-9 min-w-[104px] rounded-md bg-[#F79009] px-4 text-xs font-semibold text-white shadow-none hover:bg-[#dc7b06]"
                >
                  Cancel
                </Button>
                <Button
                  type="button"
                  className="h-9 min-w-[112px] rounded-md bg-[#003E6B] px-4 text-xs font-semibold text-white shadow-none hover:bg-[#003256]"
                >
                  Create Expense
                </Button>
              </div>
            </form>
          </section>
        </div>
      ) : null}
    </div>
  );
}
