{~
    @meta.scripts.raw.table = '
    var $filterRow = $(".filter-row")
    var $inputs    = $filterRow.find("input");
    var $btnFilter = $filterRow.find(".table-filter");
    var $btnClear  = $filterRow.find(".table-filter-clear");

    $inputs.keyup(function(e) {
        var $this = $(this);

        if($this.val() == "" && $this.attr("value") == "")
            $this.css("background-color", "transparent");
        else if($this.val() != $this.attr("value"))
            $this.css("background-color", "aliceblue");
        else
            $this.css("background-color", "#FFFFDD");

        //$this.css("background-color", $this.val() ? "#FFFFDD" : "transparent");

        var filled = $inputs.filter(function() {
            return $(this).val() != $(this).attr("value");
        }).length > 0;

        if(filled) {
            $btnFilter.removeClass("btn-default").addClass("btn-primary");
            //$btnClear.removeClass("btn-default").addClass("btn-warning");
        } else {
            $btnFilter.removeClass("btn-primary").addClass("btn-default");
            //$btnClear.removeClass("btn-warning").addClass("btn-default");
        }
    }).trigger("keyup");

    $filterFields = $(".table-filter-field").keydown(function(e) {
        if (e.keyCode == 13) {
            $(".table-filter").click();
            window.location.reload();
        }
    });

    $(".table-filter").click(function() {
        $filterFields.each(function(i, el) {
            //$el = $(el);
            var inputValue = el.value.trim();
            var cookieName = "table_filter_'.@table.route.'_" + el.name;

            if(inputValue == "")
                $.removeCookie(cookieName, {path: "/"});
            else
                $.cookie(cookieName, inputValue, {path: "/"});
        });
    });

    $(".table-filter-clear").click(function() {
        $filterFields.each(function(i, el) {
            var cookieName = "table_filter_'.@table.route.'_" + el.name;
            $.removeCookie(cookieName, {path: "/"});
        });
    });

    $(".table-sort").click(function() {
        var cookieField = "table_sort_'.@table.route.'";
        var cookieOrder = "table_order_'.@table.route.'";
        var field = $(this).data("field");
        if(field != $.cookie(cookieField)) {
            $.cookie(cookieField, field, {path: "/"});
            $.removeCookie(cookieOrder, {path: "/"});
        }

        switch($.cookie(cookieOrder)) {
            case "asc" : {
                $.cookie(cookieOrder, "desc", {path: "/"});
                break;
            }
            case "desc": {
                $.removeCookie(cookieOrder, {path: "/"});
                $.removeCookie(cookieField, {path: "/"});
                break;
            };
            default    : $.cookie(cookieOrder, "asc", {path: "/"});
        }
    });

    $(".table-filter-clear").click(function() {
        $filterFields.each(function(i, el) {
            var cookieName = "table_filter_'.@table.route.'_" + el.name;
            $.removeCookie(cookieName, {path: "/"});
        });
    });

    $(".table-export").click(function() {
        var csv = $("#table_'.@table.route.'").TableCSVExport({
            header:'.json_encode(array_values(@table.cols)).',
            columns:'.json_encode(array_values(@table.cols)).',
            rowfilter: ":not(.filter-row)",
            delivery: "value",
            separator: "'.@app.table.export.csv.separator.'"
        });
        var blob = new Blob([csv], {type: "application/octet-binary"});
        this.href = URL.createObjectURL(blob);
        this.download = "exportacao.csv";
    });
    ';

    @meta.scripts.local.inputmask    = 'jquery.inputmask.bundle.min.js';
    @meta.scripts.local.inputmasknum   = 'inputmask/inputmask.numeric.extensions.min.js';
    @meta.scripts.local.inputmaskbind  = 'inputmask/inputmask.binding.js';
    @meta.scripts.local.cookie = 'jquery.cookie.min.js';
    @meta.styles.raw.fix_table = '.box-header a.btn-primary, .box-header a.btn-success, .box-header a.btn-danger, .box-header a.btn-info, .box-header a.btn-warning { color:white; }';

    if(!isset(@page.description)) @page.description = 'Lista';
~}
<include href="hwapx/admin/admin/header.html" />
<style>
@media print {
    body * {
        visibility: hidden;
    }
    .print, .print * {
        visibility: visible;
    }
    .print {
        position: relative;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
    }
    .dont-print, .content-header, .filter-row, .box-header, .box-footer {
        display: none;
    }
    .wrapper, .right-side {
        margin: 0 !important;
    }
    a:link:after, a:visited:after {
        content: "" !important;
    }
    .content, .box-body {
        margin: 0;
    }
}
</style>
<div class="row">
    <div class="col-lg-12">
        <div class="box">
            <div class="box-header">
                <h3 class="box-title"><span class="glyphicon glyphicon-th-list"></span> &nbsp;{{ @table.title | dict }}</h3>
                <check if="{{ isset(@table.actions) }}">
                    <div class="box-tools">
                        <div class="input-groupx pull-right">
                            <repeat group="{{ @table.actions }}" value="{{ @action }}" counter="{{ @counter }}">
                                <check if="{{ @action.role | role }}">
                                    <true>
                                        <check if="{{ @counter < @app.table.maxactions or count(@table.actions) <= @app.table.maxactions }}">
                                            <true>
                                                <a onclick="{{ (isset(@action.confirm) and @action.confirm) ? 'return confirm(\''.addslashes(is_string(@action.confirm) ? @action.confirm : 'Confirma esta ação?').'\');' : '' }}" href="{{ isset(@ALIASES[@action.route]) ? @BASE.\F3::alias(@action.route) : @action.route }}" class="btn {{ isset(@action.class) ? @action.class : 'btn-default' }} btn-flat">{{ @action.description }}</a>
                                            </true>
                                            <false>
                                                <check if="{{ (count(@table.actions) > @app.table.maxactions and @counter == @app.table.maxactions)  }}">
                                                    <div class="btn-group">
                                                        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                                                            Mais Ações
                                                            <span class="caret"></span>
                                                        </button>
                                                        <ul class="dropdown-menu pull-right" role="menu">
                                                </check>
                                                    <li>
                                                        <check if="{{ is_array(@action) }}">
                                                            <true>
                                                                <a onclick="{{ (isset(@action.confirm) and @action.confirm) ? 'return confirm(\''.addslashes(is_string(@action.confirm) ? @action.confirm : 'Confirma esta ação?').'\');' : '' }}" href="{{ isset(@ALIASES[@action.route]) ? @BASE.\F3::alias(@action.route) : @action.route }}">{{ @action.description }}</a>
                                                            </true>
                                                            <false>
                                                                <li class="divider"></li>
                                                            </false>
                                                        </check>
                                                    </li>
                                                <check if="{{ @counter == count(@table.actions) }}">
                                                        </ul>
                                                    </div>
                                                </check>
                                            </false>
                                        </check>
                                    </true>
                                    <false>
                                        {~ @counter-- ~}
                                    </false>
                                </check>
                            </repeat>
                        </div>
                    </div>
                </check>
            </div>
            <!-- /.panel-heading -->
            <div class="box-body no-padding">
                <div {*class="table-responsive"*}>
                    <table id="table_{{ @table.route }}" class="table table-hover print">
                        <thead>
                            <tr>
                                <repeat group="{{ @table.cols }}" key="{{ @field }}" value="{{ @col }}" counter="{{ @count }}">
                                    <th {{ @count == 1 ? 'style="width: 120px"' : '' }}>
                                        {~
                                            @sort  = isset(@COOKIE['table_sort_'.@table.route])  ? @COOKIE['table_sort_'.@table.route] : '';
                                            @order = isset(@COOKIE['table_order_'.@table.route]) ? @COOKIE['table_order_'.@table.route] : '';
                                         ~}
                                        <check if="{{ @sort == @field }}">
                                            <span class="glyphicon glyphicon-chevron-{{ @order == 'desc' ? 'up' : 'down' }}"></span>
                                        </check>
                                        <a class="table-sort" data-field="{{ @field }}" href="{{ @URI }}">{{ isset(@col.label) ? @col.label : @col }}</a>
                                    </th>
                                </repeat>
                                <check if="{{ isset(@table.rowactions) }}">
                                    <th class="dont-print text-center" style="width:220px;"><span class="glyphicon glyphicon-flash"></span> Ações</th>
                                </check>
                            </tr>
                            <tr class="filter-row">
                                <repeat group="{{ @table.cols }}" key="{{ @field }}" value="{{ @col }}" counter="{{ @counter }}">
                                    <td>
                                        <div class="form-group">
                                            <div class="{{ @counter == count(@table.cols) && !isset(@table.rowactions) ? 'input-group' : '' }}">
                                                {~ if(isset(@COOKIE['table_filter_'.@table.route.'_'.@field])) @filter_set = true; ~}
                                                <check if="{{ isset(@col.options) }}">
                                                    <datalist id="datalist_{{ @field }}">
                                                        <repeat group="{{ @col.options }}" key="{{ @option_value }}" value="{{ @option_text }}">
                                                            <option value="{{ @option_text }}">{{ @option_value }}</option>
                                                        </repeat>
                                                    </datalist>
                                                </check>
                                                <input
                                                    class="form-control table-filter-field"
                                                    name="{{ @field }}"
                                                    placeholder="Filtrar por {{ isset(@col.label) ? @col.label : @col }}"
                                                    {{ isset(@col.mask) ? 'data-inputmask-skipOptionalPartCharacter: " " data-inputmask data-inputmask-mask="'.@col.mask.'"' : '' }}
                                                    {{ isset(@col.options) ? 'list="datalist_'.@field.'"' : '' }}
                                                    {{ isset(@col.attributes) ? packXMLAttributes(@col.attributes) : '' }}
                                                    value="{{ isset(@COOKIE['table_filter_'.@table.route.'_'.@field]) ? @COOKIE['table_filter_'.@table.route.'_'.@field] : '' }}">
                                                <check if="{{ @counter == count(@table.cols) and !isset(@table.rowactions) }}">
                                                    <span class="input-group-btn">
                                                        <a class="btn btn-default table-filter" href="{{ @BASE }}{{ @table.route|alias }}"><span class="glyphicon glyphicon-search"></span></a>
                                                    </span>
                                                </check>
                                            </div>
                                        </div>
                                    </td>
                                </repeat>
                                <check if="{{ isset(@table.rowactions) }}">
                                    <td class="text-center">
                                        <div class="form-group">
                                             <a class="btn btn-default btn-flat table-filter" href="{{ @BASE }}{{ @table.route|alias }}"><span class="glyphicon glyphicon-search"></span> Filtrar</a>
                                             <a {{ isset(@filter_set) ? '' : 'disabled' }} class="btn btn-{{ isset(@filter_set) ? 'warning' : 'default' }} btn-flat table-filter-clear" href="{{ @BASE }}{{ @table.route|alias }}"><span class="glyphicon glyphicon-erase"></span> Limpar</a>
                                        </div>
                                    </td>
                                </check>
                            </tr>
                        </thead>
                        <tbody>
                            <repeat group="{{ @table.subset }}" value="{{ @row }}">
                                {~
                                    @rowparams = array();
                                    if(isset(@table.rowcallback))
                                        @rowparams = @table.rowcallback(@row);
                                ~}
                                <tr class="{{ isset(@rowparams['class']) ? @rowparams['class'] : '' }}">
                                    <repeat group="{{ @table.cols }}" key="{{ @field }}" value="{{ @col }}">
                                        {~
                                        @_fields = is_object(@row) ? @row->schema() : array();

                                        F3::set('PARAMS.id', @row.id);

                                        @cellaction = '';

                                        if(isset(@table.rowactions)):
                                            @act = array_filter(@table.rowactions, function(@a) { return isset(@a.default) && @a.default; });
                                            @act = count(@act) ? reset(@act) : @act;

                                            if(@act && \Hwapx\Core\Library\ModuleManager::instance()->hasRole(@act.role))
                                                @cellaction = isset(@ALIASES[@act.route]) ? @BASE.\F3::alias(@act.route, is_array(@row) ? @row : @row->cast()) : F3::build(@act.route, is_array(@row) ? @row : @row->cast());
                                        endif;

                                        @cellAttr = array(
                                            'onclick' => @cellaction ? "window.location.href = '@cellaction'" : '',
                                            'class'   => (isset(@_fields[@field].pdo_type) && @_fields[@field].pdo_type == \PDO::PARAM_INT) || (isset(@col.type) && @col.type == 'number') ? 'text-right' : '',
                                            'data-field' => @field
                                        );

                                        if(is_array(@col) && isset(@col['callback'])):
                                            @cellAttr = @col.callback(@col, @cellAttr);
                                        endif;
                                        if(isset(@table['cellcallback'])):
                                            @cellAttr = @table.cellcallback(@field, @row, @col, @cellAttr);
                                        endif;
                                        ~}
                                        <td {{ packXMLAttributes(@cellAttr) }}>
                                            <switch expr="{{ isset(@col.type) && @row[@field] !== null ? @col.type : (isset(@_fields[@field]) ? strtolower(@_fields[@field].type) : 'UNKNOW') }}">
                                                <case value="date" break="TRUE">
                                                    {{ @row[@field] ? date(@app.date.format, strtotime(@row[@field])) : '' }}
                                                </case>
                                                <case value="time" break="TRUE">
                                                    {{ @row[@field] ? date(@app.time.format, strtotime(@row[@field])) : '' }}
                                                </case>
                                                <case value="datetime" break="TRUE">
                                                    {{ @row[@field] ? date(@app.datetime.format, strtotime(@row[@field])) : '' }}
                                                </case>
                                                <case value="timestamp" break="TRUE">
                                                    {{ @row[@field] ? date(@app.datetime.format, @row[@field]) : '' }}
                                                </case>
                                                <case value="number" break="TRUE">
                                                    {{ isset(@col.digits) ? number_format(@row[@field], @col.digits, ',', '.') : @row[@field] }}
                                                </case>
                                                <default>
                                                    <check if="{{ isset(@table.raw) && ((is_array(@table.raw) && in_array(@field, @table.raw)) || @table.raw) }}">
                                                        <true>
                                                            {{ isset(@row[@field]) ? @row[@field] : @row->@field | raw }}
                                                        </true>
                                                        <false>
                                                            {{ isset(@row[@field]) ? @row[@field] : @row->@field }}
                                                        </false>
                                                    </check>
                                                </default>
                                            </switch>
                                        </td>
                                    </repeat>
                                    <check if="{{ isset(@table.rowactions) }}">
                                        <td class="dont-print text-center">
                                            <repeat group="{{ @table.rowactions }}" value="{{ @action }}" counter="{{ @counter }}">
                                                <check if="{{ @action.role | role }}">
                                                    <true>
                                                        <check if="{{ isset(@action.params) }}">
                                                            <repeat group="{{ @action.params }}" key="{{ @param }}" value="{{ @field }}">
                                                                {~ F3::set('PARAMS.'.@param, @row[@field]) ~}
                                                            </repeat>
                                                        </check>
                                                        <check if="{{ @counter < @app.table.maxactions or count(@table.rowactions) <= @app.table.maxactions }}">
                                                            <true>
                                                                <a onclick="{{ (isset(@action.confirm) and @action.confirm) ? 'return confirm(\''.addslashes(is_string(@action.confirm) ? @action.confirm : 'Confirma esta ação?').'\');' : '' }}" data-id="{{ @PARAMS.id = @row.id }}" href="{{ isset(@ALIASES[@action.route]) ? @BASE.\F3::alias(@action.route, is_array(@row) ? @row : @row->cast()) : F3::build(@action.route, is_array(@row) ? @row : @row->cast()) }}" {{ isset(@action.modal) && @action.modal ? 'data-toggle="modal"' : '' }} class="btn {{ isset(@action.class) ? @action.class : 'btn-default' }} btn-xs btn-flat" target="{{ isset(@action.target) ? @action.target : '_self' }}">{{ @action.description }}</a>
                                                            </true>
                                                            <false>
                                                                <check if="{{ @counter == @app.table.maxactions }}">
                                                                    <div class="btn-group">
                                                                        <button type="button" class="btn btn-default btn-xs btn-flat dropdown-toggle" data-toggle="dropdown">
                                                                            Mais Ações
                                                                            <span class="caret"></span>
                                                                        </button>
                                                                        <ul class="dropdown-menu pull-right" role="menu">
                                                                </check>
                                                                    <li>
                                                                        <check if="{{ is_array(@action) }}">
                                                                            <true>
                                                                                <a href="{~ F3::set('PARAMS.id', @row.id) ~}{{ isset(@ALIASES[@action.route]) ? @BASE.\F3::alias(@action.route, is_array(@row) ? @row : @row->cast()) : @action.route }}" onclick="{{ (isset(@action.confirm) and @action.confirm) ? 'return confirm(\''.addslashes(is_string(@action.confirm) ? @action.confirm : 'Confirma esta ação?').'\');' : '' }}" {{ isset(@action.modal) && @action.modal ? 'data-toggle="modal"' : '' }} target="{{ isset(@action.target) ? @action.target : '_self' }}">{{ @action.description }}</a>
                                                                            </true>
                                                                            <false>
                                                                                <li class="divider"></li>
                                                                            </false>
                                                                        </check>
                                                                    </li>
                                                                <check if="{{ @counter == count(@table.rowactions) }}">
                                                                        </ul>
                                                                    </div>
                                                                </check>
                                                            </false>
                                                        </check>
                                                    </true>
                                                    <false>
                                                        {~ @counter-- ~}
                                                    </false>
                                                </check>
                                            </repeat>
                                        </td>
                                    </check>
                                </tr>
                            </repeat>
                        </tbody>
                    </table>
                </div>
                <!-- /.table-responsive -->
            </div>
            <!-- /.panel-body -->
            <div class="box-footer">
                <check if="{{ isset(@table.legend) }}">
                <div class="row">
                    <div class="col-sm-12">
                        <repeat group="{{ @table.legend }}" key="{{ @class_attr }}" value="{{ @legend }}">
                            <div
                                class="text-center {{ @class_attr != 'active' ? ' alert-' . @class_attr : '' }}"
                                style="{{ @class_attr == 'active' ? 'background-color: #f5f5f5;' : '' }}float:left;width:{{ 100 / count(@table.legend) }}%"
                                >
                                {{ @legend }}
                            </div>
                        </repeat>
                    </div>
                    <br>&nbsp;
                </div>
                </check>
                <div class="row">
                    <div class="col-sm-6">
                        <check if="{{ @table.total }}">
                            <true>
                                <check if="{{ @table.rowcount === true }}">
                                <select class="form-control" style="display: inline; width: auto;" onchange="$.cookie('{{ 'table_rowcount_'.@table.route }}', this.value); window.location.reload();">
                                    {~ @_rowcount = isset(@COOKIE['table_rowcount_'.@table.route]) ? @COOKIE['table_rowcount_'.@table.route] : null ~}
                                    <option value=""{{ @_rowcount === '' ? ' selected' : '' }}>Todos</option>
                                    <repeat group="{{ @app.table.countoptions ?: array(10, 20, 30, 40, 50, 100, 500, 1000) }}" value="{{ @value }}">
                                        <option value="{{ @value }}"{{ (@_rowcount === null && @value == @app.table.rowcount) || @_rowcount == @value ? ' selected' : '' }}>{{ @value }}</option>
                                    </repeat>
                                </select>
                                Itens por página.
                                </check>
                                Exibindo {{ {*isset(@table.name) ? strtolower(@table.name) :*} 'registros' }} {{ @table.limit * @table.pos + 1 }} a {{ @table.count > 1 ? @table.limit * @table.pos + count(@table.subset) : @table.total }} de um total de {{ @table.total }}.
                            </true>
                            <false>
                                Nenhum registro encontrado.
                            </false>
                        </check>
                    </div>
                    <div class="col-sm-6">
                        <ul class="pagination pagination-sm no-margin pull-right">
                            <check if="{{ @table.count > 1 }}">
                                <li class="{{ @table.pos ? '' : ' disabled' }}"><a href="{{ @BASE }}{{ @table.route|alias }}/1">«</a></li>
                                {~ @_c = 9 ~}
                                {~ @_h = max(@_c - (@table.count - @table.pos), floor(@_c / 2)) ~}
                                {~ @_from = max(@table.pos - @_h, 0) ~}
                                {~ @_c -= @table.pos - @_from ~}
                                {~ @_to = min(@table.pos + @_c, @table.count) ~}
                                <loop from="{{ @i = @_from }}" to="{{ @i < @_to }}" step="@i++">
                                    <li class="{{ @i == @table.pos ? ' active' : '' }}">
                                        <a href="{{ @BASE }}{{ @table.route|alias }}/{{ @i + 1 }}">{{ @i + 1 }}</a>
                                    </li>
                                </loop>
                                <li class="{{ @table.pos == @table.count - 1 ? ' disabled' : '' }}"><a href="{{ @BASE }}{{ @table.route|alias }}/{{ @table.count }}">»</a></li>
                            </check>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
<include href="hwapx/admin/admin/footer.html" />