@extends('layouts.adminLayout')

@section('title', 'Crear Grupo Mariachi')

@section('content')
  <div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
      <div>
        <h4 class="mb-1">Crear grupo mariachi</h4>
        <p class="mb-0 text-body-secondary">Paso sugerido: crear usuario y luego asociarlo a un grupo mariachi.</p>
      </div>
      <a href="{{ route('admin.mariachi-groups.index') }}" class="btn btn-label-secondary">Atrás</a>
    </div>

    <div class="card-body">
      @if ($errors->any())
        <div class="alert alert-danger">
          <strong>Corrige los errores del formulario.</strong>
        </div>
      @endif

      <form method="POST" action="{{ route('admin.mariachi-groups.store') }}">
        @csrf

        <div class="row g-6">
          <div class="col-12">
            <h5 class="mb-1">Detalles del grupo mariachi</h5>
            <p class="text-body-secondary mb-0">Define el grupo de trabajo para dueños o empresas de mariachis.</p>
          </div>

          <div class="col-md-12">
            <label class="form-label" for="name">Nombre</label>
            <input type="text" id="name" name="name" class="form-control @error('name') is-invalid @enderror" value="{{ old('name') }}" required>
            @error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
          </div>

          <div class="col-md-6">
            <label class="form-label" for="contact_phone">Teléfono</label>
            <input type="text" id="contact_phone" name="contact_phone" class="form-control @error('contact_phone') is-invalid @enderror" value="{{ old('contact_phone') }}">
            @error('contact_phone')<div class="invalid-feedback">{{ $message }}</div>@enderror
          </div>

          <div class="col-md-6">
            <label class="form-label" for="country_code">País</label>
            <select id="country_code" name="country_code" class="form-select @error('country_code') is-invalid @enderror" required>
              <option value="CO" @selected((string) old('country_code', 'CO') === 'CO')>Colombia (CO)</option>
              <option value="ES" @selected((string) old('country_code') === 'ES')>España (ES)</option>
              <option value="US" @selected((string) old('country_code') === 'US')>Estados Unidos (US)</option>
              <option value="MX" @selected((string) old('country_code') === 'MX')>México (MX)</option>
              <option value="AR" @selected((string) old('country_code') === 'AR')>Argentina (AR)</option>
              <option value="CL" @selected((string) old('country_code') === 'CL')>Chile (CL)</option>
              <option value="PE" @selected((string) old('country_code') === 'PE')>Perú (PE)</option>
            </select>
            @error('country_code')<div class="invalid-feedback">{{ $message }}</div>@enderror
          </div>

          <div class="col-md-6">
            <label class="form-label" for="city">Ciudad</label>
            <input type="text" id="city" name="city" class="form-control @error('city') is-invalid @enderror" value="{{ old('city') }}">
            @error('city')<div class="invalid-feedback">{{ $message }}</div>@enderror
          </div>

          <div class="col-12">
            <label class="form-label" for="notes">Notas</label>
            <textarea id="notes" name="notes" rows="3" class="form-control @error('notes') is-invalid @enderror">{{ old('notes') }}</textarea>
            @error('notes')<div class="invalid-feedback">{{ $message }}</div>@enderror
          </div>

          <div class="col-12"><hr class="my-0"></div>

          <div class="col-12">
            <h5 class="mb-1">Asociar usuario (opcional)</h5>
            <p class="text-body-secondary mb-0">Si ya creaste el usuario, puedes asociarlo en este paso.</p>
          </div>

          <div class="col-md-8">
            <label class="form-label" for="owner_user_id">Usuario</label>
            <select id="owner_user_id" name="owner_user_id" class="form-select @error('owner_user_id') is-invalid @enderror">
              <option value="">Sin asociar por ahora</option>
              @foreach ($users as $user)
                <option value="{{ $user->id }}" @selected((string) old('owner_user_id') === (string) $user->id)>
                  {{ $user->name }} ({{ $user->email }})
                </option>
              @endforeach
            </select>
            @error('owner_user_id')<div class="invalid-feedback">{{ $message }}</div>@enderror
          </div>

          <div class="col-md-4">
            <label class="form-label" for="is_active">Estado</label>
            <select id="is_active" name="is_active" class="form-select @error('is_active') is-invalid @enderror" required>
              <option value="1" @selected((string) old('is_active', '1') === '1')>Activo</option>
              <option value="0" @selected((string) old('is_active', '1') === '0')>Inactivo</option>
            </select>
            @error('is_active')<div class="invalid-feedback">{{ $message }}</div>@enderror
          </div>

          <div class="col-12 d-flex gap-3">
            <button type="submit" class="btn btn-primary">Guardar grupo mariachi</button>
            <a href="{{ route('admin.mariachi-groups.index') }}" class="btn btn-label-secondary">Cancelar</a>
          </div>
        </div>
      </form>
    </div>
  </div>
@endsection
