// @ts-nocheck
import { View } from "@tarojs/components";
import "./index.scss";
import { AtButton } from "taro-ui";
import React, { useEffect, useState } from "react";
import Taro from "@tarojs/taro";
import { chunks as infoChunks } from "./info.md";
import { chunks as payChunks } from "./pay.md";

const joinChunks = (chunks) => chunks.map(c => c.html).join("");

const Sphone = () => {
  const [isWeChat, setisWeChat] = useState(false);
  const [sphone, setsphone] = useState(window.localStorage.getItem("sphone"));

  useEffect(() => {
    isWeChatFun();
  }, []);

  // const handleClick = (value) => {
  //   // 跳转到目的页面，在当前页面打开
  //   Taro.navigateTo({
  //     url: "/pages/index/index",
  //   });
  // };

  const isWeChatFun = () => {
    setisWeChat(navigator.userAgent.toLowerCase().includes("micromessenger"));
  };
  
  const payh5 = (type, total_fee = 1000) => {
    // console.log("111");
    Taro.request({
      method: "POST",
      url: `${process.env.TARO_API_API}/payh5`, //仅为示例，并非真实的接口地址
      // url: "https://pay.hackrobot.cn${process.env.TARO_API_API}/payh5", //仅为示例，并非真实的接口地址

      data: {
        callback_url: window.location.href,
        openid: window.localStorage.getItem("openid"),
        total_fee: total_fee, // 金额,单位:分
        type: type, //订单类型 sphone/meetup/video/vip
      },
      header: {
        "content-type": "application/json", // 默认值
      },
      success: function (res) {
        console.log(res.data);
        // window.alert(res.data.jsapi);
        WeixinJSBridge.invoke(
          "getBrandWCPayRequest",
          {
            // 以下6个支付参数通过payjs的jsapi接口获取
            // appId: "wxc5205a653b0259bf",
            // timeStamp: "1701401644",
            // nonceStr: "KhOYB0wFV6j9qyQK",
            // package: "prepay_id=wx01113404850024729b35f4d69b73500000",
            // signType: "MD5",
            // paySign: "2A823C8D47CCF871C6C65A56DC228CF8",
            ...(res?.data?.jsapi ?? {}),
          },
          function (res) {
            // 支付成功
            if (res.err_msg == "get_brand_wcpay_request:ok") {
              window.localStorage.setItem(type, 1); //订单类型 sphone/meetup/video/vip
              WeixinJSBridge.call("closeWindow");
            }
          }
        );
      },
    });
  };


  return (
    <View className="Sphone">
      <View
        className="markdown-body"
        dangerouslySetInnerHTML={{
          __html: joinChunks(sphone == 1 ? payChunks : infoChunks),
        }}
      />

      <View className="bottomDiv"></View>
      {/* 判断有没有wxid,是否女性 */}
      {sphone != 1 ? (
        <AtButton
          type="primary"
          size="normal"
          className="joinSalon"
          onClick={() => {
            if (sphone == 1) {
              return false;
            } else {
              if (!!isWeChat) {
                // 180元
                payh5("sphone", 18000);
              } else {
                // setmeetupQR(true);
              }
            }
          }}
        >
          {/* {meetup == 1 ? "已加入" : `立即加入`} */}
          {`阅读更多  80`}
        </AtButton>
      ) : null}
    </View>
  );
};

export default Sphone;
