import { React, useState, useContext, useEffect } from "react";

import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout";
import { Button } from "primereact/button";
import { useRef } from "react";
import axios from "axios";
import { Toast } from "primereact/toast";
import { authUser } from "@/Components/AuthProvider";
import { Calendar } from "primereact/calendar";
import { renderPDF } from "@/Utils/helper";
import { Dropdown } from "primereact/dropdown";

function WareHouseTransfer({ auth, warehouseList }) {
    const { permission } = useContext(authUser);
    const [startDate, setStartDate] = useState();
    const [endDate, setEndDate] = useState();
    const [formWarehouse, setFormWarehouse] = useState([]);
    const [toWarehouse, setToWarehouse] = useState([]);
    const [isLoading, setIsLoading] = useState(false);
    const toast = useRef(null);

    useEffect(() => {
        if (!permission.includes("report_super_admin_access")) {
            setFormWarehouse(auth.user.shop_id);
        }
    }, []);
    const search = async () => {
        setIsLoading(true);
        let form = new FormData();
        form.append("start", startDate);
        form.append("end", endDate);
        form.append("from_warehouse_id", formWarehouse);
        form.append("to_warehouse_id", toWarehouse);

        let res = await axios.post("warehouse-transfer-report-search", form);
        return res.data;
    };

    const openInNewTab = async () => {
        const data = await search();
        const pdfDoc = await window.generatePDFView(
            data,
            window.reportModes.warehouseTransfer,
            false,
            "Warehouse Stock Movement",
            startDate,
            endDate
        );
        renderPDF(pdfDoc, "WareHouseTransfer.pdf");
        setIsLoading(false);
    };

    return (
        <AuthenticatedLayout
            user={auth.user}
            header={
                <>
                    <h2 className="text-skin-header font-medium ">
                        WareHouse Transfer
                    </h2>{" "}
                    <p className="text-skin-sub-header font-semibold text-xs">
                        {" "}
                        Home - WareHouseTransfer
                    </p>
                </>
            }
            headerButton={<></>}
        >
            <Toast ref={toast} />
            {/* View Table*/}
            <div className="card min-h-screen ">
                <div className="bg-gray-100  flex items-center justify-center py-12 px-2 sm:px-6 lg:px-8">
                    <div className="w-full max-w-5xl bg-white rounded-2xl shadow-lg border border-gray-200 p-4 sm:p-8 md:p-10">
                        <h2 className="text-2xl md:text-3xl font-bold text-center text-gray-800 mb-8 md:mb-10 tracking-tight">
                            Filter Report Criteria
                        </h2>

                        <div className="grid grid-cols-12 gap-6">
                            {/* Starting Date */}
                            <div className="col-span-12 sm:col-span-4">
                                <label className="block text-sm font-medium text-gray-600 mb-2">
                                    Starting Date
                                </label>
                                <Calendar
                                    placeholder="dd/mm/yyyy"
                                    value={startDate}
                                    onChange={(e) => setStartDate(e.value)}
                                    className="w-full rounded-lg shadow-sm focus:ring-2 focus:ring-blue-300"
                                />
                            </div>

                            {/* End Date */}
                            <div className="col-span-12 sm:col-span-4">
                                <label className="block text-sm font-medium text-gray-600 mb-2">
                                    End Date
                                </label>
                                <Calendar
                                    value={endDate}
                                    placeholder="dd/mm/yyyy"
                                    onChange={(e) => setEndDate(e.value)}
                                    className="w-full rounded-lg shadow-sm focus:ring-2 focus:ring-blue-300"
                                />
                            </div>

                            {/* From Warehouse */}
                            <div className="col-span-12 sm:col-span-4">
                                <label className="block text-sm font-medium text-gray-600 mb-2">
                                    From Warehouse/Shop
                                </label>
                                <Dropdown
                                    value={formWarehouse}
                                    options={warehouseList}
                                    optionLabel="name"
                                    optionValue="id"
                                    placeholder="Select a Warehouse"
                                    showClear
                                    disabled={
                                        !permission.includes(
                                            "report_super_admin_access"
                                        )
                                    }
                                    onChange={(e) =>
                                        setFormWarehouse(e.target.value ?? null)
                                    }
                                    className="w-full rounded-lg shadow-sm"
                                    inputClassName="h-11 px-4 text-sm"
                                />
                            </div>

                            {/* To Warehouse */}
                            <div className="col-span-12 sm:col-span-4">
                                <label className="block text-sm font-medium text-gray-600 mb-2">
                                    To Warehouse/Shop
                                </label>
                                <Dropdown
                                    value={toWarehouse}
                                    options={warehouseList}
                                    optionLabel="name"
                                    optionValue="id"
                                    placeholder="Select a Warehouse"
                                    showClear
                                    onChange={(e) =>
                                        setToWarehouse(e.target.value ?? null)
                                    }
                                    className="w-full rounded-lg shadow-sm"
                                    inputClassName="h-11 px-4 text-sm"
                                />
                            </div>
                        </div>

                        {/* Submit Button */}
                        <div className="mt-10 flex justify-center">
                             <button
                                type="button"
                                onClick={openInNewTab}
                                disabled={isLoading}
                                className={`px-6 py-3 rounded-xl font-semibold text-sm min-w-[160px] flex items-center justify-center gap-2 transition duration-200
                    ${
                        isLoading
                            ? "bg-blue-400 cursor-not-allowed"
                            : "bg-blue-600 hover:bg-blue-700"
                    }
                    text-white shadow-md`}
                            >
                                {isLoading ? (
                                    <>
                                        <span
                                            className="w-5 h-5 border-2 border-white border-t-transparent rounded-full animate-spin"
                                            aria-hidden="true"
                                        />
                                        <span className="text-xs font-medium">
                                            Generating...
                                        </span>
                                    </>
                                ) : (
                                    <>
                                        <span
                                            role="img"
                                            aria-label="View Report"
                                        >
                                            🔍
                                        </span>
                                        View Report
                                    </>
                                )}
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </AuthenticatedLayout>
    );
}

export default WareHouseTransfer;
