import hocAuth from "@/hocs/auth";
import { CommonComponent } from "@/types/typings";
import { View } from "@tarojs/components";
import { Button, SafeArea } from "@nutui/nutui-react-taro";
import { useEffect, useState } from "react";
import { MarketPreview, OrderSubmitReview } from "@/components";
import { business } from "@/services";
import Taro from "@tarojs/taro";
import { buildUrl } from "@/utils";
import { purchase } from "@/constant";

export default hocAuth(function Page(props: CommonComponent) {
  const { router } = props;
  const orderId = router.params.orderId as BusinessAPI.OrderShowQry["orderId"];

  const [orderVO, setOrderVO] = useState<BusinessAPI.OrderVO>();

  const init = async (orderId: BusinessAPI.OrderShowQry["orderId"]) => {
    const { data } = await business.order.showOrder({
      orderShowQry: {
        orderId: orderId,
        type: "MARKET_PURCHASE",
      },
    });

    if (data.success) {
      setOrderVO(data.data);
    }
  };

  useEffect(() => {
    if (orderId) {
      init(orderId).then();
    }
  }, [orderId]);

  if (!orderVO) {
    return null;
  }

  return (
    <>
      <View className={"overflow-x-hidden overflow-y-auto bg-[#D1D5DB]"}>
        {/* 采购预览 */}
        <MarketPreview orderVO={orderVO} />
      </View>

      {/* 按钮操作 */}
      <View className={"sticky bottom-0 z-10 bg-white"}>
        <View className="flex justify-between gap-2 border-t border-gray-200 p-2.5">
          <View className={"flex-1"}>
            <Button
              block
              type={"default"}
              size={"large"}
              onClick={() => {
                Taro.switchTab({
                  url: buildUrl("/pages/main/index/index"),
                });
              }}
            >
              返回首页
            </Button>
          </View>
          {orderVO.state === "DRAFT" && (
            <>
              <View className={"flex-1"}>
                <Button
                  block
                  type={"default"}
                  size={"large"}
                  onClick={() => {
                    Taro.redirectTo({
                      url: buildUrl("/pages/purchase/market/create", {
                        orderId: orderVO.orderId,
                        active: purchase.marketSteps.length - 1,
                      }),
                    });
                  }}
                >
                  上一步
                </Button>
              </View>
              <View className={"flex-1"}>
                <OrderSubmitReview
                  orderVO={orderVO}
                  size={"large"}
                  onFinish={(auditVO) => {
                    Taro.redirectTo({
                      url: buildUrl("/pages/purchase/market/result", {
                        orderId: orderVO.orderId,
                        auditId: auditVO.auditId,
                      }),
                    });
                  }}
                />
              </View>
            </>
          )}

          {orderVO.state === "AUDITING" &&
            orderVO.auditState === "QUOTE_REJECTED" && (
              <View className={"flex-1"}>
                <Button
                  block
                  type={"default"}
                  size={"large"}
                  onClick={() => {
                    Taro.redirectTo({
                      url: buildUrl("/pages/purchase/made/create", {
                        orderId: orderVO.orderId,
                        active: purchase.madeSteps.length - 1,
                      }),
                    });
                  }}
                >
                  编辑
                </Button>
              </View>
            )}
        </View>
        <SafeArea position={"bottom"} />
      </View>
    </>
  );
});
