{% extends "authed.twig" %}
{% import "inline.twig" as inline %}

{% block title %}{{ "Products for "|trans }} {{menuBoardCategory.name}} {{ "in Menu Board "|trans }} {{ menuBoard.name }} | {% endblock %}

{% block actionMenu %}
    <div class="widget-action-menu pull-right">
        {% if currentUser.featureEnabled("menuBoard.modify") %}
            <button class="btn btn-success XiboFormButton" title="{% trans "Add a new Menu Board Product" %}" href="{{ url_for("menuBoard.product.add.form", {id : menuBoardCategory.menuCategoryId}) }}"><i class="fa fa-plus-circle" aria-hidden="true"></i> {% trans "Add Product" %}</button>
            <button class="btn btn-info XiboRedirectButton" href="{{ url_for("menuBoard.category.view", {"id": menuBoardCategory.menuId}) }}"><i class="fa fa-columns" aria-hidden="true"></i> {% trans "View Categories" %}</button>
        {% endif %}
        <button class="btn btn-primary" id="refreshGrid" title="{% trans "Refresh the Table" %}" href="#"><i class="fa fa-refresh" aria-hidden="true"></i></button>
    </div>
{% endblock %}

{% block pageContent %}
    <div class="widget">
        <div class="widget-title">{% trans "Products for" %} {{menuBoardCategory.name}} {% trans "in Menu Board" %} {{ menuBoard.name }} </div>
        <div class="widget-body">
            <div class="XiboGrid" id="{{ random() }}" data-grid-type="menuBoardProducts" data-grid-name="menuBoardProductView">
                <div class="XiboFilter card mb-3 bg-light">
                    <div class="FilterDiv card-body" id="Filter">
                        <form class="form-inline">
                            {% set title %}{% trans "ID" %}{% endset %}
                            {{ inline.number("menuProductId", title) }}

                            <div class="form-group mr-1 mb-1">
                                <label class="control-label mr-1" title="" for="name" accesskey="">{% trans "Name" %}</label>
                                <div class="input-group">
                                    <input class="form-control" name="name" type="text" id="name" value="">
                                    <div class="input-group-append input-group-addon">
                                        <div class="input-group-text">
                                            <input title="{% trans "Use Regex?" %}" type="checkbox" id="useRegexForName" name="useRegexForName">
                                        </div>
                                    </div>
                                </div>
                            </div>

                            {% set title %}{% trans "Code" %}{% endset %}
                            {{ inline.input('code', title) }}
                        </form>
                    </div>
                </div>
                <div class="XiboData card">
                    <div class="card-header mb-3">
                        <ul class="nav nav-tabs card-header-tabs" id="menuCategories">
                            {% for category in categories %}
                                <li class="nav-item">
                                    <a class="nav-link {% if category.menuCategoryId == menuBoardCategory.menuCategoryId %} active {% endif %}" href="{{ url_for("menuBoard.product.view", {id: category.menuCategoryId}) }}">{{ category.name }}</a>
                                </li>
                            {% endfor %}
                        </ul>
                    </div>
                    <table id="menuBoardProducts" class="table table-striped responsive nowrap" data-content-type="menuBoardProduct" data-content-id-name="menuProductId" data-state-preference-name="menuBoardProductsGrid" style="width: 100%;">
                        <thead>
                        <tr>
                            <th>{% trans "ID" %}</th>
                            <th>{% trans "Name" %}</th>
                            <th>{% trans "Description" %}</th>
                            <th>{% trans "Price" %}</th>
                            <th>{% trans "Media" %}</th>
                            <th>{% trans "Display Order" %}</th>
                            <th>{% trans "Availability" %}</th>
                            <th>{% trans "Allergy Information" %}</th>
                            <th>{% trans "Calories" %}</th>
                            <th>{% trans "Code" %}</th>
                            <th class="rowMenu"></th>
                        </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block javaScript %}
    <script type="text/javascript" nonce="{{ cspNonce }}">
        var table;
        $(document).ready(function() {
            table = $("#menuBoardProducts").DataTable({
                "language": dataTablesLanguage,
                "lengthMenu": [10, 25, 50, 100, 250, 500],
                serverSide: true,
                stateSave: true,
                stateDuration: 0,
                responsive: true,
                stateLoadCallback: dataTableStateLoadCallback,
                stateSaveCallback: dataTableStateSaveCallback,
                filter: false,
                searchDelay: 3000,
                dataType: 'json',
                order: [[5, 'asc']],
                ajax: {
                    url: "{{ url_for("menuBoard.product.search", {id: menuBoardCategory.menuCategoryId}) }}",
                    data: function (d) {
                        $.extend(d, $("#menuBoardProducts").closest(".XiboGrid").find(".FilterDiv form").serializeObject());
                    }
                },
                columns: [
                    {data: 'menuProductId', responsivePriority: 2},
                    {
                        data: 'name',
                        responsivePriority: 2,
                        render: dataTableSpacingPreformatted,
                    },
                    {
                        data: 'description',
                        responsivePriority: 3,
                    },
                    {
                      data: 'price',
                      responsivePriority: 2,
                      render: function (data, type) {
                        if (type !== 'display' || data === null || data === '') {
                          return data;
                        }
                        return new Intl.NumberFormat(undefined, {minimumFractionDigits: 2}).format(data);
                      },
                    },
                    {
                        responsivePriority: 3,
                        data: 'mediaId',
                        render: function (data, type, row) {
                          if (type !== 'display' || data === null || data === '') {
                            return data;
                          }

                          if (row.thumbnail && row.thumbnail !== '') {
                            return '<a class="img-replace" data-toggle="lightbox" data-type="image" href="' + row.thumbnail + '">' +
                              '<img class="img-fluid" src="' + row.thumbnail.replace('download', 'thumbnail') + '" alt="{{ "Thumbnail"|trans }}" />' +
                              '</a>';
                          } else {
                            return '';
                          }
                        },
                    },
                    {
                        data: 'displayOrder',
                        responsivePriority: 2,
                    },
                    {
                      data: 'availability',
                      responsivePriority: 2,
                      render: function (data, type, row) {
                        if (type !== 'display') {
                          return data;
                        }

                        let icon = '';
                        if (data === 1) {
                          icon = 'fa-check';
                        } else if (data === 0) {
                          icon = 'fa-times';
                        }

                        return '<span class="fa ' + icon + '"></span>';
                      },
                    },
                    {
                        data: 'allergyInfo',
                        responsivePriority: 3,
                    },
                    {
                        data: 'calories',
                        responsivePriority: 2,
                    },
                    {
                        data: 'code',
                        responsivePriority: 2,
                    },
                    {
                        orderable: false,
                        responsivePriority: 1,
                        data: dataTableButtonsColumn,
                    }
                ]
            });

            table.on('draw', dataTableDraw);
            table.on('processing.dt', dataTableProcessing);
            dataTableAddButtons(table, $('#menuBoardProducts_wrapper').find('.col-md-6').eq(1));

            $("#refreshGrid").click(function () {
                table.ajax.reload();
            });
        });

    </script>
{% endblock %}
