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

export default hocAuth(function Page(props: CommonComponent) {
  const { router, setLoading } = props;
  const orderId = router.params.orderId as string;

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

  const init = async (orderId: string) => {
    setLoading(true);
    try {
      // 获取采购单信息
      const { data: purchaseData } = await business.order.showOrder({
        orderShowQry: {
          orderId: orderId,
        },
      });

      if (purchaseData.success) {
        setOrderVO(purchaseData.data);
      }
    } catch (error) {
      Toast.show("toast", {
        icon: "fail",
        title: "提示",
        content: "获取采购单信息失败",
      });
    } finally {
      setLoading(false);
    }
  };

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

  // 查看采购单详情
  const viewOrderDetail = () => {
    if (orderVO?.orderId) {
      Taro.navigateTo({
        url: buildUrl("/pages/purchase/market/preview", {
          orderId: orderVO.orderId,
        }),
      });
    }
  };

  return (
    <View className="flex flex-1 flex-col gap-2.5">
      <View className="flex flex-1 flex-col items-center justify-start bg-gray-100 p-2.5">
        <View className="mb-2.5 flex h-16 w-16 items-center justify-center rounded-full bg-green-100">
          <Text className="text-2xl text-green-600">✓</Text>
        </View>

        <View className="mb-2.5 text-xl font-bold text-gray-800">
          采购单已提交审核
        </View>

        <View className="mb-2.5 text-sm text-gray-600">等待报价审核员处理</View>

        <View className="mb-2.5 text-xs text-gray-400">
          您可以通过以下操作继续处理
        </View>

        <View className="w-full rounded-lg bg-white p-2.5 shadow-md">
          <View className="mb-2.5 border-b border-gray-200 pb-2">
            <Text className="text-lg font-semibold">单据信息</Text>
          </View>

          <View className="mb-2.5 flex flex-col gap-2.5">
            <View className="flex flex-row justify-between">
              <Text className="text-neutral-darker text-sm">采购单号:</Text>
              <CopyText copyData={orderVO?.orderSn || "-"}>
                <Text className="text-sm font-medium">
                  {orderVO?.orderSn || "-"}
                </Text>
              </CopyText>
            </View>

            <View className="flex flex-row justify-between">
              <Text className="text-neutral-darker text-sm">采购单类型:</Text>
              <Text className="text-sm font-medium">
                {orderVO?.type === "PRODUCTION_PURCHASE"
                  ? "产地采购"
                  : "市场采购"}
              </Text>
            </View>

            <View className="flex flex-row justify-between">
              <Text className="text-neutral-darker text-sm">经销商:</Text>
              <Text className="text-sm font-medium">
                {orderVO?.orderVehicle?.dealerName || "-"}
              </Text>
            </View>

            <View className="flex flex-row justify-between">
              <Text className="text-neutral-darker text-sm">审核状态:</Text>
              <Text className="text-sm font-medium text-green-600">
                已提交审核
              </Text>
            </View>
          </View>

          <View className="border-t border-gray-200 pt-2.5">
            <View className="mb-2">
              <Text className="text-lg font-semibold">快捷操作</Text>
            </View>

            <View className="flex flex-col gap-3">
              <Button
                type="primary"
                size={"large"}
                block
                onClick={viewOrderDetail}
              >
                查看采购单详情
              </Button>
            </View>
          </View>
        </View>
      </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
              type="default"
              size={"large"}
              block
              onClick={() =>
                Taro.switchTab({
                  url: buildUrl("/pages/main/index/index"),
                })
              }
            >
              返回首页
            </Button>
          </View>
        </View>
        <SafeArea position={"bottom"} />
      </View>
    </View>
  );
});
