@csrf

<div class="row g-6">
  <div class="col-md-6">
    <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', $user->name ?? '') }}" required>
    @error('name')
      <div class="invalid-feedback">{{ $message }}</div>
    @enderror
  </div>

  <div class="col-md-6">
    <label class="form-label" for="email">Correo</label>
    <input type="email" id="email" name="email" class="form-control @error('email') is-invalid @enderror"
      value="{{ old('email', $user->email ?? '') }}" required>
    @error('email')
      <div class="invalid-feedback">{{ $message }}</div>
    @enderror
  </div>

  <div class="col-md-6">
    <label class="form-label" for="password">Contraseña {{ isset($user) ? '(dejar en blanco para mantener)' : '' }}</label>
    <input type="password" id="password" name="password" class="form-control @error('password') is-invalid @enderror"
      {{ isset($user) ? '' : 'required' }}>
    @error('password')
      <div class="invalid-feedback">{{ $message }}</div>
    @enderror
  </div>

  <div class="col-md-3">
    <label class="form-label" for="role">Rol</label>
    <select id="role" name="role" class="form-select @error('role') is-invalid @enderror" required>
      @foreach ($roles as $role)
        <option value="{{ $role }}" @selected(old('role', $user->role ?? '') === $role)>{{ $role }}</option>
      @endforeach
    </select>
    @error('role')
      <div class="invalid-feedback">{{ $message }}</div>
    @enderror
  </div>

  <div class="col-md-3">
    <label class="form-label" for="staff_role">Staff Role</label>
    <select id="staff_role" name="staff_role" class="form-select @error('staff_role') is-invalid @enderror">
      <option value="">No aplica</option>
      @foreach ($staffRoles as $staffRole)
        <option value="{{ $staffRole->code }}" @selected(old('staff_role', $user->staff_role ?? '') === $staffRole->code)>{{ $staffRole->name }} ({{ $staffRole->code }})</option>
      @endforeach
    </select>
    @error('staff_role')
      <div class="invalid-feedback">{{ $message }}</div>
    @enderror
  </div>

  <div class="col-md-3">
    <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', isset($user) ? (int) $user->is_active : 1) === '1')>Activo</option>
      <option value="0" @selected((string) old('is_active', isset($user) ? (int) $user->is_active : 1) === '0')>Inactivo</option>
    </select>
    @error('is_active')
      <div class="invalid-feedback">{{ $message }}</div>
    @enderror
  </div>

  <div class="col-md-6">
    <label class="form-label" for="mariachi_group_id">Grupo mariachi</label>
    <select id="mariachi_group_id" name="mariachi_group_id" class="form-select @error('mariachi_group_id') is-invalid @enderror">
      <option value="">Sin grupo mariachi</option>
      @foreach ($mariachiGroups as $mariachiGroup)
        <option value="{{ $mariachiGroup->id }}" @selected((string) old('mariachi_group_id', $user->mariachi_group_id ?? '') === (string) $mariachiGroup->id)>
          {{ $mariachiGroup->name }} ({{ $mariachiGroup->entity_type }})
        </option>
      @endforeach
    </select>
    @error('mariachi_group_id')
      <div class="invalid-feedback">{{ $message }}</div>
    @enderror
  </div>

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