import { View ,Text} from "@tarojs/components";
import Buttons from '@/components/basic/Buttons'
import Texts from '@/components/basic/Texts'
import './ChooseAuth.scss'
import { ButtonType, ComponentStatus, TextType } from "../../utils/types";
import Taro, { useReady } from "@tarojs/taro";
import { useDispatch, useSelector } from "react-redux";
import { useEffect, useState } from "react";
import { wxLogin } from "@/services/user";
import { ColorType } from "@/context/themes/color";
import { ChooseScenarioBtn } from "@/features/common/SpecBtns";

export default function Page() {
    const dispatch = useDispatch();
    var code = '';

    const user = useSelector((state: any) => state.user);

    useEffect(() => {
        if (user.isLogin) {
            if (user.scenario_select_count > 0) {
                Taro.navigateBack({
                    delta: 1
                })
            }
            else {
                Taro.redirectTo({
                    url: '/pages/clock/ChooseScenario'
                })
            }

        }
    }, [user.isLogin])

    useReady(() => {
        if (process.env.TARO_ENV === 'weapp'){
            Taro.login().then(res => {
                code = res.code;
    
            })
        }
        
    })

    async function login() {
        dispatch(wxLogin(code) as any);
        // try {
        //     const { encryptedData, iv } = await Taro.getUserProfile({
        //         desc: '获取用户信息',
        //     });

        //     dispatch(wxLogin(code, encryptedData, iv) as any);

        //     // 在这里处理用户信息
        // } catch (error) {
        //     console.log('获取用户信息失败:', error);
        // }
    }

    function createAccount() {
        Taro.navigateTo({
            url: '/pages/account/Auth'
        })
    }

    return <View className="container choose_container">
        <Texts text='fast16cc' type={TextType.big} />
        <Text className="slogon">健康生物钟</Text>
        <ChooseScenarioBtn title='微信登录' onClick={login} background={ColorType.fast} />
        {/* <View style={{ height: 100 }} /> */}

        {/* <Buttons title='微信登录' onClick={login} btnStyle={{ width: 289, marginBottom: 30,backgroundColor:ColorType.fast }} /> */}
        {/* <Buttons title='Create account' type={ButtonType.outline} onClick={createAccount} btnStyle={{ width: 289, marginBottom: 30 }} /> */}
    </View>;
}