@extends('layouts.ownerLayout')

@section('title', 'Externos')

@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-external-partners.js')
<script>
  window.__externalPartnersPageData = {
    canManage: @json($canManageExternalPartners),
    openCreateModal: @json(old('form_type') === 'create'),
    openEditModal: @json(old('form_type') === 'edit'),
    editItemId: @json(old('item_id')),
    toast: {
      success: @json(session('status')),
      errors: @json($errors->all())
    }
  };
</script>
@endsection

@section('content')
@if (!$canManageExternalPartners)
  <div class="alert alert-warning mb-6">
    Tu usuario no tiene organización asignada. Solicita al administrador asociarte a una organización para gestionar externos.
  </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 externos</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-users-group 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">Inactivos</p>
              <h4 class="mb-1">{{ number_format($stats['inactive']) }}</h4>
            </div>
            <span class="avatar p-2">
              <span class="avatar-initial rounded bg-label-warning"><i class="icon-base ti tabler-ban 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">Externos para serenatas cedidas</h5>
      <button type="button" class="btn btn-primary" id="btnOpenCreateExternalPartner" @disabled(!$canManageExternalPartners)>
        <i class="icon-base ti tabler-plus icon-xs me-1"></i>
        Agregar externo
      </button>
    </div>
  </div>
  <div class="card-datatable">
    <table class="datatables-owner-external-partners table border-top">
      <thead>
        <tr>
          <th>Nombre</th>
          <th>Contacto</th>
          <th>Teléfono</th>
          <th>Ciudad</th>
          <th>Estado</th>
          <th>Orden</th>
          <th>Acciones</th>
        </tr>
      </thead>
      <tbody>
        @foreach ($items as $item)
          <tr>
            <td class="fw-medium text-heading">{{ $item->name }}</td>
            <td>{{ $item->contact_name ?: '-' }}</td>
            <td>{{ $item->formatted_phone ?: '-' }}</td>
            <td>{{ $item->city ?: '-' }}</td>
            <td>
              <span class="badge {{ $item->is_active ? 'bg-label-info' : 'bg-label-secondary' }}">
                {{ $item->is_active ? 'Activo' : 'Inactivo' }}
              </span>
            </td>
            <td>{{ $item->sort_order }}</td>
            <td>
              <div class="d-flex align-items-center">
                <a
                  href="{{ route('owner.settings.external-partners.show', $item) }}"
                  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-external-partner"
                  title="Editar"
                  data-id="{{ $item->id }}"
                  data-name="{{ $item->name }}"
                  data-contact-name="{{ $item->contact_name }}"
                  data-phone="{{ $item->phone }}"
                  data-email="{{ $item->email }}"
                  data-city="{{ $item->city }}"
                  data-notes="{{ $item->notes }}"
                  data-is-active="{{ $item->is_active ? 1 : 0 }}"
                  data-sort-order="{{ $item->sort_order }}"
                  data-update-url="{{ route('owner.settings.external-partners.update', $item) }}"
                >
                  <i class="icon-base ti tabler-edit icon-22px"></i>
                </button>
                <form id="toggle-external-partner-form-{{ $item->id }}" action="{{ route('owner.settings.external-partners.deactivate', $item) }}" method="POST">
                  @csrf
                  @method('PATCH')
                  <button
                    type="button"
                    class="btn btn-text-secondary rounded-pill waves-effect btn-icon btn-toggle-external-partner"
                    data-form-id="toggle-external-partner-form-{{ $item->id }}"
                    data-name="{{ $item->name }}"
                    data-next-state="{{ $item->is_active ? 'inactivar' : 'activar' }}"
                    title="{{ $item->is_active ? 'Inactivar' : 'Activar' }}"
                  >
                    <i class="icon-base ti {{ $item->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="offcanvasCreateExternalPartner" aria-labelledby="offcanvasCreateExternalPartnerLabel">
  <div class="offcanvas-header border-bottom">
    <h5 id="offcanvasCreateExternalPartnerLabel" class="offcanvas-title">Nuevo externo</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="createExternalPartnerForm" method="POST" action="{{ route('owner.settings.external-partners.store') }}" class="h-100 d-flex flex-column">
      @csrf
      <input type="hidden" name="form_type" value="create">
      <div class="mb-3">
        <label class="form-label" for="create_name">Nombre</label>
        <input type="text" class="form-control" id="create_name" name="name" value="{{ old('form_type') === 'create' ? old('name') : '' }}" required>
      </div>
      <div class="mb-3">
        <label class="form-label" for="create_contact_name">Contacto</label>
        <input type="text" class="form-control" id="create_contact_name" name="contact_name" value="{{ old('form_type') === 'create' ? old('contact_name') : '' }}">
      </div>
      <div class="mb-3">
        <label class="form-label" for="create_phone">Teléfono</label>
        <input type="text" class="form-control" id="create_phone" name="phone" value="{{ old('form_type') === 'create' ? old('phone') : '' }}">
      </div>
      <div class="mb-3">
        <label class="form-label" for="create_email">Correo</label>
        <input type="email" class="form-control" id="create_email" name="email" value="{{ old('form_type') === 'create' ? old('email') : '' }}">
      </div>
      <div class="mb-3">
        <label class="form-label" for="create_city">Ciudad</label>
        <input type="text" class="form-control" id="create_city" name="city" value="{{ old('form_type') === 'create' ? old('city') : '' }}">
      </div>
      <div class="mb-3">
        <label class="form-label" for="create_sort_order">Orden</label>
        <input type="number" min="0" class="form-control" id="create_sort_order" name="sort_order" value="{{ old('form_type') === 'create' ? old('sort_order', 100) : 100 }}">
      </div>
      <div class="mb-3">
        <label class="form-label" for="create_is_active">Estado</label>
        <select class="form-select" id="create_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-3">
        <label class="form-label" for="create_notes">Notas</label>
        <textarea class="form-control" id="create_notes" name="notes" rows="3">{{ old('form_type') === 'create' ? old('notes') : '' }}</textarea>
      </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(!$canManageExternalPartners)>Guardar</button>
      </div>
    </form>
  </div>
</div>

<div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasEditExternalPartner" aria-labelledby="offcanvasEditExternalPartnerLabel">
  <div class="offcanvas-header border-bottom">
    <h5 id="offcanvasEditExternalPartnerLabel" class="offcanvas-title">Editar externo</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="editExternalPartnerForm" 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="item_id" id="edit_item_id" value="{{ old('item_id') }}">
      <div class="mb-3">
        <label class="form-label" for="edit_name">Nombre</label>
        <input type="text" class="form-control" id="edit_name" name="name" value="{{ old('form_type') === 'edit' ? old('name') : '' }}" required>
      </div>
      <div class="mb-3">
        <label class="form-label" for="edit_contact_name">Contacto</label>
        <input type="text" class="form-control" id="edit_contact_name" name="contact_name" value="{{ old('form_type') === 'edit' ? old('contact_name') : '' }}">
      </div>
      <div class="mb-3">
        <label class="form-label" for="edit_phone">Teléfono</label>
        <input type="text" class="form-control" id="edit_phone" name="phone" value="{{ old('form_type') === 'edit' ? old('phone') : '' }}">
      </div>
      <div class="mb-3">
        <label class="form-label" for="edit_email">Correo</label>
        <input type="email" class="form-control" id="edit_email" name="email" value="{{ old('form_type') === 'edit' ? old('email') : '' }}">
      </div>
      <div class="mb-3">
        <label class="form-label" for="edit_city">Ciudad</label>
        <input type="text" class="form-control" id="edit_city" name="city" value="{{ old('form_type') === 'edit' ? old('city') : '' }}">
      </div>
      <div class="mb-3">
        <label class="form-label" for="edit_sort_order">Orden</label>
        <input type="number" min="0" class="form-control" id="edit_sort_order" name="sort_order" value="{{ old('form_type') === 'edit' ? old('sort_order', 100) : 100 }}">
      </div>
      <div class="mb-3">
        <label class="form-label" for="edit_is_active">Estado</label>
        <select class="form-select" id="edit_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-3">
        <label class="form-label" for="edit_notes">Notas</label>
        <textarea class="form-control" id="edit_notes" name="notes" rows="3">{{ old('form_type') === 'edit' ? old('notes') : '' }}</textarea>
      </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(!$canManageExternalPartners)>Actualizar</button>
      </div>
    </form>
  </div>
</div>
@endsection
