import { View,Text, Image, Checkbox, Button } from "@tarojs/components";
import { useLogin } from '@/xiaolanbenlib/hooks/data/useAuth'
import WemetaRadio from "@/components/WemetaRadio";

import { useEffect, useState } from "react";

import Popup from "@/components/popup/popup";
import Taro, { useDidShow } from "@tarojs/taro";
import { navToWebView } from "@/utils/index";
import { getSysAgreements, type TAgreementItem } from "@/service/system";
import { isSuccess } from "@/utils";

interface IProps {
  showPopup: boolean
  setShowPopup: (b: boolean)=> void
  onEnd: ()=>void
}

export default function Index({showPopup, setShowPopup, onEnd}:IProps) {
  const { onGetPhoneNumber, onError, onClick, openType } = useLogin({
    onSuccess: () => {
      console.log('yes')
      setShowPopup(false)
      onEnd()
    },
  })
  const [checked, setChecked] = useState(false);

  const handleChecked = () => {
    setChecked(prev => !prev)
  }

  const [agreements, setAgreements] = useState<TAgreementItem[]>([])

  const fetchPageData = async () => {
    const response = await getSysAgreements('register')
    if(isSuccess(response.status)){
      setAgreements(response.data)
    }
  }

  useEffect(()=> {
    fetchPageData()
  }, [])

  
  

  return (
    <Popup setShow={setShowPopup} show={showPopup}>
      <View className="text-24 text-center font-medium leading-36 text-black mb-12">欢迎来到小蓝本智能体</View>
      <View className="text-gray-45 text-center text-14 leading-24 mb-40">
        <View>激活您的AI商务分身，开启智能商务</View>
        <View>7x24h随时应答·智能管理知识库·主动拓展人脉</View>
      </View>
      <Button
        disabled={!checked}
        className="button-rounded-big mb-16"
        style={{backgroundColor: '#317CFA', fontSize: '14px'}}
        type='primary'
        openType={openType}
        onGetPhoneNumber={onGetPhoneNumber}
        onError={onError}
        onClick={() => {
          onClick()
        }}
      >
        手机号快速登录
      </Button>
      <View className="flex items-center justify-center text-12 mb-86 text-center">
        <View className="flex-center text-gray-45 gap-8" onClick={handleChecked}><WemetaRadio checkbox checked={checked}></WemetaRadio>同意</View>
        {agreements.map(item=> <Text onClick={()=> navToWebView(item.link)} className="primary-color">《{item.name}》</Text>)}
      </View>
    </Popup>
  );
}
