@extends('layouts.backend')

@section('title', 'Productos')

@section('content')
    <!-- Page Header -->
    <div class="page-header">
        <div class="row align-items-end">
            <div class="col-sm mb-2 mb-sm-0">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb breadcrumb-no-gutter">
                        <li class="breadcrumb-item"><a class="breadcrumb-link" href="javascript:;">eCommerce</a></li>
                        <li class="breadcrumb-item active" aria-current="page">@yield('title')</li>
                    </ol>
                </nav>

                <h1 class="page-header-title">@yield('title')</h1>
            </div>

            <div class="col-sm-auto">
                <a class="btn btn-primary" href="{{ route('backend.products.create') }}">
                    <i class="tio-shopping-basket-add mr-1"></i> Crear producto
                </a>
            </div>
        </div>
        <!-- End Row -->
    </div>
    <!-- End Page Header -->

    @if($products_total)
    <!-- Stats -->
    <div class="row gx-2 gx-lg-3">
        <div class="col-sm-6 mb-3 mb-lg-5">
            <!-- Card -->
            <div class="card h-100">
                <div class="card-body">
                    <h6 class="card-subtitle mb-2">Total de productos</h6>

                    <div class="row align-items-center gx-2">
                        <div class="col">
                            <span class="js-counter display-4 text-dark">{{ $products_total }}</span>
                        </div>
                    </div>
                    <!-- End Row -->
                </div>
            </div>
            <!-- End Card -->
        </div>

        <div class="col-sm-6 mb-3 mb-lg-5">
            <!-- Card -->
            <div class="card h-100">
                <div class="card-body">
                    <h6 class="card-subtitle mb-2">Productos activos</h6>

                    <div class="row align-items-center gx-2">
                        <div class="col">
                            <span class="js-counter display-4 text-dark">{{ $products_active }}</span>
                            <span class="text-body font-size-sm ml-1">de {{ $products_total }}</span>
                        </div>

                        <div class="col-auto">
                            <span class="badge badge-soft-success p-1">
                              <i class="tio-trending-up"></i> {{ number_format($products_active / $products_total * 100, 1, ',', '.') }}%
                            </span>
                        </div>
                    </div>
                    <!-- End Row -->
                </div>
            </div>
            <!-- End Card -->
        </div>

    </div>
    <!-- End Stats -->
    @endif
    <!-- Card -->
    <div class="card">
        <!-- Header -->
        <div class="card-header">
            <div class="row justify-content-between align-items-center flex-grow-1">
                <div class="col-sm-6 col-md-4 mb-3 mb-sm-0">
                {!! Form::open([
                    'method' => 'get'
                ]) !!}
                <!-- Search -->
                    <div class="input-group input-group-merge input-group-flush">
                        <div class="input-group-prepend">
                            <div class="input-group-text">
                                <i class="tio-search"></i>
                            </div>
                        </div>
                        {!! Form::text('q', request()->q, ['class' => 'form-control', 'placeholder' => 'Buscar productos']) !!}
                    </div>
                    <!-- End Search -->
                    {!! Form::close() !!}
                </div>

                <div class="col-sm-6">
                    <div class="d-sm-flex justify-content-sm-end align-items-sm-center">
                        <!-- Unfold -->
                        <div class="hs-unfold">
                            <a class="js-hs-unfold-invoker btn btn-sm btn-white" href="javascript:;"
                               data-hs-unfold-options='{
                         "target": "#usersFilterDropdown",
                         "type": "css-animation",
                         "smartPositionOff": true
                       }'>
                                <i class="tio-filter-list mr-1"></i> Filtro <span class="badge badge-soft-dark rounded-circle ml-1">2</span>
                            </a>

                            <div id="usersFilterDropdown" class="hs-unfold-content dropdown-unfold dropdown-menu dropdown-menu-sm-right dropdown-card card-dropdown-filter-centered" style="min-width: 22rem;">
                                <!-- Card -->
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-header-title">Filtrar productos</h5>

                                        <!-- Toggle Button -->
                                        <a class="js-hs-unfold-invoker btn btn-icon btn-xs btn-ghost-secondary ml-2" href="javascript:;"
                                           data-hs-unfold-options='{
                              "target": "#usersFilterDropdown",
                              "type": "css-animation",
                              "smartPositionOff": true
                             }'>
                                            <i class="tio-clear tio-lg"></i>
                                        </a>
                                        <!-- End Toggle Button -->
                                    </div>

                                    <div class="card-body">
                                        {!! Form::open([
                                            'method' => 'get'
                                        ]) !!}
                                        @if($categories->count())
                                        <div class="form-group">
                                            <small class="text-cap mb-2">Categorías</small>
                                            <div class="form-row text-nowrap">
                                                @foreach($categories as $category)
                                                    <div class="col">
                                                        <!-- Checkbox -->
                                                        <div class="custom-control custom-checkbox">
                                                            {!! Form::checkbox('category_id[]', $category->id, request()->has('category_id') && in_array($category->id, request()->category_id), ['class' => 'custom-control-input', 'id' => 'categoriesLabel_'.$category->id]) !!}
                                                            <label class="custom-control-label" for="categoriesLabel_{{ $category->id }}">{{ $category->title }}</label>
                                                        </div>
                                                        <!-- End Checkbox -->
                                                    </div>
                                                @endforeach
                                            </div>
                                        </div>
                                        @endif
                                        <div class="form-group">
                                            <small class="text-cap mb-2">Estado</small>

                                            <div class="form-row text-nowrap">
                                                @foreach([
                                                    0 => 'Inactivo',
                                                    1 => 'Activo'
                                                ] as $key => $value)
                                                    <div class="col">
                                                        <!-- Checkbox -->
                                                        <div class="custom-control custom-checkbox">
                                                            {!! Form::radio('active', $key, null, ['class' => 'custom-control-input', 'id' => 'activeLabel_'.$key]) !!}
                                                            <label class="custom-control-label" for="activeLabel_{{ $key }}">{{ $value }}</label>
                                                        </div>
                                                        <!-- End Checkbox -->
                                                    </div>
                                                @endforeach
                                            </div>
                                            <!-- End Row -->
                                        </div>

                                        <button type="submit" class="btn btn-block btn-primary">Filtrar</button>
                                        {!! Form::close() !!}
                                    </div>
                                </div>
                                <!-- End Card -->
                            </div>
                        </div>
                        <!-- End Unfold -->
                    </div>
                </div>
            </div>
            <!-- End Row -->
        </div>
        <!-- End Header -->

        <!-- Table -->
        <table class="table table-lg table-borderless table-thead-bordered table-nowrap table-align-middle card-table">
            <thead class="thead-light">
            <tr>
                <th>Nombre</th>
                <th>SKU</th>
                <th>Categorías</th>
                <th>Estado</th>
                <th></th>
            </tr>
            </thead>
            <tbody>
            @forelse($products as $product)
                <tr>
                    <td>
                        <a class="d-flex align-items-center" href="{{ route('backend.products.edit', $product->id) }}">
                            @if($product->image)
                                <div class="avatar avatar-circle mr-3">
                                    <img class="avatar-img" src="{{ asset($product->image) }}" alt="Image Description">
                                </div>
                            @endif
                            <div>
                                {{ $product->post->title }}
                            </div>
                        </a>
                    </td>
                    <td>{{ $product->sku }}</td>
                    <td>{{ $product->post->categories->implode('title', '-') }}</td>
                    <td>
                        <span class="legend-indicator {{ $product->post->active ? 'bg-success' : 'bg-danger' }}"></span>{{ $product->post->active ? 'Activo' : 'Inactivo' }}
                    </td>
                    <td>
                        <div class="btn-group" role="group">
                            <a class="btn btn-sm btn-white" href="{{ route('backend.products.edit', $product->id) }}">
                                <i class="tio-edit"></i> Editar
                            </a>

                            <!-- Unfold -->
                            <div class="hs-unfold btn-group">
                                <a class="js-hs-unfold-invoker btn btn-icon btn-sm btn-white dropdown-toggle dropdown-toggle-empty" href="javascript:;"
                                   data-hs-unfold-options='{
                         "target": "#productsEditDropdown_{{ $product->id }}",
                         "type": "css-animation",
                         "smartPositionOffEl": "#datatable"
                       }'></a>

                                <div id="productsEditDropdown_{{ $product->id }}" class="hs-unfold-content dropdown-unfold dropdown-menu dropdown-menu-right mt-1">
                                    <a class="dropdown-item modal-toggle-delete" href="#" data-href="{{ route('backend.products.destroy', $product->id) }}" data-title="{{ $product->post->title }}">
                                        <i class="tio-delete-outlined dropdown-item-icon"></i> Eliminar
                                    </a>
                                </div>
                            </div>
                            <!-- End Unfold -->
                        </div>
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="5" class="text-center"><b>No hay resultados</b></td>
                </tr>
            @endforelse
            </tbody>
        </table>
        <!-- End Table -->

        @if($products->hasPages())
        <!-- Footer -->
        <div class="card-footer">
            <!-- Pagination -->
            <div class="row justify-content-center justify-content-sm-between align-items-sm-center">
                <div class="col-sm mb-2 mb-sm-0">

                </div>

                <div class="col-sm-auto">
                    <div class="d-flex justify-content-center justify-content-sm-end">
                        <!-- Pagination -->
                        <nav id="datatablePagination" aria-label="Activity pagination">
                            {!! $products !!}
                        </nav>
                    </div>
                </div>
            </div>
            <!-- End Pagination -->
        </div>
        <!-- End Footer -->
        @endif
    </div>
    <!-- End Card -->
@endsection

@push('scripts')
    <!-- Modal -->
    <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                {!! Form::open([
                    'method' => 'delete'
                ]) !!}
                <div class="modal-header">
                    <h5 class="modal-title" id="deleteModalLabel">Eliminar producto</h5>
                    <button type="button" class="btn btn-xs btn-icon btn-ghost-secondary" data-dismiss="modal" aria-label="Close">
                        <i class="tio-clear tio-lg"></i>
                    </button>
                </div>
                <div class="modal-body">
                    <span></span>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-white" data-dismiss="modal">Cancelar</button>
                    <button class="btn btn-danger" type="submit">Eliminar</button>
                </div>
                {!! Form::close() !!}
            </div>
        </div>
    </div>
    <!-- End Modal -->
    <script>
      $(function() {
        $('.modal-toggle-delete').on('click', function() {
          $('#deleteModal form').attr('action', $(this).attr('data-href'));
          $('#deleteModal .modal-body span').text($(this).attr('data-title'));
          $('#deleteModal').modal('show');
        });
      });
    </script>
@endpush
