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";
import { set } from "react-hook-form";

function SupplierOpening({ auth }) {
    const { permission, warehouseList } = useContext(authUser);

    const [startDate, setStartDate] = useState();
    const [endDate, setEndDate] = useState();
    const [location, setLocation] = useState([]);
    const toast = useRef(null);
    const [isLoading, setIsLoading] = useState(false);
    useEffect(() => {
        if (!permission.includes("report_super_admin_access")) {
            setLocation(auth.user.shop_id);
        }
    }, []);

    const search = async () => {
        setIsLoading(true);
        let form = new FormData();
        form.append("start", startDate);
        form.append("end", endDate);
        // form.append("location_id", location);
        let res = await axios.post("supplier-opening-search-report", form);
        return res.data;
    };

    const openInNewTab = async () => {
        const data = await search();
        const pdfDoc = await window.generatePDFView(
            data,
            window.reportModes.supplierOpening,
            false,
            "Supplier Account Opening",
            startDate,
            endDate
        );
        renderPDF(pdfDoc, "SupplierOpening.pdf");
        setIsLoading(false);
    };

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

                        <div className="grid  grid-cols-12  gap-6">
                            <div className="col-span-12 md:col-span-4">
                                <label className="block text-sm font-medium text-gray-600 mb-2">
                                    Starting Date
                                </label>

                                <Calendar
                                    value={startDate}
                                    onChange={(e) => {
                                        setStartDate(e.value);
                                    }}
                                    className="w-full rounded-lg shadow-sm focus:ring-2 focus:ring-blue-300"
                                ></Calendar>
                            </div>
                            <div className="col-span-12 md:col-span-4">
                                <label className="block text-sm font-medium text-gray-600 mb-2">
                                    End Date
                                </label>

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

                        <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 SupplierOpening;
