import { useState, useEffect } from "react";
import { Plus, Package, Search, RotateCcw, ChevronDown, ChevronUp } from 'lucide-react';
import { Button } from "@/Components/ui/button";
import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout";
import { Head, router, Link } from "@inertiajs/react";
import Pagination from "@/Components/shared/Pagination";
import { Can } from "@/Components/Permission/Can";
import { SearchableSelect } from "@/Components/ui/searchable-select";
import { Input } from "@/Components/ui/input";
import { Label } from "@/Components/ui/label";
import {
    Table,
    TableBody,
    TableCell,
    TableHead,
    TableHeader,
    TableRow,
} from "@/Components/ui/table";
import { Badge } from "@/Components/ui/badge";

interface Props {
    orders: {
        data: any[];
        links: any[];
    };
    filters: {
        search?: string;
        status?: string;
    };
    warehouses: { id: number; name: string }[];
}

export default function ShippingOrderIndex({ orders, filters, warehouses }: Props) {
    const [search, setSearch] = useState(filters.search || "");
    const [status, setStatus] = useState<string>(filters.status || "all");

    const handleFilter = () => {
        router.get(
            route('delivery-notes.index'),
            {
                search,
                status: status === 'all' ? undefined : status,
            },
            { preserveState: true, replace: true }
        );
    };

    const handleReset = () => {
        setSearch("");
        setStatus("all");
        router.get(route('delivery-notes.index'));
    };

    const getStatusBadge = (status: string) => {
        switch (status) {
            case 'draft':
                return <Badge variant="secondary">草稿</Badge>;
            case 'completed':
                return <Badge className="bg-green-100 text-green-800">已過帳</Badge>;
            case 'cancelled':
                return <Badge variant="destructive">已取消</Badge>;
            default:
                return <Badge>{status}</Badge>;
        }
    };

    return (
        <AuthenticatedLayout breadcrumbs={[
            { label: '供應鏈管理', href: '#' },
            { label: '出貨單管理', href: route('delivery-notes.index'), isPage: true }
        ] as any}>
            <Head title="出貨單管理" />
            <div className="container mx-auto p-6 max-w-7xl">
                <div className="flex items-center justify-between mb-6">
                    <div>
                        <h1 className="text-2xl font-bold text-grey-0 flex items-center gap-2">
                            <Package className="h-6 w-6 text-primary-main" />
                            出貨單管理
                        </h1>
                        <p className="text-gray-500 mt-1">
                            建立出貨單並執行過帳扣庫存
                        </p>
                    </div>
                    <div className="flex gap-2">
                        <Can permission="delivery_notes.create">
                            <Button
                                onClick={() => router.get(route('delivery-notes.create'))}
                                className="gap-2 button-filled-primary"
                            >
                                <Plus className="h-4 w-4" />
                                建立出貨單
                            </Button>
                        </Can>
                    </div>
                </div>

                <div className="bg-white p-5 rounded-lg shadow-sm border border-gray-200 mb-6">
                    <div className="grid grid-cols-1 md:grid-cols-12 gap-4">
                        <div className="md:col-span-6 space-y-1">
                            <Label className="text-xs font-medium text-grey-1">關鍵字搜尋</Label>
                            <div className="relative">
                                <Search className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 h-4 w-4" />
                                <Input
                                    placeholder="搜尋單號、客戶名稱..."
                                    value={search}
                                    onChange={(e) => setSearch(e.target.value)}
                                    className="pl-10 h-9 block"
                                    onKeyDown={(e) => e.key === 'Enter' && handleFilter()}
                                />
                            </div>
                        </div>

                        <div className="md:col-span-4 space-y-1">
                            <Label className="text-xs font-medium text-grey-1">單據狀態</Label>
                            <SearchableSelect
                                value={status}
                                onValueChange={setStatus}
                                options={[
                                    { label: "全部狀態", value: "all" },
                                    { label: "草稿", value: "draft" },
                                    { label: "已過帳", value: "completed" },
                                    { label: "已取消", value: "cancelled" },
                                ]}
                                className="h-9"
                                showSearch={false}
                            />
                        </div>

                        <div className="md:col-span-2 flex items-end gap-2">
                            <Button
                                variant="outline"
                                onClick={handleReset}
                                className="flex-1 h-9"
                            >
                                <RotateCcw className="h-4 w-4" />
                            </Button>
                            <Button
                                onClick={handleFilter}
                                className="flex-[2] button-filled-primary h-9"
                            >
                                <Search className="h-4 w-4 mr-2" />
                                查詢
                            </Button>
                        </div>
                    </div>
                </div>

                <div className="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                    <Table>
                        <TableHeader>
                            <TableRow className="bg-gray-50 hover:bg-gray-50">
                                <TableHead className="w-[180px]">出貨單號</TableHead>
                                <TableHead>客戶名稱</TableHead>
                                <TableHead>倉庫</TableHead>
                                <TableHead>出貨日期</TableHead>
                                <TableHead>總金額</TableHead>
                                <TableHead>狀態</TableHead>
                                <TableHead>建立者</TableHead>
                                <TableHead className="text-right">操作</TableHead>
                            </TableRow>
                        </TableHeader>
                        <TableBody>
                            {orders.data.length > 0 ? (
                                orders.data.map((order) => (
                                    <TableRow key={order.id}>
                                        <TableCell className="font-medium text-primary-main">
                                            <Link href={route('delivery-notes.show', order.id)}>
                                                {order.doc_no}
                                            </Link>
                                        </TableCell>
                                        <TableCell>{order.customer_name || '-'}</TableCell>
                                        <TableCell>{order.warehouse_name}</TableCell>
                                        <TableCell>{order.shipping_date}</TableCell>
                                        <TableCell>${Number(order.grand_total).toLocaleString()}</TableCell>
                                        <TableCell>{getStatusBadge(order.status)}</TableCell>
                                        <TableCell>{order.creator_name}</TableCell>
                                        <TableCell className="text-right">
                                            <Button
                                                variant="ghost"
                                                size="sm"
                                                asChild
                                            >
                                                <Link href={route('delivery-notes.show', order.id)}>
                                                    查看
                                                </Link>
                                            </Button>
                                        </TableCell>
                                    </TableRow>
                                ))
                            ) : (
                                <TableRow>
                                    <TableCell colSpan={8} className="h-24 text-center text-gray-500">
                                        尚無出貨單資料
                                    </TableCell>
                                </TableRow>
                            )}
                        </TableBody>
                    </Table>
                </div>

                <div className="mt-4 flex justify-end">
                    <Pagination links={orders.links} />
                </div>
            </div>
        </AuthenticatedLayout>
    );
}
