@extends('layouts.adminLayout')

@section('title', 'Equipo')

@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/@form-validation/form-validation.scss',
  'resources/assets/vendor/libs/animate-css/animate.scss',
  'resources/assets/vendor/libs/sweetalert2/sweetalert2.scss',
  'resources/assets/vendor/libs/notyf/notyf.scss'
])
@endsection

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

@section('page-script')
  @vite('resources/assets/js/admin-team.js')
  <script>
    window.__openAddTeamUser = @json($errors->any());
    window.__toastData = {
      success: @json(session('status')),
      errors: @json($errors->all())
    };
    window.__swalData = {
      success: @json(session('swal_success'))
    };
  </script>
@endsection

@section('content')
<div class="row g-6 mb-6">
  <div class="col-sm-6 col-xl-3">
    <div class="card">
      <div class="card-body">
        <div class="d-flex align-items-start justify-content-between">
          <div class="content-left">
            <span class="text-heading">Total equipo</span>
            <div class="d-flex align-items-center my-1">
              <h4 class="mb-0 me-2">{{ number_format($stats['total']) }}</h4>
            </div>
            <small class="mb-0">Usuarios internos</small>
          </div>
          <div class="avatar">
            <span class="avatar-initial rounded bg-label-primary">
              <i class="icon-base ti tabler-users icon-26px"></i>
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="col-sm-6 col-xl-3">
    <div class="card">
      <div class="card-body">
        <div class="d-flex align-items-start justify-content-between">
          <div class="content-left">
            <span class="text-heading">Activos</span>
            <div class="d-flex align-items-center my-1">
              <h4 class="mb-0 me-2">{{ number_format($stats['active']) }}</h4>
            </div>
            <small class="mb-0">Con acceso al panel</small>
          </div>
          <div class="avatar">
            <span class="avatar-initial rounded bg-label-success">
              <i class="icon-base ti tabler-user-check icon-26px"></i>
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="col-sm-6 col-xl-3">
    <div class="card">
      <div class="card-body">
        <div class="d-flex align-items-start justify-content-between">
          <div class="content-left">
            <span class="text-heading">Inactivos</span>
            <div class="d-flex align-items-center my-1">
              <h4 class="mb-0 me-2">{{ number_format($stats['inactive']) }}</h4>
            </div>
            <small class="mb-0">Sin acceso</small>
          </div>
          <div class="avatar">
            <span class="avatar-initial rounded bg-label-danger">
              <i class="icon-base ti tabler-user-x icon-26px"></i>
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="col-sm-6 col-xl-3">
    <div class="card">
      <div class="card-body">
        <div class="d-flex align-items-start justify-content-between">
          <div class="content-left">
            <span class="text-heading">Administradores</span>
            <div class="d-flex align-items-center my-1">
              <h4 class="mb-0 me-2">{{ number_format($stats['admins']) }}</h4>
            </div>
            <small class="mb-0">Rol ADMIN</small>
          </div>
          <div class="avatar">
            <span class="avatar-initial rounded bg-label-warning">
              <i class="icon-base ti tabler-shield-check icon-26px"></i>
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="card">
  <div class="card-header border-bottom">
    <h5 class="card-title mb-0">Filtros</h5>
    <div class="d-flex justify-content-between align-items-center row pt-4 gap-4 gap-md-0">
      <div class="col-md-6 team_role"></div>
      <div class="col-md-6 team_status"></div>
    </div>
  </div>

  <div class="card-datatable">
    <table class="datatables-team table border-top">
      <thead>
        <tr>
          <th>ID</th>
          <th>Usuario</th>
          <th>Tipo acceso</th>
          <th>Rol del equipo</th>
          <th>Estado</th>
          <th>Acciones</th>
        </tr>
      </thead>
      <tbody>
        @foreach ($users as $user)
          <tr>
            <td>{{ $user->id }}</td>
            <td>
              <div class="d-flex flex-column">
                <span class="fw-medium text-heading">{{ $user->name }}</span>
                <small>{{ $user->email }}</small>
              </div>
            </td>
            <td>
              <span class="text-truncate d-flex align-items-center text-heading">
                @if ($user->role === 'ADMIN')
                  <i class="icon-base ti tabler-shield text-danger me-2"></i>
                @else
                  <i class="icon-base ti tabler-users text-primary me-2"></i>
                @endif
                {{ $user->role }}
              </span>
            </td>
            <td>
              @if (!empty($user->current_role_name))
                <span class="badge bg-label-info">{{ $user->current_role_name }}</span>
              @else
                <span class="text-body-secondary">No aplica</span>
              @endif
            </td>
            <td>
              <span class="badge {{ $user->is_active ? 'bg-label-success' : 'bg-label-secondary' }}">
                {{ $user->is_active ? 'Activo' : 'Inactivo' }}
              </span>
            </td>
            <td>
              @if ($user->is_active)
                <form action="{{ route('admin.team.deactivate', $user) }}" method="POST">
                  @csrf
                  @method('PATCH')
                  <button type="submit" class="btn btn-text-secondary rounded-pill waves-effect btn-icon btn-deactivate-team-user" title="Desactivar">
                    <i class="icon-base ti tabler-user-x icon-22px"></i>
                  </button>
                </form>
              @endif
            </td>
          </tr>
        @endforeach
      </tbody>
    </table>
  </div>

  <div class="offcanvas offcanvas-end" tabindex="-1" id="offcanvasAddTeamUser" aria-labelledby="offcanvasAddTeamUserLabel">
    <div class="offcanvas-header border-bottom">
      <h5 id="offcanvasAddTeamUserLabel" class="offcanvas-title">Agregar miembro</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 class="add-new-user pt-0" id="addNewTeamUserForm" method="POST" action="{{ route('admin.team.store') }}">
        @csrf

        <div class="mb-6 form-control-validation">
          <label class="form-label" for="team_name">Nombre</label>
          <input type="text" class="form-control" id="team_name" name="name" placeholder="Nombre completo" value="{{ old('name') }}" required>
        </div>

        <div class="mb-6 form-control-validation">
          <label class="form-label" for="team_email">Correo</label>
          <input type="email" id="team_email" class="form-control" placeholder="correo@empresa.com" name="email" value="{{ old('email') }}" required>
        </div>

        <div class="mb-6 form-control-validation">
          <label class="form-label" for="team_password">Contraseña</label>
          <input type="password" id="team_password" class="form-control" name="password" placeholder="Mínimo 8 caracteres" required>
        </div>

        <div class="mb-6 form-control-validation">
          <label class="form-label" for="team_staff_role">Rol del equipo</label>
          <select id="team_staff_role" class="form-select" name="staff_role">
            @forelse ($staffRoles as $staffRole)
              <option value="{{ $staffRole->code }}" @selected(old('staff_role') === $staffRole->code)>{{ $staffRole->name }} ({{ $staffRole->code }})</option>
            @empty
              <option value="">No hay roles disponibles</option>
            @endforelse
          </select>
        </div>

        <div class="mb-6">
          <label class="form-label" for="team_is_active">Estado</label>
          <select id="team_is_active" class="form-select" name="is_active" 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>
        </div>

        @if ($staffRoles->isEmpty())
          <small class="d-block text-warning mb-3">Debes crear un Staff Role en Roles antes de agregar miembros del equipo.</small>
        @endif

        <button type="submit" class="btn btn-primary me-3" @disabled($staffRoles->isEmpty())>Guardar</button>
        <button type="reset" class="btn btn-label-danger" data-bs-dismiss="offcanvas">Cancelar</button>
      </form>
    </div>
  </div>
</div>
@endsection
