import React, { useEffect, useState } from 'react';
import { View, StyleSheet } from 'react-native';
import { useNavigation ,useFocusEffect} from '@react-navigation/native';
import NoDevice from './NoDevice';
import NormaIndex from './NormaIndex';
import { apiService } from '../../utils/api'; // 假设 apiService 中有 getDeviceList 方法

const HomeScreen = () => {
    const navigation = useNavigation();
    const [hasMultipleDevices, setHasMultipleDevices] = useState(false);

    useFocusEffect(
        React.useCallback(() => {
            fetchDeviceList();
            getuserInfo()
        }, [])
    );

    // const checkLoginStatus = async () => {
    //     const isLoggedIn = await auth.isLoggedIn();
    //     if (!isLoggedIn) {
    //         navigation.getParent()?.navigate('Login');
    //     }
    // };
    const getuserInfo = async () => {
        try {
            const response = await apiService.getUserInfo();
            // if (response && response.data && response.data.length > 1) {
            //     setHasMultipleDevices(true);
            // } else {
            //     setHasMultipleDevices(false);
            // }
        } catch (error) {
            // console.error('获取设备列表失败:', error);
        }
    };
    const fetchDeviceList = async () => {
        try {
            const response = await apiService.getDeviceList();
            console.log(response.data.length,'response');
            if (response && response.data && response.data.length > 0) {
                setHasMultipleDevices(true);
            } else {
                setHasMultipleDevices(false);
            }
        } catch (error) {
            console.error('获取设备列表失败:', error);
        }
    };

    return (
        <View style={styles.container}>
            {hasMultipleDevices ? <NormaIndex /> : <NoDevice />}
        </View>
    );
};

const styles = StyleSheet.create({
    container: {
        flex: 1,
    },
});

export default HomeScreen;