import MemberLayout from '@/layouts/MemberLayout';
import { Invoice, Member, Vehicle, Voucher } from '@/types';
import { useState } from 'react';
import { Ticket } from 'lucide-react';
import { Link } from '@inertiajs/react';
import { sign_out } from '@/actions/App/Http/Controllers/MembershipController';


interface Props {
    member: Member;
}

export default function MemberPage({ member }: Props) {

    const NAVIGATIONS = [
        { label: "My Invoices", index: 0 },
        { label: "My Vouchers", index: 1 },
        { label: "My Vehicle", index: 2 },
    ];

    const [tab, setTab] = useState(0);

    function handleTabChange(index: number) {
        setTab(index);
    }

    return (
        <MemberLayout
            header={<HeaderMember member={member} contentTitle={NAVIGATIONS[tab].label} />}
            tab_bar={<TabBar onChange={handleTabChange} tab={tab} navigations={NAVIGATIONS} />}
        >
            {tab === 0 && (
                <div className="px-[1em] pt-[1em] pb-[3em] flex flex-col">
                    {member.invoices?.map((invoice: Invoice) => (
                        <InvoiceCard key={invoice.sellout_number} invoice={invoice} />
                    ))}
                </div>)}
            {tab === 1 && (
                <div className="px-[1em] pt-[1em] pb-[3em] flex flex-col flex-wrap gap-[.6em] md:grid md:grid-cols-2">
                    {member
                        .vouchers?.filter((voucher: Voucher) => voucher.voucher_component === 'voucher-cashback')
                        .map((voucher: Voucher) => (
                            <VoucherCashbackCard key={voucher.voucher_code} voucher={voucher} />
                        ))}
                    {member
                        .vouchers?.filter((voucher: Voucher) => voucher.voucher_component === 'voucher-discount')
                        .map((voucher: Voucher) => (
                            <VoucherDiscountCard key={voucher.voucher_code} voucher={voucher} />
                        ))}
                </div>)
            }
            {tab === 2 && (
                <div className="px-[1.6em] flex flex-col">
                    {member.vehicles?.map((vehicle: Vehicle) => (
                        <VehicleItem key={vehicle.plate_number} vehicle={vehicle} />
                    ))}
                </div>)
            }
        </MemberLayout>
    );
}

function VehicleItem({ vehicle }: { vehicle: Vehicle }) {
    return (
        <div className="flex flex-row flex-none gap-2 py-[1em] border-t border-black/20 first:border-none">
            <div className="flex-none w-[5em]">
                <div className="place-items-start grid h-full">
                    <p className="w-full font-mono text-[1.2em] text-left">{vehicle.size}</p>
                </div>
            </div>
            <div className="flex-1">
                <p className="font-display text-[1.2em]">
                    {vehicle.brand} - {vehicle.type}
                </p>
                <div className="bg-[#CE2026] px-[.6em] py-[.1em] rounded-md w-fit font-mono font-bold text-[.8em] text-white uppercase tracking-widest">
                    {vehicle.plate_number}
                </div>
            </div>
        </div>
    );
}

function VoucherCashbackCard({ voucher }: { voucher: Voucher }) {
    return (
        <div className="relative pb-[1em] bg-white border-[.1em] border-black/20 rounded-[1em] shadow-lg overflow-clip">
            <div className="bg-[#CE2026] px-[1em] pt-[0.8em] pb-[0.6em] border-b text-white text-center">
                <div className="flex justify-between">
                    <div className="flex flex-col text-left">
                        <p className="">Voucher Code</p>
                        <strong className="font-mono">{voucher.voucher_code}</strong>
                        <p className="">Valid until: <strong>{voucher.voucher_expire_at}</strong></p>
                    </div>
                    <div className="flex flex-row gap-1">
                        <strong>{voucher.voucher_qty - voucher.voucher_redeem_count} /  {voucher.voucher_qty}</strong>x<Ticket />
                    </div>
                </div>
            </div>
            <div className="flex flex-col gap-4 px-[1em] pt-[1em] text-black">
                <strong className="text-[1.4em] leading-none">{voucher.voucher_name}</strong>

                <div className="flex flex-col">
                    <p className="font-display font-medium text-[1em] uppercase">Cashback</p>
                    <strong className="text-[2.8em] leading-none">{voucher.voucher_cashback}</strong>
                </div>
            </div>
        </div>
    );
}

function VoucherDiscountCard({ voucher }: { voucher: Voucher }) {
    return (
        <div className="relative pb-[1em] bg-white border-[.1em] border-black/20 rounded-[1em] shadow-lg overflow-clip">
            <div className="bg-[#CE2026] px-[1em] pt-[0.8em] pb-[0.6em] border-b text-white text-center">
                <div className="flex justify-between">
                    <div className="flex flex-col text-left">
                        <p className="">Voucher Code</p>
                        <strong className="font-mono">{voucher.voucher_code}</strong>
                        <p className="">Valid until: <strong>{voucher.voucher_expire_at}</strong></p>
                    </div>
                    <div className="flex flex-row gap-1">
                        <strong>{voucher.voucher_qty - voucher.voucher_redeem_count} / {voucher.voucher_qty}</strong>x<Ticket />
                    </div>
                </div>
            </div>
            <div className="flex flex-col gap-4 px-[1em] pt-[1em] text-black">
                <strong className="text-[1.4em] leading-none">{voucher.voucher_name}</strong>

                <div className="flex flex-col">
                    <p className="font-display font-medium text-[1em] uppercase">Discount</p>
                    <strong className="text-[2.8em] leading-none">{voucher.voucher_discount} %</strong>
                    <p className="font-display font-medium text-[1em] uppercase">Max: Rp. {voucher.voucher_max_cashback}</p>
                </div>
            </div>
        </div>
    );
}

function InvoiceCard({ invoice }: { invoice: Invoice }) {
    const [showInvoiceDetail, setShowInvoiceDetail] = useState(false);

    function handleInvoiceClick() {
        setShowInvoiceDetail(!showInvoiceDetail);
    }

    return (<>
        <div className="overflow-clip bg-background border-[.1em] border-black/20 rounded-[1em] shadow-lg mb-[1em]">
            <div className="bg-[#ce2026] text-white p-[.5em] px-[1em]" onClick={handleInvoiceClick}>
                <p className="font-display font-bold text-[1em] uppercase">E-Invoice SCT - <span className="font-mono">{invoice.sellout_number}</span></p>
            </div>
            <div className='flex flex-col gap-2 mb-[1em] px-[1em]' style={{ display: showInvoiceDetail ? 'flex' : 'none' }}>
                <p className="font-display font-bold text-[.8em] mt-[1em] opacity-50 uppercase">Invoice Items</p>
                <div className="flex flex-col">

                    {invoice.items.map((item: any, index: number) => (
                        <div className="grid grid-cols-2 gap-2 p-[1em] border-b border-black/20" key={index}>
                            <div className="flex-1">
                                <p className="font-display font-medium text-[.8em] uppercase">Nama item</p>
                                <p className="font-display font-bold text-[1em] ">{item.name}</p>
                            </div>
                            <div className="flex-1">
                                <p className="font-display font-medium text-[.8em] uppercase">Jumlah</p>
                                <p className="font-display font-bold text-[1em] ">{item.quantity}</p>
                            </div>
                            <div className="flex-1">
                                <p className="font-display font-medium text-[.8em] uppercase">Harga</p>
                                <p className="font-display font-bold text-[1em] ">{item.price}</p>
                            </div>
                            <div className="flex-1">
                                <p className="font-display font-medium text-[.8em] uppercase">Harga total</p>
                                <p className="font-display font-bold text-[1em] ">{item.total}</p>
                            </div>
                        </div>
                    ))}
                </div>
                <p className="font-display font-bold text-[.8em] mt-[1em] opacity-50 uppercase">Invoice Detail</p>
                <div className="flex flex-row gap-2">
                    <div className="flex-1">
                        <p className="font-display font-medium text-[.8em] uppercase">Outlet</p>
                        <p className="font-display font-bold text-[1em] ">{(invoice.outlet_name + ' - ' + invoice.region_name)}</p>
                    </div>
                    <div className="flex-1">
                        <p className="font-display font-medium text-[.8em] uppercase">Tanggal transaksi</p>
                        <p className="font-display font-bold text-[1em] ">{invoice.sellout_date}</p>
                    </div>
                </div>
                <div className="flex flex-row gap-2">
                    <div className="flex-1">
                        <p className="font-display font-medium text-[.8em] uppercase">Nomor transaksi</p>
                        <p className="font-display font-bold text-[1em] ">{invoice.sellout_number}</p>
                    </div>
                    <div className="flex-1">
                        <p className="font-display font-medium text-[.8em] uppercase">Total transaksi</p>
                        <p className="font-display font-bold text-[1em] ">{invoice.grand_total}</p>
                    </div>
                </div>
                <div className="flex flex-row gap-2">
                    <div className="flex-1">
                        <p className="font-display font-medium text-[.8em] uppercase">Metode pembayaran</p>
                        <p className="font-display font-bold text-[1em] ">{invoice.payment_method}</p>
                    </div>
                    <div className="flex-1">
                        <p className="font-display font-medium text-[.8em] uppercase">Nama pelanggan</p>
                        <p className="font-display font-bold text-[1em] ">{invoice.customer_name}</p>
                    </div>
                </div>
                <div className="flex flex-row gap-2">
                    <div className="flex-1">
                        <p className="font-display font-medium text-[.8em] uppercase">Nomor telepon</p>
                        <p className="font-display font-bold text-[1em] ">{invoice.customer_phone}</p>
                    </div>
                    <div className="flex-1">
                        <p className="font-display font-medium text-[.8em] uppercase">Merek kendaraan</p>
                        <p className="font-display font-bold text-[1em] ">{invoice.vehicle_brand_name}</p>
                    </div>
                </div>
                <div className="flex flex-row gap-2">
                    <div className="flex-1">
                        <p className="font-display font-medium text-[.8em] uppercase">Tipe kendaraan</p>
                        <p className="font-display font-bold text-[1em] ">{invoice.vehicle_type_name}</p>
                    </div>
                    <div className="flex-1">
                        <p className="font-display font-medium text-[.8em] uppercase">Nomor kendaraan</p>
                        <p className="font-display font-bold text-[1em] ">{invoice.vehicle_plate_number}</p>
                    </div>
                </div>

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



function TabItem({ menuName, isActive, onClick }: { menuName: string, isActive: boolean, onClick: () => void }) {
    return (
        <div className="flex-none place-items-center grid cursor-pointer border-t border-black/20" onClick={onClick}>
            <div className={`flex flex-col rounded-full items-center px-2 py-1  ${isActive ? 'bg-[#ce2026] text-white' : 'bg-white text-black'}`}>
                <p className="font-display font-bold text-[.8em] uppercase">{menuName}</p>
            </div>
        </div>
    );
}

function HeaderMember({ member, contentTitle }: { member: Member, contentTitle: string }) {
    const logoImageSrc = "https://static.wixstatic.com/media/80e941_ff223148ff3a4b76b489debeaa79189d~mv2.png/v1/crop/x_0,y_730,w_7087,h_3457/fill/w_450,h_210,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/Logo%20SCT%20Motodetailing%20Red.png";

    return (
        <div className="flex-none h-full">
            <div className="flex justify-between items-center px-[1.6em] py-[1em] mt-[1em]">
                <img src={logoImageSrc} alt="" className="h-[3em]" />
                <div className="flex gap-2 font-medium text-[#ce2026] cursor-pointer">
                    <Link href={sign_out()} method='post' className="underline lowercase">Sign Out</Link>
                </div>
            </div>
            <div className="px-[1.6em] pb-[1.4em] mt-[1em]">
                <p className="font-medium text-[1.2em] text-gray leading-none">Welcome {member.customer_name}</p>
                <p className="font-display font-medium text-[2.6em] text-gray leading-none">{contentTitle}</p>
            </div>
        </div>
    );
}

function TabBar({ onChange, tab, navigations }: { onChange: (index: number) => void, tab: number, navigations: { label: string, index: number }[] }) {

    return (
        <div className="flex-none h-full flex flex-row px-[1.6em] gap-2">
            {navigations.map((navigation: { label: string, index: number }) => (
                <TabItem key={navigation.index} menuName={navigation.label} isActive={tab === navigation.index} onClick={() => onChange(navigation.index)} />
            ))}
        </div>
    );
}

