<script setup>
import Modal from '@Holos/Modal.vue';
import GoogleIcon from '@Shared/GoogleIcon.vue';

/** Props */
const props = defineProps({
    show: {
        type: Boolean,
        default: false
    },
    client: {
        type: Object,
        default: null
    }
});

/** Emits */
const emit = defineEmits(['close', 'confirm']);

/** Métodos */
const handleConfirm = () => {
    emit('confirm', props.client.id);
};

const handleClose = () => {
    emit('close');
};
</script>

<template>
    <Modal :show="show" max-width="md" @close="handleClose">
        <div class="p-6">
            <!-- Header -->
            <div class="flex items-center justify-between mb-6">
                <div class="flex items-center gap-3">
                    <div class="flex items-center justify-center w-12 h-12 rounded-full bg-red-100 dark:bg-red-900/30">
                        <GoogleIcon name="delete_forever" class="text-2xl text-red-600 dark:text-red-400" />
                    </div>
                    <h3 class="text-lg font-bold text-gray-900 dark:text-gray-100">
                        Eliminar Nivel de Cliente
                    </h3>
                </div>
                <button
                    @click="handleClose"
                    class="text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 transition-colors"
                >
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                    </svg>
                </button>
            </div>

            <!-- Content -->
            <div class="space-y-5">
                <p class="text-gray-700 dark:text-gray-300 text-base">
                    ¿Estás seguro de que deseas eliminar este nivel de cliente?
                </p>

                <div v-if="client" class="bg-gradient-to-br from-gray-50 to-gray-100 dark:from-gray-800 dark:to-gray-900 border border-gray-200 dark:border-gray-700 rounded-xl p-5 space-y-3">
                    <div class="flex items-start justify-between">
                        <div class="flex-1">
                            <p class="text-base font-bold text-gray-900 dark:text-gray-100 mb-1">
                                {{ client.tier_name }}
                            </p>
                            <div class="space-y-1 text-sm text-gray-600 dark:text-gray-400">
                                <p>Descuento: {{ parseFloat(client.discount_percentage).toFixed(2) }}%</p>
                                <p>Rango: ${{ parseFloat(client.min_purchase_amount).toFixed(2) }} - {{ client.max_purchase_amount ? '$' + parseFloat(client.max_purchase_amount).toFixed(2) : 'Sin límite' }}</p>
                                <p v-if="client.clients_count !== undefined" class="font-semibold" :class="client.clients_count > 0 ? 'text-amber-600 dark:text-amber-400' : 'text-gray-600 dark:text-gray-400'">
                                    Clientes asignados: {{ client.clients_count || 0 }}
                                </p>
                            </div>
                        </div>
                    </div>
                </div>

                <div v-if="client && client.clients_count > 0" class="flex items-start gap-2 bg-amber-50 dark:bg-amber-900/20 border border-amber-200 dark:border-amber-800 rounded-lg p-4">
                    <GoogleIcon name="warning" class="text-amber-600 dark:text-amber-400 text-xl flex-shrink-0 mt-0.5" />
                    <p class="text-sm text-amber-800 dark:text-amber-300 font-medium">
                        Este nivel tiene {{ client.clients_count }} cliente(s) asignado(s). No podrás eliminarlo hasta que todos los clientes sean reasignados a otro nivel.
                    </p>
                </div>

                <div class="flex items-start gap-2 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg p-4">
                    <GoogleIcon name="info" class="text-red-600 dark:text-red-400 text-xl flex-shrink-0 mt-0.5" />
                    <p class="text-sm text-red-800 dark:text-red-300 font-medium">
                        Esta acción es permanente y no se puede deshacer.
                    </p>
                </div>
            </div>

            <!-- Footer -->
            <div class="flex items-center justify-end gap-3 mt-8 pt-6 border-t border-gray-200 dark:border-gray-700">
                <button
                    type="button"
                    @click="handleClose"
                    class="px-5 py-2.5 text-sm font-semibold text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500 transition-colors"
                >
                    Cancelar
                </button>
                <button
                    type="button"
                    @click="handleConfirm"
                    :disabled="client && client.clients_count > 0"
                    class="flex items-center gap-2 px-5 py-2.5 bg-red-600 hover:bg-red-700 text-white text-sm font-semibold rounded-lg focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-600 shadow-lg shadow-red-600/30 transition-all disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:bg-red-600"
                >
                    <GoogleIcon name="delete" class="text-xl" />
                    Eliminar Nivel
                </button>
            </div>
        </div>
    </Modal>
</template>
