{% extends 'base.html.twig' %}
{% block searchform_container %}{% endblock %}
{% block body %}

<div class="container-fluid">

    <div class="row">
        <div class="col-md-12">

            <h3>
                <span class="glyphicon glyphicon-th-list"></span>
                MANEJO DE INVENTARIO
            </h3>

            <hr>

            {# ============================================================
               MENSAJES
               ============================================================ #}

            {% for message in app.flashes('success') %}
                <div class="alert alert-success">
                    {{ message }}
                </div>
            {% endfor %}

            {% for message in app.flashes('info') %}
                <div class="alert alert-info">
                    {{ message }}
                </div>
            {% endfor %}

            {% for message in app.flashes('danger') %}
                <div class="alert alert-danger">
                    {{ message }}
                </div>
            {% endfor %}


            {# ============================================================
               BUSCADOR
               ============================================================ #}

            <div class="panel panel-default">

                <div class="panel-heading">
                    <strong>
                        BUSCAR PRODUCTO
                    </strong>
                    
                    <div class="pull-right" style="margin-top:10px;">

        <a
            href="{{ path('product_index') }}"
            class="btn btn-default"
            style="
                font-size:14px;
                font-weight:bold;
                padding:9px 16px;
                border-radius:5px;
                border:1px solid #999;
                box-shadow:0 2px 4px rgba(0,0,0,0.20);
            "
        >

            <span class="glyphicon glyphicon-arrow-left"></span>

            VOLVER A LISTA

        </a>

    </div>
                </div>

                <div class="panel-body">

                    <form
                        method="get"
                        action="{{ path('inventario_index') }}"
                        class="form-inline"
                    >

                        <div class="form-group">

                            <input
                                type="text"
                                name="q"
                                value="{{ buscar }}"
                                class="form-control"
                                style="width:350px;"
                                placeholder="Código o descripción..."
                                autocomplete="off"
                            >

                        </div>

                        <button
                            type="submit"
                            class="btn btn-primary"
                        >
                            <span class="glyphicon glyphicon-search"></span>
                            BUSCAR
                        </button>

                        {% if buscar != '' %}

                            <a
                                href="{{ path('inventario_index') }}"
                                class="btn btn-default"
                            >
                                <span class="glyphicon glyphicon-remove"></span>
                                LIMPIAR
                            </a>

                        {% endif %}

                    </form>

                </div>
                
                

            </div>
            
            
            
            
            


            {# ============================================================
               INFORMACIÓN
               ============================================================ #}

            <div class="row">

                <div class="col-md-6">

                    <p>
                        <strong>
                            Productos encontrados:
                        </strong>

                        {{ totalProductos }}

                    </p>

                </div>

                <div class="col-md-6 text-right">

                    <p>

                        <strong>
                            Página:
                        </strong>

                        {{ pagina }}

                        {% if totalPaginas > 0 %}
                            de {{ totalPaginas }}
                        {% endif %}

                    </p>

                </div>

            </div>


            {# ============================================================
               LISTADO
               ============================================================ #}

            <div class="panel panel-default">

                <div class="panel-heading">

                    <strong>
                        EXISTENCIAS
                    </strong>

                    <span class="pull-right">
                        {{ porPagina }} productos por página
                    </span>

                </div>

                <div class="panel-body"
                     style="padding:0;">

                    {% if inventario|length > 0 %}

                        <table
                            class="table table-bordered table-striped table-hover"
                            style="margin-bottom:0;"
                        >

                            <thead>

                                <tr>

                                    <th style="width:110px;">
                                        CÓDIGO
                                    </th>

                                    <th>
                                        PRODUCTO
                                    </th>

                                    <th
                                        style="width:120px;"
                                        class="text-right"
                                    >
                                        PRECIO
                                    </th>

                                    <th
                                        style="width:130px;"
                                        class="text-right"
                                    >
                                        EXISTENCIA
                                    </th>

                                    <th style="width:220px;">
                                        NUEVO AJUSTE
                                    </th>

                                    <th
                                        style="width:120px;"
                                        class="text-center"
                                    >
                                        ESTADO
                                    </th>

                                    <th
                                        style="width:100px;"
                                        class="text-center"
                                    >
                                        ACCIONES
                                    </th>

                                </tr>

                            </thead>

                            <tbody>

                            {% for item in inventario %}

                                {% set producto = item.producto %}
                                {% set existencia = item.existencia %}

                                <tr>

                                    {# ====================================================
                                       CÓDIGO
                                       ==================================================== #}

                                    <td>

                                        <strong>
                                            {{ producto.reference }}
                                        </strong>

                                    </td>


                                    {# ====================================================
                                       PRODUCTO
                                       ==================================================== #}

                                    <td>

                                        {{ producto.description }}

                                    </td>


                                    {# ====================================================
                                       PRECIO
                                       ==================================================== #}

                                    <td class="text-right">

                                        {{ producto.price|number_format(
                                            2,
                                            '.',
                                            ','
                                        ) }}

                                    </td>


                                    {# ====================================================
                                       EXISTENCIA ACTUAL
                                       ==================================================== #}

                                    <td class="text-right">

                                        <strong>

                                            {{ existencia|number_format(
                                                2,
                                                '.',
                                                ','
                                            ) }}

                                        </strong>

                                    </td>


                                    {# ====================================================
                                       NUEVA EXISTENCIA + GUARDAR
                                       ==================================================== #}

                                    <td>

                                        <form
                                            method="post"
                                            action="{{ path('inventario_index') }}"
                                            class="form-inline"
                                            style="margin:0;"
                                        >

                                            <input
                                                type="hidden"
                                                name="_token"
                                                value="{{ csrf_token('inventario_existencias') }}"
                                            >

                                            <input
                                                type="hidden"
                                                name="product_id"
                                                value="{{ producto.id }}"
                                            >

                                            {% if buscar != '' %}

                                                <input
                                                    type="hidden"
                                                    name="q"
                                                    value="{{ buscar }}"
                                                >

                                            {% endif %}

                                            <div
                                                class="input-group"
                                                style="width:100%;"
                                            >

                                                <input
                                                    type="number"
                                                    name="existencia"
                                                    value="{{ existencia|number_format(
                                                        2,
                                                        '.',
                                                        ''
                                                    ) }}"
                                                    min="0"
                                                    step="0.01"
                                                    class="form-control"
                                                    required
                                                >

                                                <span class="input-group-btn">

                                                    <button
                                                        type="submit"
                                                        class="btn btn-success"
                                                        title="Guardar existencia"
                                                    >

                                                        <span class="glyphicon glyphicon-floppy-disk"></span>
                                                        GUARDAR

                                                    </button>

                                                </span>

                                            </div>

                                        </form>

                                    </td>


                                    {# ====================================================
                                       ESTADO
                                       ==================================================== #}

                                    <td class="text-center">

                                        {% if existencia|number_format(2, '.', '') == '0.00' %}

                                            <span class="label label-warning">
                                                SIN EXISTENCIA
                                            </span>

                                        {% else %}

                                            <span class="label label-success">
                                                DISPONIBLE
                                            </span>

                                        {% endif %}

                                    </td>


                                    {# ====================================================
                                       ACCIONES
                                       ==================================================== #}

                                    <td class="text-center">

                                        <a
                                            href="{{ path(
                                                'inventario_movimientos',
                                                {'id': producto.id}
                                            ) }}"
                                            class="btn btn-default btn-xs"
                                            title="Ver movimientos"
                                        >

                                            <span class="glyphicon glyphicon-list-alt"></span>

                                        </a>

                                    </td>

                                </tr>

                            {% endfor %}

                            </tbody>

                        </table>

                    {% else %}

                        <div
                            class="alert alert-info"
                            style="margin:15px;"
                        >

                            <span class="glyphicon glyphicon-info-sign"></span>

                            No se encontraron productos.

                            {% if buscar != '' %}

                                Prueba con otro código o descripción.

                            {% endif %}

                        </div>

                    {% endif %}

                </div>

            </div>


            {# ============================================================
               PAGINACIÓN
               ============================================================ #}

            {% if totalPaginas > 1 %}

                <nav aria-label="Paginación">

                    <ul class="pagination">


                        {# ====================================================
                           PRIMERA
                           ==================================================== #}

                        {% if pagina > 1 %}

                            <li>

                                <a
                                    href="{{ path(
                                        'inventario_index',
                                        {
                                            'page': 1,
                                            'q': buscar
                                        }
                                    ) }}"
                                >

                                    <span class="glyphicon glyphicon-step-backward"></span>

                                </a>

                            </li>

                        {% else %}

                            <li class="disabled">

                                <span>

                                    <span class="glyphicon glyphicon-step-backward"></span>

                                </span>

                            </li>

                        {% endif %}


                        {# ====================================================
                           ANTERIOR
                           ==================================================== #}

                        {% if pagina > 1 %}

                            <li>

                                <a
                                    href="{{ path(
                                        'inventario_index',
                                        {
                                            'page': pagina - 1,
                                            'q': buscar
                                        }
                                    ) }}"
                                >

                                    &laquo;

                                </a>

                            </li>

                        {% endif %}


                        {# ====================================================
                           NÚMEROS DE PÁGINA
                           ==================================================== #}

                        {% set inicio = max(1, pagina - 2) %}
                        {% set fin = min(totalPaginas, pagina + 2) %}

                        {% if inicio <= fin %}

                            {% for p in inicio..fin %}

                                <li
                                    class="{% if p == pagina %}active{% endif %}"
                                >

                                    <a
                                        href="{{ path(
                                            'inventario_index',
                                            {
                                                'page': p,
                                                'q': buscar
                                            }
                                        ) }}"
                                    >

                                        {{ p }}

                                    </a>

                                </li>

                            {% endfor %}

                        {% endif %}


                        {# ====================================================
                           SIGUIENTE
                           ==================================================== #}

                        {% if pagina < totalPaginas %}

                            <li>

                                <a
                                    href="{{ path(
                                        'inventario_index',
                                        {
                                            'page': pagina + 1,
                                            'q': buscar
                                        }
                                    ) }}"
                                >

                                    &raquo;

                                </a>

                            </li>

                        {% endif %}


                        {# ====================================================
                           ÚLTIMA
                           ==================================================== #}

                        {% if pagina < totalPaginas %}

                            <li>

                                <a
                                    href="{{ path(
                                        'inventario_index',
                                        {
                                            'page': totalPaginas,
                                            'q': buscar
                                        }
                                    ) }}"
                                >

                                    <span class="glyphicon glyphicon-step-forward"></span>

                                </a>

                            </li>

                        {% else %}

                            <li class="disabled">

                                <span>

                                    <span class="glyphicon glyphicon-step-forward"></span>

                                </span>

                            </li>

                        {% endif %}

                    </ul>

                </nav>

            {% endif %}

        </div>
    </div>

</div>

{% endblock %}
