<template>
    <div class="card shadow-none">
        <div class="card-header">
            <h6 class="card-title mb-0">
                <i class="fe fe-map-pin me-2"></i>Hành trình đơn hàng
            </h6>
        </div>
        <div class="card-body tracking-list">
            <div v-if="!trackModel || trackModel.length === 0" class="text-center py-4">
                <i class="fe fe-inbox fs-1 text-muted"></i>
                <p class="mt-2 text-muted">Chưa có hành trình nào</p>
            </div>

            <div v-else class="timeline">
                <div v-for="(item, index) in trackModel" :key="item.Id" class="timeline-item">
                    <div class="timeline-marker" :class="getMarkerClass(index)">
                        <i :class="getMarkerIcon(item.TrackActionTypeStr)"></i>
                    </div>
                    <div class="timeline-content">
                        <div class="timeline-header">
                            <div class="d-flex justify-content-between align-items-center">
                                <h6 class="mb-1">
                                    <span :class="getActionBadgeClass(item.TrackActionTypeStr)">
                                        {{ item.TrackActionTypeStr }}
                                    </span>
                                </h6>
                                <small class="text-muted">
                                    {{ item.CreatedTimeDisplay }}
                                </small>
                            </div>
                            <p class="text-muted mb-2">
                                <i class="fe fe-user me-1"></i>
                                {{ item.CreatedByUserName }}
                                <span v-if="item.PicUserName && item.PicUserName.length > 0" class="ms-2">
                                    <i class="fe fe-user-check me-1"></i>
                                    {{ item.PicUserName }}
                                </span>
                            </p>
                        </div>

                        <div v-if="hasContent(item)" class="timeline-body">
                            <div v-if="item.Name && item.Name.length > 0" class="mb-3">
                                <div class="info-item">
                                    <label class="fw-bold">Mô tả:</label>
                                    <span class="ms-2">{{ item.Name }}</span>
                                </div>
                            </div>

                            <!-- Media items -->
                            <div v-if="item.MediaItems && item.MediaItems.length > 0" class="mt-3">
                                <div class="media-section-header" @click="toggleMediaSection(item.Id)">
                                    <h6 class="mb-2 d-flex align-items-center">
                                        <i class="fe fe-image me-1"></i>
                                        Hình ảnh ({{ item.MediaItems.length }})
                                        <i :class="getCollapseIcon(item.Id)" class="ms-auto"></i>
                                    </h6>
                                </div>
                                <div v-show="!collapsedSections.includes(item.Id)" class="media-section-content">
                                    <viewer>
                                        <div class="media-gallery">
                                            <div v-for="media in item.MediaItems" :key="media.Id" class="media-item">
                                                <div class="media-card">
                                                    <div class="image-container">
                                                        <img :src="media.Url"
                                                            :alt="media.AltText || 'Hình ảnh hành trình'"
                                                            class="media-image" @error="handleImageError"
                                                            @load="handleImageLoad" loading="lazy">
                                                        <div class="image-overlay">
                                                            <i class="fe fe-maximize-2"></i>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </viewer>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue';

const props = defineProps({
    trackModel: Object
});

const collapsedSections = ref([]);

// Function để collapse tất cả sections
const collapseAllSections = () => {
    collapsedSections.value = [];
    if (props.trackModel && props.trackModel.length > 0) {
        props.trackModel.forEach(item => {
            if (item.MediaItems && item.MediaItems.length > 0) {
                collapsedSections.value.push(item.Id);
            }
        });
    }
};

// Mặc định collapse tất cả sections khi component mount
onMounted(() => {
    collapseAllSections();
});

// Watch trackModel để collapse sections khi dữ liệu thay đổi
watch(() => props.trackModel, () => {
    collapseAllSections();
}, { deep: true });

// Methods
const getMarkerClass = (index) => {
    return index === 0 ? 'current' : 'completed';
};

const getMarkerIcon = (actionType) => {
    // Map các action type với icon tương ứng
    const iconMap = {
        'Tạo đơn hàng': 'fe fe-plus',
        'Xác nhận đơn hàng': 'fe fe-check',
        'Đóng gói': 'fe fe-package',
        'Giao hàng': 'fe fe-truck',
        'Hoàn thành': 'fe fe-check-circle',
        'Hủy đơn': 'fe fe-x-circle',
        'Trả hàng': 'fe fe-rotate-ccw',
        'default': 'fe fe-circle'
    };

    return iconMap[actionType] || iconMap.default;
};

const getActionBadgeClass = (actionType) => {
    const baseClass = 'badge';
    const statusMap = {
        'Tạo đơn hàng': `${baseClass} bg-primary`,
        'Xác nhận đơn hàng': `${baseClass} bg-info`,
        'Đóng gói': `${baseClass} bg-warning`,
        'Giao hàng': `${baseClass} bg-success`,
        'Hoàn thành': `${baseClass} bg-success`,
        'Hủy đơn': `${baseClass} bg-danger`,
        'Trả hàng': `${baseClass} bg-secondary`,
        'default': `${baseClass} bg-secondary`
    };

    return statusMap[actionType] || statusMap.default;
};



const handleImageError = (event) => {
    console.error('Image failed to load:', event.target.src);
    event.target.style.display = 'none';
    event.target.alt = 'Không thể tải ảnh';
};

const handleImageLoad = (event) => {
    console.log('Image loaded successfully:', event.target.src);
};

// Helper function để kiểm tra xem item có nội dung không
const hasContent = (item) => {
    return (item.Name && item.Name.length > 0) ||
        (item.MediaItems && item.MediaItems.length > 0);
};

// Toggle collapse/expand cho media section
const toggleMediaSection = (itemId) => {
    const index = collapsedSections.value.indexOf(itemId);
    if (index > -1) {
        collapsedSections.value.splice(index, 1);
    } else {
        collapsedSections.value.push(itemId);
    }
};

// Lấy icon cho collapse/expand
const getCollapseIcon = (itemId) => {
    return collapsedSections.value.includes(itemId)
        ? 'fe fe-chevron-down'
        : 'fe fe-chevron-up';
};
</script>

<style scoped>
.tracking-list {
    max-height: 80vh;
    overflow: auto;
    padding: 20px;
}

.info-item {
    margin-bottom: 10px;
}

.timeline {
    position: relative;
    padding-left: 30px;
}

.timeline::before {
    content: '';
    position: absolute;
    left: 15px;
    top: 0;
    bottom: 0;
    width: 2px;
    background: #e9ecef;
}

.timeline-item {
    position: relative;
    margin-bottom: 30px;
}

.timeline-marker {
    position: absolute;
    left: -22px;
    top: 0;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 12px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.timeline-marker.current {
    background: #28a745;
    animation: pulse 2s infinite;
}

.timeline-marker.completed {
    background: #6c757d;
}

@keyframes pulse {
    0% {
        box-shadow: 0 0 0 0 rgba(40, 167, 69, 0.7);
    }

    70% {
        box-shadow: 0 0 0 10px rgba(40, 167, 69, 0);
    }

    100% {
        box-shadow: 0 0 0 0 rgba(40, 167, 69, 0);
    }
}

.timeline-content {
    background: #f8f9fa;
    border-radius: 8px;
    padding: 15px;
    margin-left: 20px;
    border: 1px solid #e9ecef;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.timeline-header {
    border-bottom: 1px solid #dee2e6;
    padding-bottom: 10px;
    margin-bottom: 15px;
}

.timeline-body {
    background: white;
    border-radius: 6px;
    padding: 15px;
    margin-top: 10px;
}

.media-section-header {
    cursor: pointer;
    transition: background-color 0.2s;
}

.media-section-header:hover {
    background-color: #f8f9fa;
    border-radius: 6px;
    padding: 5px;
    margin: -5px;
}

.media-section-content {
    transition: all 0.3s ease;
}

.media-gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: 10px;
    margin-top: 10px;
}

.media-card {
    border: 1px solid #dee2e6;
    border-radius: 8px;
    overflow: hidden;
    background: white;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    transition: box-shadow 0.2s;
}

.media-card:hover {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.image-container {
    position: relative;
    overflow: hidden;
    cursor: pointer;
}

.media-image {
    width: 100%;
    height: 80px;
    object-fit: cover;
    cursor: pointer;
    transition: transform 0.2s;
    display: block;
    position: relative;
    z-index: 1;
}

.media-image:hover {
    transform: scale(1.05);
}

.image-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.2s;
    color: white;
    font-size: 1.5rem;
    z-index: 2;
    pointer-events: none;
}

.image-container:hover .image-overlay {
    opacity: 1;
}



.badge {
    font-size: 0.75em;
    padding: 0.375em 0.75em;
}

@media (max-width: 768px) {
    .tracking-list {
        padding: 10px;
    }

    .timeline {
        padding-left: 20px;
    }

    .timeline-marker {
        left: -15px;
        width: 25px;
        height: 25px;
        font-size: 10px;
    }

    .timeline-content {
        margin-left: 15px;
    }

    .media-gallery {
        grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
    }
}
</style>