import React, { createContext, useContext, useState } from "react";
import Breadcrumb from "../../components/common/Breadcrumb";
import showToast from "../../services/toastService";
import GlobalModel from "../../components/common/GlobalModel";
import ManagePurchase from "../../components/purchase/ManagePurchase";
import PurchaseList from "../../components/purchase/PurchaseList";
import ViewPurchase from "../../components/purchase/ViewPurchase";
import DeliveryChallane from "../../components/purchase/DeliveryChallane";

export const PurchaseContext = createContext();
export const usePurchaseContext = () => {
  let context = useContext(PurchaseContext);

  if (!context) {
    showToast("Please use Innne cntext", "warning");
    window.location = "/dashboard";
  }
  return context;
};
const PurchasePage = () => {
  const [searchText, setSearchText] = useState("");
  const [addChallan, setChallan] = useState({
    isOpen: false,
    purchaseId: null,
  });
  const [managePurchase, setManagePurchase] = useState({
    isOpen: false,
    purchaseId: null,
  });
  const [viewPurchaseState, setViewPurchase] = useState({
    isOpen: false,
    purchaseId: null,
  });

  const contextValue = {
    setViewPurchase,
    setManagePurchase,
    setChallan,
  };
  return (
    <PurchaseContext.Provider value={contextValue}>
      <div className="container-fluid">
        <Breadcrumb
          data={[
            { label: "Home", link: "/dashboard" },
            { label: "Procurement & Inventory", link: "/purchase-invoice" },
            { label: "Purchase" },
          ]}
        />
        <div className="card px-sm-4 my-3">
          <div className="row p-2">
            <div className="col-12 col-md-6 text-start">
              {" "}
              <label className="mb-0">
                <input
                  type="search"
                  value={searchText}
                  onChange={(e) => setSearchText(e.target.value)}
                  className="form-control form-control-sm"
                  placeholder="Search Purchase"
                  aria-controls="DataTables_Table_0"
                />
              </label>
            </div>
            <di className="col-6 text-end">
              <button
                className="btn btn-sm btn-primary"
                onClick={() =>
                  setManagePurchase({
                    isOpen: true,
                    purchaseId: null,
                  })
                }
              >
                <i className="bx bx-plus-circle me-2"></i>Add
              </button>
            </di>
          </div>
        </div>

        <PurchaseList searchString={searchText} />
        {managePurchase.isOpen && (
          <GlobalModel
            isOpen={managePurchase.isOpen}
            size="lg"
            closeModal={() =>
              setManagePurchase({
                isOpen: false,
                purchaseId: null,
              })
            }
          >
            <ManagePurchase
              onClose={() =>
                setManagePurchase({
                  isOpen: false,
                  purchaseId: null,
                })
              }
              purchaseId={managePurchase.purchaseId}
            />
          </GlobalModel>
        )}

        {viewPurchaseState.isOpen && (
          <GlobalModel
            isOpen={viewPurchaseState.isOpen}
            size="lg"
            closeModal={() =>
              setViewPurchase({ isOpen: false, purchaseId: null })
            }
          >
            <ViewPurchase purchaseId={viewPurchaseState.purchaseId} />
          </GlobalModel>
        )}

        {addChallan.isOpen && (
          <GlobalModel size="xl"
            isOpen={addChallan.isOpen}
            closeModal={() => setChallan({ isOpen: false, purchaseId: null })}
          >
            <DeliveryChallane
              purchaseId={addChallan.purchaseId}
              onClose={() => setChallan({ isOpen: false, purchaseId: null })}
            />
          </GlobalModel>
        )}
      </div>
    </PurchaseContext.Provider>
  );
};

export default PurchasePage;
