import React, { useEffect, useState } from 'react';
import { View, Text, Image, TouchableOpacity, StyleSheet } from 'react-native';
import { useNavigation } from '@react-navigation/native';
import { apiService } from '../../utils/api';
import { rpx } from '../../utils/rpx';
interface KeyItem {
    id: string;
    avatarUrl: string;
    shareUserName: string;
    sharePhone: string;
    status: string;
    expirationTime: string;
}

const DeviceShare = () => {
    const navigation = useNavigation();
    const [keyList, setKeyList] = useState<KeyItem[]>([]);

    const getKeyList = async () => {
        const response = await apiService.getKeyListByOwnerId();

        if (response.code == 200) {
            setKeyList(response.data);

        }
    };
    const calculateRemainingTime = (expirationTime: string): string => {
        const now = new Date();
        const expiration = new Date(expirationTime);
        const diffTime = expiration.getTime() - now.getTime();

        // 如果已过期
        if (diffTime <= 0) {
            return '已过期';
        }

        // 计算剩余天数、小时、分钟
        const days = Math.floor(diffTime / (1000 * 60 * 60 * 24));
        const hours = Math.floor((diffTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        const minutes = Math.floor((diffTime % (1000 * 60 * 60)) / (1000 * 60));

        if (days > 0) {
            return `${days}天${hours}小时`;
        } else if (hours > 0) {
            return `${hours}小时${minutes}分钟`;
        } else {
            return `${minutes}分钟`;
        }
    };
    const getStatusText = (status: string | number) => {
        switch (status) {
            case '0':
            case 0:
                return '待领取';
            case '1':
            case 1:
                return '已领取';
            default:
                return '未知状态';
        }
    };
    useEffect(() => {
        getKeyList();
    }, []);

    return (
        <View style={styles.container}>
            <View style={styles.shareBox}>
                <Text style={styles.shareTitle}>共享钥匙（{keyList.length}/3）</Text>
                <Text style={styles.shareTxt}>临时借用车辆，可以使用基础控车功能</Text>

                {keyList.map((key) => (
                    <TouchableOpacity 
                    key={key.id}
                    onPress={() => {
                        navigation.navigate('ShareDetailScreen', { keyItem: key });
                    }}
                >

                        <View key={key.id} style={styles.card}>
                            <View style={styles.cardTop}>
                                <Image
                                    source={{ uri: key.avatarUrl || 'https://lxnapi.ccttiot.com/bike/img/static/uVnIDwcwQP7oo12PeYVJ' }}
                                    style={styles.cardTopImg}
                                />
                                <View style={styles.cardTopTxt}>
                                    <Text style={styles.cardTopName}>{key.shareUserName}</Text>
                                    <Text style={styles.cardTopPhone}>{key.sharePhone}</Text>
                                </View>
                                <Text style={[
                                    styles.cardType,
                                    { color: key.status == '0' ? '#FF9900' : '#4297F3' }
                                ]}>
                                    {getStatusText(key.status)}
                                </Text>
                            </View>
                            <View style={styles.cardBottom}>
                                <Text style={styles.lasttime}>
                                    剩余有效期：{calculateRemainingTime(key.expirationTime)}
                                </Text>
                                <Image
                                    source={{ uri: 'https://lxnapi.ccttiot.com/bike/img/static/uGq4yJlU1ZZRkwiJ8Y74' }}
                                    style={styles.lasttimeImg}
                                />
                            </View>
                        </View>
                    </TouchableOpacity>

                ))}
                {keyList.length < 3 && (
                    <TouchableOpacity onPress={() => {
                        navigation.navigate('AddShare' as never);
                    }}>
                        <View style={styles.addBtn}>
                            <Image
                                source={{ uri: 'https://lxnapi.ccttiot.com/bike/img/static/uI4CdJFzS1GkY1AzfFyG' }}
                                style={styles.addBtnImg}
                            />
                            <Text style={styles.addBtnTxt}>添加成员</Text>
                        </View>
                    </TouchableOpacity>
                )}
            </View>

            <View style={styles.shareTip}>
                <Text style={styles.shareBtnTxt}>查看已失效共享</Text>
                <Image
                    source={{ uri: 'https://lxnapi.ccttiot.com/bike/img/static/uGq4yJlU1ZZRkwiJ8Y74' }}
                    style={styles.lasttimeImg}
                />
            </View>
        </View>
    );
};


const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#F3FCFF',
        alignItems: 'center',
    },
    addBtnImg: {
        width: rpx(32),
        height: rpx(32),
    },
    shareTip: {
        flexDirection: 'row',
        alignItems: 'center',
        marginTop: rpx(44),
    },
    shareBtnTxt: {
        fontSize: rpx(32),
        color: '#808080',
    },
    addBtn: {
        width: rpx(592),
        height: rpx(108),
        backgroundColor: '#ffffff',
        borderRadius: rpx(16),
        marginTop: rpx(30),
        borderWidth: rpx(2),
        borderColor: '#808080',
        alignItems: 'center',
        justifyContent: 'center',
        flexDirection: 'row',
    },
    addBtnTxt: {
        marginLeft: rpx(12),
        fontSize: rpx(36),
        color: '#808080',
    },
    cardBottom: {
        flexDirection: 'row',
        alignItems: 'center',
        display: 'flex',
        flexWrap: 'nowrap',
    },
    lasttime: {
        marginLeft: rpx(124),
        fontSize: rpx(24),
        color: '#4297F3',
    },
    lasttimeImg: {
        marginLeft: 'auto',
        width: rpx(24),
        height: rpx(24),
    },
    cardTop: {
        flexDirection: 'row',
        alignItems: 'center',
        display: 'flex',
        flexWrap: 'nowrap',
    },
    cardType: {
        marginLeft: 'auto',
        alignSelf: 'flex-start',
        fontSize: rpx(32),
        color: '#4297F3',
    },
    cardTopTxt: {
        marginLeft: rpx(28),

    },
    cardTopImg: {
        width: rpx(96),
        height: rpx(96),
        borderRadius: rpx(48),
    },
    cardTopName: {

        fontSize: rpx(44),
        color: '#3D3D3D',
    },
    cardTopPhone: {
        fontSize: rpx(36),
        color: '#808080',
    },

    shareBox: {
        width: rpx(688),
        borderRadius: rpx(30),
        backgroundColor: '#fff',
        padding: rpx(32),
    },
    shareTitle: {
        fontWeight: 'bold',
        fontSize: rpx(44),
        color: '#3D3D3D',
    },
    shareTxt: {
        marginTop: rpx(14),
        fontWeight: '500',
        fontSize: rpx(28),
        color: '#999',
    },
    card: {
        width: rpx(592),
        padding: rpx(32),
        marginTop: rpx(32),
        borderRadius: rpx(16),
        borderWidth: rpx(2),
        borderColor: '#808080 ',

    },
});
export default DeviceShare;
