<script setup>
import { ref, computed } from 'vue';
import GoogleIcon from '@Shared/GoogleIcon.vue';

/** Propiedades */
const props = defineProps({
    modelValue: {
        type: String,
        default: 'A4'
    }
});

/** Eventos */
const emit = defineEmits(['update:modelValue']);

/** Referencias */
const isOpen = ref(false);

/** Tamaños de página disponibles */
const pageSizes = [
    {
        name: 'A4',
        label: 'A4 (210 x 297 mm)',
        width: 794,
        height: 1123,
        description: 'Estándar internacional'
    },
    {
        name: 'A3',
        label: 'A3 (297 x 420 mm)',
        width: 1123,
        height: 1587,
        description: 'Doble de A4'
    },
    {
        name: 'Letter',
        label: 'Carta (216 x 279 mm)',
        width: 816,
        height: 1056,
        description: 'Estándar US'
    },
    {
        name: 'Legal',
        label: 'Oficio (216 x 356 mm)', 
        width: 816,
        height: 1344,
        description: 'Legal US'
    },
    {
        name: 'Tabloid',
        label: 'Tabloide (279 x 432 mm)',
        width: 1056,
        height: 1632,
        description: 'Doble carta'
    }
];

/** Propiedades computadas */
const selectedSize = computed(() => {
    return pageSizes.find(size => size.name === props.modelValue) || pageSizes[0];
});

/** Métodos */
const selectSize = (size) => {
    emit('update:modelValue', size.name);
    isOpen.value = false;
};
</script>

<template>
    <div class="relative">
        <!-- Selector principal MEJORADO -->
        <button
            @click="isOpen = !isOpen"
            class="flex items-center gap-2 px-3 py-2 text-sm bg-white dark:bg-primary-d border border-gray-200 dark:border-primary/20 rounded-lg hover:bg-gray-50 dark:hover:bg-primary/10 transition-colors shadow-sm"
        >
            <GoogleIcon name="aspect_ratio" class="text-gray-500 dark:text-primary-dt/70" />
            <span class="text-gray-700 dark:text-primary-dt font-medium">{{ selectedSize.name }}</span>
            <GoogleIcon 
                name="expand_more" 
                class="text-gray-400 dark:text-primary-dt/50 transition-transform"
                :class="{ 'rotate-180': isOpen }"
            />
        </button>

        <!-- Dropdown -->
        <div
            v-if="isOpen"
            @click.away="isOpen = false"
            class="absolute top-full left-0 mt-2 w-80 bg-white dark:bg-primary-d border border-gray-200 dark:border-primary/20 rounded-xl shadow-xl z-50 py-3"
        >
            <div class="px-4 py-2 text-xs font-semibold text-gray-500 dark:text-primary-dt/70 uppercase tracking-wider border-b border-gray-100 dark:border-primary/20 mb-2">
                Tamaños de página
            </div>
            
            <div class="max-h-72 overflow-y-auto">
                <button
                    v-for="size in pageSizes"
                    :key="size.name"
                    @click="selectSize(size)"
                    class="w-full flex items-center gap-4 px-4 py-3 hover:bg-gray-50 dark:hover:bg-primary/10 transition-colors text-left"
                    :class="{
                        'bg-blue-50 dark:bg-blue-900/20 border-l-4 border-blue-500': selectedSize.name === size.name
                    }"
                >
                    <div class="flex-shrink-0">
                        <div 
                            class="w-10 h-12 border-2 border-gray-300 dark:border-primary/30 rounded-md bg-white dark:bg-primary-d flex items-center justify-center shadow-sm"
                            :class="{
                                'border-blue-500 dark:border-blue-400 shadow-blue-200': selectedSize.name === size.name
                            }"
                        >
                            <div 
                                class="bg-gray-200 dark:bg-primary/20 rounded-sm"
                                :style="{
                                    width: `${Math.min(24, (size.width / size.height) * 36)}px`,
                                    height: `${Math.min(36, (size.height / size.width) * 24)}px`
                                }"
                                :class="{
                                    'bg-blue-200 dark:bg-blue-800': selectedSize.name === size.name
                                }"
                            ></div>
                        </div>
                    </div>
                    
                    <div class="flex-1 min-w-0">
                        <div class="font-semibold text-gray-900 dark:text-primary-dt text-base">{{ size.name }}</div>
                        <div class="text-sm text-gray-600 dark:text-primary-dt/80">{{ size.label.split('(')[1]?.replace(')', '') || size.label }}</div>
                        <div class="text-xs text-gray-500 dark:text-primary-dt/60 mt-1">
                            {{ size.description }}
                        </div>
                    </div>
                    
                    <div v-if="selectedSize.name === size.name" class="flex-shrink-0">
                        <div class="w-6 h-6 bg-blue-500 rounded-full flex items-center justify-center">
                            <GoogleIcon name="check" class="text-white text-sm" />
                        </div>
                    </div>
                </button>
            </div>
        </div>
    </div>
</template>