@extends('layouts.app')
@section('titulo', $meta['titulo'])
@section('contenido')
@php $editando = !empty($registro->id ?? null); @endphp
<h4 class="mb-3"><i class="fa fa-pen-to-square"></i> {{ $editando ? 'Editar' : 'Nuevo' }} — {{ $meta['titulo'] }}</h4>
<form method="POST" action="{{ $editando ? route('catalogos.update', [$slug, $registro->id]) : route('catalogos.store', $slug) }}" class="card p-3">
  @csrf
  @if($editando) @method('PUT') @endif
  <div class="row g-3">
    @foreach($meta['campos'] as $c)
      @php
        [$nombre, $label, $tipo] = $c;
        $req = $c[3] ?? false;
        $max = $c[4] ?? null;
        $opciones = $c[5] ?? null;
        $val = old($nombre, $registro->{$nombre} ?? '');
      @endphp
      <div class="col-md-{{ $tipo==='textarea' ? 12 : 6 }}">
        <label class="form-label small fw-bold">{{ $label }} @if($req)<span class="text-danger">*</span>@endif</label>
        @if($tipo==='textarea')
          <textarea name="{{ $nombre }}" class="form-control" rows="3" {{ $req?'required':'' }}>{{ $val }}</textarea>
        @elseif($tipo==='bool')
          <div class="form-check form-switch mt-2">
            <input type="hidden" name="{{ $nombre }}" value="0">
            <input type="checkbox" name="{{ $nombre }}" value="1" class="form-check-input" {{ ($val??1)?'checked':'' }}>
            <label class="form-check-label">Activo / habilitado</label>
          </div>
        @elseif($tipo==='select')
          <select name="{{ $nombre }}" class="form-select" {{ $req?'required':'' }}>
            <option value="">—</option>
            @foreach($opciones as $op)<option value="{{ $op }}" @selected($val===$op)>{{ ucfirst($op) }}</option>@endforeach
          </select>
        @elseif($tipo==='color')
          <input type="color" name="{{ $nombre }}" value="{{ $val ?: '#0d6efd' }}" class="form-control form-control-color">
        @elseif($tipo==='number')
          <input type="number" step="any" name="{{ $nombre }}" value="{{ $val }}" class="form-control" {{ $req?'required':'' }}>
        @else
          <input type="text" name="{{ $nombre }}" value="{{ $val }}" class="form-control" maxlength="{{ $max ?? 255 }}" {{ $req?'required':'' }}>
        @endif
      </div>
    @endforeach
  </div>
  <div class="mt-3 text-end">
    <a href="{{ route('catalogos.index', $slug) }}" class="btn btn-outline-secondary">Cancelar</a>
    <button class="btn btn-primario"><i class="fa fa-save"></i> Guardar</button>
  </div>
</form>
@endsection
