@extends('admin.layouts.app')
@section('title', isset($estadistica) ? 'Editar Estadística' : 'Nueva Estadística')
@section('styles')
<style>
:root{--m:#0d47a1;--mg:#1565c0;}
.mod-hero{background:linear-gradient(135deg,#0a2e6e,#1565c0,#0288d1);border-radius:20px;padding:22px 28px;display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:16px;margin-bottom:22px;box-shadow:0 8px 32px rgba(13,71,161,.28);}
.mod-hero h1{font-size:1.3rem;font-weight:800;color:#fff;margin:0;display:flex;align-items:center;gap:10px;}
.mod-hero p{color:rgba(255,255,255,.5);margin:0;font-size:.82rem;}
.btn-back{display:inline-flex;align-items:center;gap:7px;padding:10px 18px;background:rgba(255,255,255,.12);border:1.5px solid rgba(255,255,255,.3);color:#fff;border-radius:12px;font-weight:700;font-size:.85rem;text-decoration:none;transition:all .2s;}
.btn-back:hover{background:rgba(255,255,255,.22);}
.form-card{background:#fff;border-radius:18px;box-shadow:0 4px 24px rgba(0,0,0,.07);overflow:hidden;margin-bottom:20px;}
.form-card-hd{background:linear-gradient(135deg,var(--m),#0288d1);padding:14px 22px;display:flex;align-items:center;gap:10px;}
.form-card-hd .ic{width:32px;height:32px;border-radius:9px;background:rgba(255,255,255,.15);display:flex;align-items:center;justify-content:center;}
.form-card-hd .ic i{color:#ffe082;font-size:15px;}
.form-card-hd span{color:#fff;font-weight:800;font-size:.92rem;}
.form-card-body{padding:24px 28px;}
.fl{font-size:.82rem;font-weight:700;color:#475569;margin-bottom:6px;display:block;}
.fl .req{color:#ef4444;}
.fi{width:100%;border:1.5px solid #e2e8f0;border-radius:10px;padding:10px 14px;font-size:.88rem;color:#1e293b;transition:border-color .2s,box-shadow .2s;outline:none;background:#fff;}
.fi:focus{border-color:var(--m);box-shadow:0 0 0 3px rgba(13,71,161,.1);}
.fi-row{display:grid;grid-template-columns:1fr 1fr;gap:16px;margin-bottom:18px;}
.fi-row-3{display:grid;grid-template-columns:1fr 1fr 1fr;gap:16px;margin-bottom:18px;}
.fg{margin-bottom:18px;}
.toggle-lbl{display:flex;align-items:center;gap:10px;cursor:pointer;background:#fff;padding:10px 16px;border-radius:10px;border:1.5px solid #e2e8f0;transition:border-color .2s;width:fit-content;}
.toggle-lbl:hover{border-color:var(--m);}
.toggle-lbl input{width:18px;height:18px;cursor:pointer;accent-color:var(--m);}
.toggle-lbl span{font-weight:700;color:#334155;font-size:.88rem;}
.submit-bar{background:#f8faff;border-top:1.5px solid #f1f5f9;padding:18px 28px;display:flex;gap:12px;justify-content:flex-end;align-items:center;}
.btn-cancel{display:inline-flex;align-items:center;gap:7px;padding:11px 20px;background:#f1f5f9;color:#64748b;border-radius:12px;font-weight:700;text-decoration:none;font-size:.88rem;transition:all .2s;}
.btn-cancel:hover{background:#e2e8f0;}
.btn-save{display:inline-flex;align-items:center;gap:8px;padding:12px 28px;background:linear-gradient(135deg,#0d47a1,#0288d1);color:#fff;border:none;border-radius:14px;font-weight:800;font-size:.9rem;cursor:pointer;box-shadow:0 4px 16px rgba(13,71,161,.3);transition:all .2s;}
.btn-save:hover{transform:translateY(-2px);}
/* Preview */
.stat-preview{display:flex;align-items:center;gap:16px;padding:18px 22px;background:linear-gradient(135deg,#0d47a1,#0288d1);border-radius:14px;margin-bottom:4px;}
.stat-preview .sp-icon{width:52px;height:52px;border-radius:14px;display:flex;align-items:center;justify-content:center;flex-shrink:0;}
.stat-preview .sp-num{font-size:1.8rem;font-weight:900;color:#fff;line-height:1;}
.stat-preview .sp-lbl{font-size:.8rem;color:rgba(255,255,255,.65);margin-top:2px;}
</style>
@endsection
@section('content')
<div class="mod-hero">
    <div>
        <h1>
            <div style="width:38px;height:38px;border-radius:12px;background:rgba(255,255,255,.15);border:1.5px solid rgba(255,255,255,.25);display:flex;align-items:center;justify-content:center;">
                <i class="bi bi-bar-chart-fill" style="color:#ffe082;font-size:18px;"></i>
            </div>
            {{ isset($estadistica) ? 'Editar Estadística' : 'Nueva Estadística' }}
        </h1>
        <p>{{ isset($estadistica) ? 'Modifica los datos del contador' : 'Agrega un nuevo contador animado' }}</p>
    </div>
    <a href="{{ route('admin.estadisticas.index') }}" class="btn-back"><i class="bi bi-arrow-left"></i> Volver</a>
</div>

<div class="form-card">
    <div class="form-card-hd">
        <div class="ic"><i class="bi bi-sliders"></i></div>
        <span>Datos del Contador</span>
    </div>
    <div class="form-card-body">
        <form action="{{ isset($estadistica) ? route('admin.estadisticas.update', $estadistica) : route('admin.estadisticas.store') }}" method="POST" id="frmEst">
            @csrf @if(isset($estadistica)) @method('PUT') @endif

            {{-- Vista previa --}}
            <div class="stat-preview" id="spWrap" style="margin-bottom:20px;">
                <div class="sp-icon" id="spIcon" style="background:{{ old('color',$estadistica->color??'#3b82f6') }}40;">
                    <i class="bi {{ old('icono',$estadistica->icono??'bi-bar-chart') }}" id="spIconEl" style="font-size:24px;color:{{ old('color',$estadistica->color??'#3b82f6') }};"></i>
                </div>
                <div>
                    <div class="sp-num" id="spNum">{{ number_format(old('valor',$estadistica->valor??0)) }}<span id="spSuf" style="font-size:1rem;">{{ old('sufijo',$estadistica->sufijo??'+') }}</span></div>
                    <div class="sp-lbl" id="spLbl">{{ old('etiqueta',$estadistica->etiqueta??'Etiqueta') }}</div>
                </div>
            </div>

            <div class="fi-row">
                <div>
                    <label class="fl">Etiqueta <span class="req">*</span></label>
                    <input type="text" name="etiqueta" id="inEtq" class="fi" value="{{ old('etiqueta',$estadistica->etiqueta??'') }}" placeholder="Ej: Estudiantes Atendidos" required>
                </div>
                <div>
                    <label class="fl">Valor Numérico <span class="req">*</span></label>
                    <input type="number" name="valor" id="inVal" class="fi" value="{{ old('valor',$estadistica->valor??'') }}" placeholder="500" required>
                </div>
            </div>
            <div class="fi-row-3">
                <div>
                    <label class="fl">Sufijo</label>
                    <input type="text" name="sufijo" id="inSuf" class="fi" value="{{ old('sufijo',$estadistica->sufijo??'+') }}" placeholder="+ o %">
                </div>
                <div>
                    <label class="fl">Ícono Bootstrap</label>
                    <input type="text" name="icono" id="inIco" class="fi" value="{{ old('icono',$estadistica->icono??'bi-people-fill') }}" placeholder="bi-people-fill">
                    <div style="font-size:11px;color:#94a3b8;margin-top:4px;"><i class="bi bi-info-circle me-1"></i><a href="https://icons.getbootstrap.com" target="_blank" style="color:#0288d1;">icons.getbootstrap.com</a></div>
                </div>
                <div>
                    <label class="fl">Color</label>
                    <input type="color" name="color" id="inCol" class="fi" value="{{ old('color',$estadistica->color??'#3b82f6') }}" style="height:44px;padding:4px 8px;cursor:pointer;">
                </div>
            </div>
            <div class="fi-row">
                <div>
                    <label class="fl">Orden <span style="font-weight:400;color:#94a3b8;font-size:.78rem;">(0 = primero)</span></label>
                    <input type="number" name="orden" class="fi" value="{{ old('orden',$estadistica->orden??0) }}" min="0">
                </div>
                <div style="display:flex;align-items:flex-end;padding-bottom:2px;">
                    <label class="toggle-lbl">
                        <input type="checkbox" name="activo" value="1" {{ old('activo',$estadistica->activo??true) ? 'checked' : '' }}>
                        <i class="bi bi-toggle-on" style="color:#10b981;"></i>
                        <span>Activo / Visible</span>
                    </label>
                </div>
            </div>
        </form>
    </div>
    <div class="submit-bar">
        <a href="{{ route('admin.estadisticas.index') }}" class="btn-cancel"><i class="bi bi-x-lg"></i> Cancelar</a>
        <button type="submit" form="frmEst" class="btn-save"><i class="bi bi-save-fill"></i> {{ isset($estadistica) ? 'Actualizar' : 'Guardar' }}</button>
    </div>
</div>
@endsection
@section('scripts')
<script>
const inEtq=document.getElementById('inEtq'), inVal=document.getElementById('inVal'),
      inSuf=document.getElementById('inSuf'), inIco=document.getElementById('inIco'), inCol=document.getElementById('inCol');
const spLbl=document.getElementById('spLbl'), spNum=document.getElementById('spNum'),
      spSuf=document.getElementById('spSuf'), spIconEl=document.getElementById('spIconEl'), spIcon=document.getElementById('spIcon');
function refresh(){
    spLbl.textContent=inEtq.value||'Etiqueta';
    spNum.firstChild.textContent=Number(inVal.value||0).toLocaleString();
    spSuf.textContent=inSuf.value||'+';
    spIconEl.className='bi '+(inIco.value||'bi-bar-chart');
    spIconEl.style.color=inCol.value;
    spIcon.style.background=inCol.value+'40';
}
[inEtq,inVal,inSuf,inIco,inCol].forEach(el=>el.addEventListener('input',refresh));
</script>
@endsection
