<template>
    <div class="fuel-refill-detail">
        <!-- Loading State -->
        <div v-if="loading" class="text-center py-5">
            <span class="spinner-border spinner-border-sm"></span>
            <span class="ms-2">Đang tải dữ liệu...</span>
        </div>

        <!-- Error State -->
        <div v-if="!loading && error" class="alert alert-danger">
            <i class="fas fa-exclamation-triangle me-2"></i>
            {{ error }}
        </div>

        <!-- Detail Content -->
        <div v-if="!loading && !error && detail" class="modal-body p-0">
            <div class="row g-0">
                <!-- LEFT COLUMN: Hình ảnh -->
                <div class="col-lg-5 border-end">
                    <div class="p-4">
                        <h6 class="mb-3">
                            <i class="fas fa-images me-2"></i>Hình ảnh đính kèm 
                            <span v-if="detail.MediaItems && detail.MediaItems.length > 0" class="badge bg-info ms-2">
                                {{ detail.MediaItems.length }}
                            </span>
                        </h6>
                        
                        <div v-if="!detail.MediaItems || detail.MediaItems.length === 0" class="text-center py-5 text-muted">
                            <i class="fas fa-image fs-1 mb-3 d-block"></i>
                            <p>Không có hình ảnh</p>
                        </div>

                        <viewer v-else style="z-index: 99999 !important;">
                            <div class="media-gallery-vertical">
                                <div v-for="item in detail.MediaItems" :key="item.Id" class="media-item-vertical mb-3">
                                    <div class="media-card-vertical">
                                        <div class="image-container-vertical">
                                            <img :src="item.MediaUrl" 
                                                 :alt="item.AltText" 
                                                 :title="item.Caption"
                                                 class="media-image-vertical"
                                                 loading="lazy">
                                            <div class="image-overlay">
                                                <i class="fas fa-search-plus"></i>
                                            </div>
                                        </div>
                                        <div class="media-info-vertical p-2">
                                            <span class="badge bg-secondary mb-1 d-block">{{ item.MediaTypeStr }}</span>
                                            <p class="small text-muted mb-0">{{ item.Caption }}</p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </viewer>
                    </div>
                </div>

                <!-- RIGHT COLUMN: Thông tin chi tiết -->
                <div class="col-lg-7">
                    <div class="p-4">
                        <!-- Thông tin cơ bản -->
                        <div class="info-section mb-4">
                            <h6 class="section-title mb-3">
                                <i class="fas fa-info-circle me-2"></i>Thông tin cơ bản
                            </h6>
                            <div class="row g-3">
                                <div class="col-md-6">
                                    <div class="info-item">
                                        <label>Xe:</label>
                                        <div><span class="badge bg-info fs-6">{{ detail.TruckLicensePlate }}</span></div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="info-item">
                                        <label>Lái xe:</label>
                                        <div class="fw-bold">{{ detail.DriverName }}</div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="info-item">
                                        <label>Thời gian:</label>
                                        <div>{{ formatDateTime(detail.RefillTime) }}</div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="info-item">
                                        <label>Thanh toán:</label>
                                        <div>
                                            <span :class="getPaymentMethodBadge(detail.PaymentMethod)">
                                                {{ detail.PaymentMethodStr }}
                                            </span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Thông tin ODO -->
                        <div class="info-section mb-4">
                            <h6 class="section-title mb-3">
                                <i class="fas fa-tachometer-alt me-2"></i>Thông tin ODO
                            </h6>
                            <div class="row g-3">
                                <div class="col-4">
                                    <div class="stat-card">
                                        <div class="stat-label">ODO trước</div>
                                        <div class="stat-value">{{ formatNumber(detail.PreviousOdo) }}</div>
                                        <div class="stat-unit">km</div>
                                    </div>
                                </div>
                                <div class="col-4">
                                    <div class="stat-card stat-primary">
                                        <div class="stat-label">ODO hiện tại</div>
                                        <div class="stat-value">{{ formatNumber(detail.CurrentOdo) }}</div>
                                        <div class="stat-unit">km</div>
                                    </div>
                                </div>
                                <div class="col-4">
                                    <div class="stat-card stat-success">
                                        <div class="stat-label">Km chạy</div>
                                        <div class="stat-value">{{ formatNumber(detail.DistanceKm) }}</div>
                                        <div class="stat-unit">km</div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Thông tin dầu -->
                        <div class="info-section mb-4">
                            <h6 class="section-title mb-3">
                                <i class="fas fa-gas-pump me-2"></i>Thông tin dầu
                            </h6>
                            <div class="row g-3">
                                <div class="col-md-4">
                                    <div class="info-item">
                                        <label>Số tiền:</label>
                                        <div class="text-danger fw-bold fs-5">{{ formatCurrency(detail.Amount) }}</div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="info-item">
                                        <label>Đơn giá:</label>
                                        <div>{{ formatNumber(detail.UnitPrice) }} ₫/lít</div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="info-item">
                                        <label>Lít thực tế:</label>
                                        <div class="fw-bold">{{ formatLiters(detail.ActualLiters) }}</div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="info-item">
                                        <label>Lít khoán:</label>
                                        <div>{{ formatLiters(detail.QuotaLiters) }}</div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="info-item">
                                        <label>Chênh lệch:</label>
                                        <div :class="getDifferenceClass(detail.DifferenceLiters)">
                                            {{ getDifferenceIcon(detail.DifferenceLiters) }}
                                            {{ formatLiters(Math.abs(detail.DifferenceLiters)) }}
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Ghi chú -->
                        <div v-if="detail.Notes" class="info-section">
                            <h6 class="section-title mb-3">
                                <i class="fas fa-sticky-note me-2"></i>Ghi chú
                            </h6>
                            <div class="alert alert-info mb-0">
                                {{ detail.Notes }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Footer Actions -->
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" @click="close">
                    <i class="fas fa-times me-2"></i>Đóng
                </button>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import fuelCardService from '@/services/fuelCardService';
import { useToast } from 'vue-toastification';
import moment from 'moment';

const props = defineProps({
    id: {
        type: Number,
        required: true
    }
});

const emit = defineEmits(['close']);

const toast = useToast();
const loading = ref(false);
const error = ref(null);
const detail = ref(null);

onMounted(async () => {
    await loadDetail();
});

async function loadDetail() {
    loading.value = true;
    error.value = null;
    try {
        const result = await fuelCardService.getRefillById(props.id);
        if (result) {
            detail.value = result;
        } else {
            error.value = 'Không tìm thấy thông tin chi tiết';
            toast.error('Không tìm thấy thông tin chi tiết');
        }
    } catch (err) {
        console.error('Error loading refill detail:', err);
        error.value = 'Có lỗi xảy ra khi tải dữ liệu';
        toast.error('Có lỗi xảy ra khi tải dữ liệu');
    } finally {
        loading.value = false;
    }
}

function formatNumber(value) {
    if (value == null || value == undefined) return '0';
    return new Intl.NumberFormat('vi-VN').format(value);
}

function formatCurrency(value) {
    if (value == null || value == undefined) return '0 ₫';
    return new Intl.NumberFormat('vi-VN', {
        style: 'currency',
        currency: 'VND'
    }).format(value);
}

function formatLiters(value) {
    if (value == null || value == undefined) return '0.00 L';
    return `${parseFloat(value).toFixed(2)} L`;
}

function formatDateTime(value) {
    if (!value) return '';
    return moment(value).format('DD/MM/YYYY HH:mm');
}

function getDifferenceClass(difference) {
    if (difference > 0) {
        return 'fs-5 fw-bold text-success';
    } else if (difference < 0) {
        return 'fs-5 fw-bold text-danger';
    }
    return 'fs-6 text-muted';
}

function getDifferenceIcon(difference) {
    if (difference > 0) {
        return '↑';
    } else if (difference < 0) {
        return '↓';
    }
    return '';
}

function getPaymentMethodBadge(method) {
    return method === 1 ? 'badge bg-primary' : 'badge bg-success';
}

function close() {
    emit('close');
}
</script>

<style scoped>

.viewer-container {
    z-index: 99999 !important;
}

.fuel-refill-detail {
    max-height: 90vh;
    overflow-y: auto;
}

.card-header h6 {
    font-size: 0.95rem;
}

/* Section Styles */
.section-title {
    font-size: 1rem;
    font-weight: 600;
    color: #495057;
    border-bottom: 2px solid #e9ecef;
    padding-bottom: 0.5rem;
}

.info-section {
    background: #f8f9fa;
    border-radius: 8px;
    padding: 1rem;
}

.info-item label {
    font-size: 0.85rem;
    font-weight: 600;
    color: #6c757d;
    margin-bottom: 0.25rem;
    display: block;
}

.info-item div {
    font-size: 0.95rem;
    color: #212529;
}

/* Stat Cards */
.stat-card {
    background: white;
    border: 1px solid #dee2e6;
    border-radius: 8px;
    padding: 1rem;
    text-align: center;
    transition: all 0.3s;
}

.stat-card:hover {
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    transform: translateY(-2px);
}

.stat-card.stat-primary {
    border-color: #0d6efd;
    background: #e7f1ff;
}

.stat-card.stat-success {
    border-color: #198754;
    background: #d1e7dd;
}

.stat-label {
    font-size: 0.75rem;
    color: #6c757d;
    margin-bottom: 0.5rem;
    text-transform: uppercase;
    font-weight: 600;
}

.stat-value {
    font-size: 1.5rem;
    font-weight: 700;
    color: #212529;
    margin-bottom: 0.25rem;
}

.stat-unit {
    font-size: 0.85rem;
    color: #6c757d;
}

/* Vertical Media Gallery */
.media-gallery-vertical {
    max-height: 70vh;
    overflow-y: auto;
    padding-right: 5px;
}

.media-gallery-vertical::-webkit-scrollbar {
    width: 6px;
}

.media-gallery-vertical::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 10px;
}

.media-gallery-vertical::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 10px;
}

.media-gallery-vertical::-webkit-scrollbar-thumb:hover {
    background: #555;
}

.media-item-vertical {
    width: 100%;
}

.media-card-vertical {
    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, transform 0.2s;
}

.media-card-vertical:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    transform: translateX(5px);
}

.image-container-vertical {
    position: relative;
    overflow: hidden;
    cursor: pointer;
    background: #f8f9fa;
}

.media-image-vertical {
    width: 100%;
    height: 200px;
    object-fit: cover;
    cursor: pointer;
    transition: transform 0.3s;
    display: block;
}

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

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

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

.media-info-vertical {
    background: white;
}

.media-info-vertical .badge {
    font-size: 0.7rem;
}

.media-info-vertical p {
    font-size: 0.75rem;
    line-height: 1.3;
}

@media (max-width: 992px) {
    .col-lg-5,
    .col-lg-7 {
        border-right: none !important;
    }

    .media-gallery-vertical {
        max-height: 50vh;
    }

    .media-image-vertical {
        height: 150px;
    }
}
</style>
