@extends('layouts.ownerLayout')

@section('title', 'Grupos de Mariachi')

@section('vendor-style')
@vite([
  'resources/assets/vendor/libs/datatables-bs5/datatables.bootstrap5.scss',
  'resources/assets/vendor/libs/datatables-responsive-bs5/responsive.bootstrap5.scss',
  'resources/assets/vendor/libs/datatables-buttons-bs5/buttons.bootstrap5.scss',
  'resources/assets/vendor/libs/notyf/notyf.scss',
  'resources/assets/vendor/libs/sweetalert2/sweetalert2.scss'
])
@endsection

@section('vendor-script')
@vite([
  'resources/assets/vendor/libs/datatables-bs5/datatables-bootstrap5.js',
  'resources/assets/vendor/libs/notyf/notyf.js',
  'resources/assets/vendor/libs/sweetalert2/sweetalert2.js'
])
@endsection

@section('page-script')
@vite('resources/assets/js/owner-mariachi-groups.js')
<script>
  window.__mariachiGroupsPageData = {
    canManage: @json($canManageGroups),
    openCreateModal: @json(old('form_type') === 'create'),
    openEditModal: @json(old('form_type') === 'edit'),
    editGroupId: @json(old('mariachi_group_id')),
    toast: {
      success: @json(session('status')),
      errors: @json($errors->all())
    }
  };
</script>
@endsection

@section('content')
@if (!$canManageGroups)
  <div class="alert alert-warning mb-6">
    Tu usuario no tiene organización asignada.
  </div>
@endif

<div class="card mb-6">
  <div class="card-widget-separator-wrapper">
    <div class="card-body card-widget-separator">
      <div class="row gy-4 gy-sm-1">
        <div class="col-sm-6 col-lg-4">
          <div class="d-flex justify-content-between align-items-start card-widget-1 border-end pb-4 pb-sm-0">
            <div>
              <p class="mb-1">Total grupos</p>
              <h4 class="mb-1">{{ number_format($stats['total']) }}</h4>
            </div>
            <span class="avatar me-sm-6">
              <span class="avatar-initial rounded bg-label-primary"><i class="icon-base ti tabler-music icon-28px"></i></span>
            </span>
          </div>
        </div>
        <div class="col-sm-6 col-lg-4">
          <div class="d-flex justify-content-between align-items-start card-widget-2 border-end pb-4 pb-sm-0">
            <div>
              <p class="mb-1">Activos</p>
              <h4 class="mb-1">{{ number_format($stats['active']) }}</h4>
            </div>
            <span class="avatar p-2 me-lg-6">
              <span class="avatar-initial rounded bg-label-success"><i class="icon-base ti tabler-checks icon-28px"></i></span>
            </span>
          </div>
        </div>
        <div class="col-sm-6 col-lg-4">
          <div class="d-flex justify-content-between align-items-start">
            <div>
              <p class="mb-1">Por defecto</p>
              <h4 class="mb-1">{{ number_format($stats['default']) }}</h4>
            </div>
            <span class="avatar p-2">
              <span class="avatar-initial rounded bg-label-info"><i class="icon-base ti tabler-star icon-28px"></i></span>
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="card">
  <div class="card-header border-bottom">
    <div class="d-flex flex-column flex-md-row align-items-md-center justify-content-between gap-4">
      <h5 class="card-title mb-0">Grupos musicales</h5>
      <button type="button" class="btn btn-primary" id="btnOpenCreateMariachiGroup" @disabled(!$canManageGroups)>
        <i class="icon-base ti tabler-plus icon-xs me-1"></i>
        Agregar grupo
      </button>
    </div>
  </div>
  <div class="card-datatable">
    <table class="datatables-owner-mariachi-groups table border-top">
      <thead>
        <tr>
          <th>ID</th>
          <th>Nombre</th>
          <th>Descripción</th>
          <th>Estado</th>
          <th>Por defecto</th>
          <th>Orden</th>
          <th>Acciones</th>
        </tr>
      </thead>
      <tbody>
        @foreach ($groups as $group)
          <tr>
            <td>{{ $group->id }}</td>
            <td class="fw-medium text-heading">{{ $group->name }}</td>
            <td>{{ $group->description ?: '-' }}</td>
            <td><span class="badge {{ $group->is_active ? 'bg-label-success' : 'bg-label-secondary' }}">{{ $group->is_active ? 'Activo' : 'Inactivo' }}</span></td>
            <td><span class="badge {{ $group->is_default ? 'bg-label-primary' : 'bg-label-secondary' }}">{{ $group->is_default ? 'Sí' : 'No' }}</span></td>
            <td>{{ $group->sort_order }}</td>
            <td>
              <div class="d-flex align-items-center">
                <a
                  href="{{ route('owner.settings.mariachi-groups.show', $group) }}"
                  class="btn btn-text-secondary rounded-pill waves-effect btn-icon"
                  title="Ver detalle">
                  <i class="icon-base ti tabler-eye icon-22px"></i>
                </a>
                <button
                  type="button"
                  class="btn btn-text-secondary rounded-pill waves-effect btn-icon btn-edit-mariachi-group"
                  data-id="{{ $group->id }}"
                  data-name="{{ $group->name }}"
                  data-description="{{ $group->description }}"
                  data-is-active="{{ $group->is_active ? 1 : 0 }}"
                  data-is-default="{{ $group->is_default ? 1 : 0 }}"
                  data-sort-order="{{ $group->sort_order }}"
                  data-update-url="{{ route('owner.settings.mariachi-groups.update', $group) }}"
                  title="Editar">
                  <i class="icon-base ti tabler-edit icon-22px"></i>
                </button>
                <form id="toggle-mariachi-group-form-{{ $group->id }}" action="{{ route('owner.settings.mariachi-groups.deactivate', $group) }}" method="POST">
                  @csrf
                  @method('PATCH')
                  <button
                    type="button"
                    class="btn btn-text-secondary rounded-pill waves-effect btn-icon btn-toggle-mariachi-group"
                    data-form-id="toggle-mariachi-group-form-{{ $group->id }}"
                    data-name="{{ $group->name }}"
                    data-next-state="{{ $group->is_active ? 'inactivar' : 'activar' }}"
                    title="{{ $group->is_active ? 'Inactivar' : 'Activar' }}">
                    <i class="icon-base ti {{ $group->is_active ? 'tabler-ban' : 'tabler-check' }} icon-22px"></i>
                  </button>
                </form>
              </div>
            </td>
          </tr>
        @endforeach
      </tbody>
    </table>
  </div>
</div>

<div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasCreateMariachiGroup">
  <div class="offcanvas-header border-bottom">
    <h5 class="offcanvas-title">Nuevo grupo</h5>
    <button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body mx-0 flex-grow-0 p-6 h-100">
    <form method="POST" action="{{ route('owner.settings.mariachi-groups.store') }}" class="h-100 d-flex flex-column">
      @csrf
      <input type="hidden" name="form_type" value="create">
      <div class="mb-4">
        <label class="form-label" for="create_group_name">Nombre</label>
        <input type="text" class="form-control" id="create_group_name" name="name" value="{{ old('form_type') === 'create' ? old('name') : '' }}" required>
      </div>
      <div class="mb-4">
        <label class="form-label" for="create_group_description">Descripción</label>
        <textarea class="form-control" id="create_group_description" name="description" rows="3">{{ old('form_type') === 'create' ? old('description') : '' }}</textarea>
        <small class="text-body-secondary d-block mt-1">El nuevo grupo copiará paquetes, obsequios, artistas y otros costos del grupo principal.</small>
      </div>
      <div class="mb-4">
        <label class="form-label" for="create_group_sort_order">Orden</label>
        <input type="number" min="0" class="form-control" id="create_group_sort_order" name="sort_order" value="{{ old('form_type') === 'create' ? old('sort_order', 100) : 100 }}">
      </div>
      <div class="mb-4">
        <label class="form-label" for="create_group_is_active">Estado</label>
        <select class="form-select" id="create_group_is_active" name="is_active">
          <option value="1" @selected(old('form_type') === 'create' ? old('is_active', '1') === '1' : true)>Activo</option>
          <option value="0" @selected(old('form_type') === 'create' && old('is_active') === '0')>Inactivo</option>
        </select>
      </div>
      <div class="mb-4">
        <label class="form-label" for="create_group_is_default">Por defecto</label>
        <select class="form-select" id="create_group_is_default" name="is_default">
          <option value="1" @selected(old('form_type') === 'create' && old('is_default') === '1')>Sí</option>
          <option value="0" @selected(old('form_type') === 'create' ? old('is_default', '0') === '0' : true)>No</option>
        </select>
      </div>
      <div class="mt-auto pt-4 d-flex gap-3">
        <button type="button" class="btn btn-label-secondary" data-bs-dismiss="offcanvas">Cancelar</button>
        <button type="submit" class="btn btn-primary" @disabled(!$canManageGroups)>Guardar</button>
      </div>
    </form>
  </div>
</div>

<div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasEditMariachiGroup">
  <div class="offcanvas-header border-bottom">
    <h5 class="offcanvas-title">Editar grupo</h5>
    <button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
  </div>
  <div class="offcanvas-body mx-0 flex-grow-0 p-6 h-100">
    <form id="editMariachiGroupForm" method="POST" action="#" class="h-100 d-flex flex-column">
      @csrf
      @method('PUT')
      <input type="hidden" name="form_type" value="edit">
      <input type="hidden" name="mariachi_group_id" id="edit_mariachi_group_id" value="{{ old('mariachi_group_id') }}">
      <div class="mb-4">
        <label class="form-label" for="edit_group_name">Nombre</label>
        <input type="text" class="form-control" id="edit_group_name" name="name" value="{{ old('form_type') === 'edit' ? old('name') : '' }}" required>
      </div>
      <div class="mb-4">
        <label class="form-label" for="edit_group_description">Descripción</label>
        <textarea class="form-control" id="edit_group_description" name="description" rows="3">{{ old('form_type') === 'edit' ? old('description') : '' }}</textarea>
      </div>
      <div class="mb-4">
        <label class="form-label" for="edit_group_sort_order">Orden</label>
        <input type="number" min="0" class="form-control" id="edit_group_sort_order" name="sort_order" value="{{ old('form_type') === 'edit' ? old('sort_order', 100) : 100 }}">
      </div>
      <div class="mb-4">
        <label class="form-label" for="edit_group_is_active">Estado</label>
        <select class="form-select" id="edit_group_is_active" name="is_active">
          <option value="1" @selected(old('form_type') === 'edit' ? old('is_active', '1') === '1' : true)>Activo</option>
          <option value="0" @selected(old('form_type') === 'edit' && old('is_active') === '0')>Inactivo</option>
        </select>
      </div>
      <div class="mb-4">
        <label class="form-label" for="edit_group_is_default">Por defecto</label>
        <select class="form-select" id="edit_group_is_default" name="is_default">
          <option value="1" @selected(old('form_type') === 'edit' && old('is_default') === '1')>Sí</option>
          <option value="0" @selected(old('form_type') === 'edit' ? old('is_default', '0') === '0' : true)>No</option>
        </select>
      </div>
      <div class="mt-auto pt-4 d-flex gap-3">
        <button type="button" class="btn btn-label-secondary" data-bs-dismiss="offcanvas">Cancelar</button>
        <button type="submit" class="btn btn-primary" @disabled(!$canManageGroups)>Actualizar</button>
      </div>
    </form>
  </div>
</div>
@endsection
