import { ActionType, PurchaseOrderAuditList } from "@/components";
import { useShareAppMessage } from "@tarojs/taro";
import { useRef, useState } from "react";
import hocAuth from "@/hocs/auth";
import { CommonComponent } from "@/types/typings";
import purchaseOrder from "@/constant/purchaseOrder";

export default hocAuth(function Page(props: CommonComponent) {
  const { shareOptions } = props;

  const [auditState, setAuditState] = useState<
    BusinessAPI.PurchaseOrderPageQry["auditState"]
  >("PENDING_QUOTE_APPROVAL");

  const actionRef = useRef<ActionType>();

  useShareAppMessage((res) => {
    console.log("useShareAppMessage1", res, shareOptions);
    // 如果是按钮触发的转发，使用默认配置
    if (res.from === "button") {
      return shareOptions;
    }
    // 页面转发使用设置的配置
    return {};
  });

  return (
    <PurchaseOrderAuditList
      actionRef={actionRef}
      toolbar={{
        tabs: {
          activeKey: "state",
          defaultActiveKey: auditState!,
          items: purchaseOrder.auditStateList,
          onChange: (item) => {
            setAuditState(
              item as BusinessAPI.PurchaseOrderPageQry["auditState"],
            );
            actionRef.current?.reload();
          },
        },
      }}
      params={{
        state: "WAITING_AUDIT",
        auditState: auditState,
      }}
    />
  );
});
