@extends('layouts.app')

@section('title', 'Planes | Superadmin | Veltrix v2')

@push('styles')
<style>
:root {
    --pl-indigo:  #4f46e5;
    --pl-indigo2: #6366f1;
    --pl-green:   #10b981;
    --pl-red:     #ef4444;
    --pl-amber:   #f59e0b;
    --pl-gray:    #64748b;
    --pl-border:  #e2e8f0;
    --pl-bg:      #f8fafc;
    --pl-card:    #ffffff;
}

.pl-shell { background: var(--pl-bg); min-height: 100vh; }

/* Header */
.pl-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 28px 32px 0;
}
.pl-header-title { font-size: 1.35rem; font-weight: 700; color: #0f172a; }
.pl-header-sub   { font-size: .82rem; color: var(--pl-gray); margin-top: 2px; }

/* Grid */
.pl-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
    gap: 20px;
    padding: 24px 32px 40px;
}

/* Plan card */
.pl-card {
    background: var(--pl-card);
    border: 1px solid var(--pl-border);
    border-radius: 16px;
    overflow: hidden;
    transition: box-shadow .18s, transform .18s;
    position: relative;
}
.pl-card:hover { box-shadow: 0 8px 28px rgba(0,0,0,.09); transform: translateY(-2px); }
.pl-card.inactive { opacity: .7; }

.pl-card-top {
    padding: 22px 22px 16px;
    border-bottom: 1px solid var(--pl-border);
}
.pl-card-badge {
    display: inline-flex; align-items: center; gap: 5px;
    font-size: .7rem; font-weight: 600; letter-spacing: .04em; text-transform: uppercase;
    padding: 3px 10px; border-radius: 20px; margin-bottom: 10px;
}
.pl-card-badge.active   { background: #d1fae5; color: #065f46; }
.pl-card-badge.inactive { background: #fee2e2; color: #991b1b; }

.pl-card-name  { font-size: 1.15rem; font-weight: 700; color: #0f172a; margin: 0; }
.pl-card-slug  { font-size: .75rem; color: var(--pl-gray); font-family: monospace; }
.pl-card-price { font-size: 1.7rem; font-weight: 800; color: var(--pl-indigo); margin-top: 6px; }
.pl-card-price small { font-size: .8rem; font-weight: 500; color: var(--pl-gray); }

.pl-card-limits {
    display: flex; gap: 12px; flex-wrap: wrap; margin-top: 10px;
}
.pl-limit-chip {
    display: inline-flex; align-items: center; gap: 4px;
    background: #f1f5f9; border-radius: 6px;
    font-size: .72rem; color: #475569; padding: 3px 8px; font-weight: 500;
}

/* Modules */
.pl-card-body  { padding: 16px 22px; }
.pl-mod-title  { font-size: .72rem; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--pl-gray); margin-bottom: 10px; }
.pl-mod-chips  { display: flex; flex-wrap: wrap; gap: 6px; }
.pl-mod-chip {
    display: inline-flex; align-items: center; gap: 5px;
    background: #eef2ff; color: var(--pl-indigo); border-radius: 20px;
    font-size: .72rem; font-weight: 500; padding: 3px 10px;
}
.pl-mod-chip i { font-size: .85rem; }

/* Promos */
.pl-promo-row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.pl-promo-chip {
    background: #fef3c7; color: #92400e; border-radius: 20px;
    font-size: .7rem; font-weight: 600; padding: 3px 10px;
}

/* Actions */
.pl-card-foot {
    display: flex; gap: 8px;
    padding: 12px 22px 18px;
    border-top: 1px solid var(--pl-border);
}
.pl-btn-edit {
    flex: 1; text-align: center; font-size: .8rem; font-weight: 600;
    padding: 7px 14px; border-radius: 8px;
    background: #eef2ff; color: var(--pl-indigo);
    text-decoration: none; transition: background .15s;
}
.pl-btn-edit:hover { background: #e0e7ff; color: var(--pl-indigo); }

.pl-btn-del {
    font-size: .8rem; font-weight: 600; padding: 7px 12px; border-radius: 8px;
    background: #fee2e2; color: #b91c1c; border: none; cursor: pointer;
    transition: background .15s;
}
.pl-btn-del:hover { background: #fecaca; }

.pl-subs-badge {
    display: inline-flex; align-items: center; gap: 4px;
    font-size: .72rem; font-weight: 600; color: #475569;
    background: #f1f5f9; padding: 3px 10px; border-radius: 20px;
}

/* Empty state */
.pl-empty {
    text-align: center; padding: 80px 32px;
    color: var(--pl-gray); grid-column: 1 / -1;
}
.pl-empty i { font-size: 3rem; color: #cbd5e1; display: block; margin-bottom: 12px; }
</style>
@endpush

@section('topbar_actions')
    <a href="{{ route('superadmin.planes.create', [], false) }}" class="btn btn-primary btn-sm vx-topbar-action">
        <i class="bx bx-plus me-1"></i>Nuevo plan
    </a>
    <a href="{{ route('superadmin.index', [], false) }}" class="btn btn-outline-secondary btn-sm vx-topbar-action">
        <i class="bx bx-arrow-back me-1"></i>Superadmin
    </a>
@endsection

@section('content')
<div class="pl-shell">
    <div class="pl-header">
        <div>
            <div class="pl-header-title">Planes de suscripción</div>
            <div class="pl-header-sub">{{ $planes->count() }} planes · gestiona módulos, precios y promociones</div>
        </div>
        <a href="{{ route('precios', [], false) }}" target="_blank" class="btn btn-outline-indigo btn-sm d-flex align-items-center gap-1" style="border-color:#4f46e5;color:#4f46e5">
            <i class="bx bx-link-external"></i> Ver página pública
        </a>
    </div>

    @if (session('success'))
        <div class="mx-4 mt-4 alert alert-success">{{ session('success') }}</div>
    @endif
    @if (session('error'))
        <div class="mx-4 mt-4 alert alert-danger">{{ session('error') }}</div>
    @endif

    <div class="pl-grid">
        @forelse ($planes as $plan)
            @php
                $activoModulos = $plan->modulos->pluck('modulo')->all();
                $promos = $plan->promociones->where('activo', true);
            @endphp
            <div class="pl-card {{ $plan->activo ? '' : 'inactive' }}">
                <div class="pl-card-top">
                    <div class="d-flex align-items-start justify-content-between">
                        <div>
                            <span class="pl-card-badge {{ $plan->activo ? 'active' : 'inactive' }}">
                                <i class="bx {{ $plan->activo ? 'bxs-circle' : 'bx-circle' }}"></i>
                                {{ $plan->activo ? 'Activo' : 'Inactivo' }}
                            </span>
                            <div class="pl-card-name">{{ $plan->nombre }}</div>
                            <div class="pl-card-slug">{{ $plan->slug }}</div>
                        </div>
                        <span class="pl-subs-badge">
                            <i class="bx bxs-buildings"></i>
                            {{ $plan->suscripciones_count }} empresa{{ $plan->suscripciones_count !== 1 ? 's' : '' }}
                        </span>
                    </div>

                    <div class="pl-card-price">
                        {{ number_format((float) $plan->precio_mensual, 0, ',', '.') === '0' ? 'Gratis' : '$'.number_format((float) $plan->precio_mensual, 0, ',', '.') }}
                        @if ((float) $plan->precio_mensual > 0)
                            <small>/mes</small>
                        @endif
                    </div>

                    <div class="pl-card-limits">
                        <span class="pl-limit-chip"><i class="bx bxs-user"></i>{{ $plan->max_usuarios }} usuario{{ $plan->max_usuarios !== 1 ? 's' : '' }}</span>
                        <span class="pl-limit-chip"><i class="bx bxs-building"></i>{{ $plan->max_sucursales }} sucursal{{ $plan->max_sucursales !== 1 ? 'es' : '' }}</span>
                        <span class="pl-limit-chip"><i class="bx bxs-box"></i>{{ number_format($plan->max_productos) }} productos</span>
                    </div>
                </div>

                <div class="pl-card-body">
                    <div class="pl-mod-title">Módulos ({{ count($activoModulos) }})</div>
                    <div class="pl-mod-chips">
                        @forelse ($activoModulos as $mod)
                            @php $meta = $todosModulos[$mod] ?? null; @endphp
                            @if ($meta)
                                <span class="pl-mod-chip">
                                    <i class="bx {{ $meta['icono'] }}"></i>
                                    {{ $meta['label'] }}
                                </span>
                            @endif
                        @empty
                            <span style="font-size:.78rem;color:#94a3b8">Sin módulos asignados</span>
                        @endforelse
                    </div>

                    @if ($promos->isNotEmpty())
                        <div class="pl-promo-row">
                            @foreach ($promos as $promo)
                                <span class="pl-promo-chip">
                                    <i class="bx bxs-tag-alt me-1"></i>
                                    {{ $promo->meses }} mes{{ $promo->meses !== 1 ? 'es' : '' }} — {{ number_format((float)$promo->descuento_porcentaje, 0) }}% dto.
                                </span>
                            @endforeach
                        </div>
                    @endif
                </div>

                <div class="pl-card-foot">
                    <a href="{{ route('superadmin.planes.edit', $plan, false) }}" class="pl-btn-edit">
                        <i class="bx bx-edit-alt me-1"></i>Editar
                    </a>
                    @if ($plan->suscripciones_count === 0)
                        <form method="POST" action="{{ route('superadmin.planes.destroy', $plan) }}" onsubmit="return confirm('¿Eliminar el plan {{ addslashes($plan->nombre) }}?')">
                            @csrf @method('DELETE')
                            <button type="submit" class="pl-btn-del"><i class="bx bx-trash"></i></button>
                        </form>
                    @endif
                </div>
            </div>
        @empty
            <div class="pl-empty">
                <i class="bx bx-layer"></i>
                <div style="font-weight:600;font-size:1rem;color:#475569;margin-bottom:6px">No hay planes creados</div>
                <a href="{{ route('superadmin.planes.create') }}" class="btn btn-primary btn-sm mt-2">
                    <i class="bx bx-plus me-1"></i>Crear primer plan
                </a>
            </div>
        @endforelse
    </div>
</div>
@endsection
