import { NavBar } from "@nutui/nutui-react-taro";
import { ArrowLeft } from "@nutui/icons-react-taro";
import { getStorageSync, navigateBack, navigateTo } from "@tarojs/taro";
import { View } from "@tarojs/components";
import config from "../../config";
import { useEffect, useState } from "react";
import "./index.scss";
export default function Act() {
  const [userInfo, setUserInfo] = useState<any>(null);
  // 监听iframe的emit过来的事件， onMessage
  useEffect(() => {
    window.addEventListener("message", (event) => {
      if (event.type === "message") {
        console.log("event.data", event.data);
        switch (event.data) {
          case "login":
            navigateTo({ url: "/pages/login/index" });
            break;
          default:
            break;
        }
      }
    });
  }, []);

  useEffect(() => {
    setUserInfo(getStorageSync("User"));
  }, []);
  return (
    <>
      <NavBar
        back={
          <>
            <ArrowLeft />
            返回
          </>
        }
        fixed
        onBackClick={(e) => {
          e.preventDefault();
          navigateBack({ delta: 1 });
        }}
      >
        积分中心
      </NavBar>
      <View>
        <iframe
          className="act-content"
          src={`${config.TARO_ACT_HOST}/jifen/exchange?vip_code=${userInfo?.vip_code}`}
        />
      </View>
    </>
  );
}
