<div>
    @include('parts.loading')
    @include('parts.alerts')

    @if ($mode == 'list')
        <div class="row mt-5">
            <div class="col-12">
                <div class="card">
                    <div class="card-header bg-gray-100">
                        <div class="form-row form-row-filter">
                            <div class="col-md-6">
                                <div class="wrapper-search">
                                    <input type="text" id="search" class="form-control" wire:model.debounce.1000ms="search" placeholder="Buscar...">
                                    @if (!empty($search))
                                        <button type="button" class="btn" id="btn-search">
                                            <i class="fa fa-times"></i>
                                        </button>
                                    @endif
                                </div>
                            </div>
                            <div class="col-md-3">
                                <select class="form-control" wire:model="search_category">
                                    <option value="">.:: Seleccionar categoria ::.</option>
                                    @foreach (config('const.product-categories') as $key => $value)
                                        <option value="{{ $key }}">{{ $value }}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="col-md-3">
                                <button wire:click="create()" class="btn btn-primary btn-block float-right">
                                    <i class="fas fa-plus"></i>
                                    Crear Producto
                                </button>
                            </div>
                        </div>
                    </div>
                    <div class="card-content">
                        <div class="card-body">
                            @if ($products->count())
                                <div class="table-responsive">
                                    <table class="table">
                                        <thead class="thead-light">
                                            <tr>
                                                <th>Nombre</th>
                                                <th style="width: 175px;">Categoria</th>
                                                <th class="text-center" style="width: 100px;">Acciones</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach($products as $value)
                                                <tr>
                                                    <td>{{ $value->name }}</td>
                                                    <td>{{ config('const.product-categories')[$value->category] }}</td>
                                                    <td class="text-center">
                                                        <div class="btn-group btn-group-sm">
                                                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                                                <i class="fas fa-ellipsis-h"></i>
                                                            </a>
                                                            <div class="dropdown-menu dropdown-menu-right">
                                                                <button wire:click="edit({{ $value->id }})" class="dropdown-item btn-link">
                                                                    <i class="fas fa-pencil-alt mr-1"></i> Editar
                                                                </button>
                                                                <button wire:click="$emit('triggerDestroy',{{ $value->id }})" class="dropdown-item btn-link text-danger">
                                                                    <i class="far fa-trash-alt mr-1"></i> Eliminar
                                                                </button>
                                                            </div>
                                                        </div>
                                                    </td>
                                                </tr>
                                            @endforeach
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th colspan="3">
                                                    {{ $products->links() }}
                                                </th>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div>
                            @else
                                <div class="alert alert-secondary" role="alert">
                                    No se encontraron resultados
                                </div>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </div>
    @else
        @include('livewire.products.form')
    @endif

</div>

@section('js')
    @include('parts.js-destroy')
    @include('parts.js-preview')
    @include('parts.js-search')

    <script>
        document.addEventListener('livewire:load', function () {
            init();
        });
        window.addEventListener('init-libs', event => {
            init();
        });

        function init() {
            if (tinyMCE.get('description')) {
                tinymce.remove('#description');
            }
            tinyMCE.init({
                selector         : '#description',
                plugins          : 'contextmenu preview paste importcss searchreplace directionality code visualchars fullscreen  link codesample table charmap hr pagebreak nonbreaking anchor toc advlist lists  textpattern noneditable help charmap emoticons image',
                menubar          : 'edit view format tools table help insert',                                                                                                                                                                                                                                   //insert
                toolbar          : 'undo redo | bold italic underline strikethrough |  formatselect | alignleft aligncenter alignright alignjustify | outdent indent |  numlist bullist | forecolor backcolor removeformat | pagebreak | charmap emoticons | fullscreen  preview save print | ltr rtl | image',   //insertfile image media template link anchor codesample |
                toolbar_sticky   : true,
                contextmenu      : false,
                height           : 400,
                paste_data_images: false,
                automatic_uploads: true,
                setup: function (editor) {
                    editor.on('init change', function () {
                        editor.save();
                    });
                    // editor.on('change', function (e) {
                    //     @this.set('description', editor.getContent());
                    // });
                    editor.on('blur', function (e) {
                        @this.set('description', editor.getContent());
                    });
                }
            });
        }
    </script>
@endsection

