<?php if(!isset($page['description']))
        $page['description'] = 'Criar/Editar';

    if(!$breadcrumbs)
        if(isset($form['table']))
            $breadcrumbs = array($form['table'] => $page['title'], $page['description']);
        else
            $breadcrumbs = array($page['title']);

    /*$meta['styles']['local']['codemirror']    = 'codemirror.css';
    $meta['scripts']['local']['codemirror']   = 'codemirror.js';
    $meta['scripts']['local']['codemirror_xml']  = 'codemirror/mode/xml/xml.js';
    $meta['scripts']['local']['codemirror_css']  = 'codemirror/mode/css/css.js';
    $meta['scripts']['local']['codemirror_js']   = 'codemirror/mode/javascript/javascript.js';
    $meta['scripts']['local']['codemirror_html'] = 'codemirror/mode/htmlmixed/htmlmixed.js';
    $meta['styles']['raw']['codemirror']      = '.CodeMirror {border: 1px solid #CCC;}';*/
    $meta['styles']['local']['trumbowyg']     = 'trumbowyg.min.css';
    $meta['styles']['raw']['trumbowyg']       = '.trumbowyg { margin: 0 !important; width: 100% !important; }';
    $meta['scripts']['local']['trumbowyg']    = 'trumbowyg.min.js';
    $meta['scripts']['local']['trumbowyg_pt'] = 'trumbowyg-pt.min.js';
    $meta['scripts']['raw']['trumbowyg']      = '$(".trumbowyg").trumbowyg({lang: "pt", autogrow: true});';
    $meta['scripts']['local']['moment']       = 'moment.js';
    $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']['datetimepicker'] = 'bootstrap-datetimepicker.min.js';
    $meta['styles']['local']['datetimepicker']  = 'bootstrap-datetimepicker.min.css';
    array_unshift($meta['scripts']['raw'], '$(".date").datetimepicker();');

    $meta['styles']['local']['fileinput']     = 'fileinput.min.css';
    $meta['styles']['raw']['fileinput']       = '.kv-fileinput-caption { margin-bottom: 2px !important; }';
    $meta['scripts']['local']['fileinput']    = 'fileinput.min.js';

    $meta['styles']['local']['select2']       = 'select2.min.css';
    $meta['styles']['local']['select2_bs']    = 'select2-bootstrap.min.css';
    $meta['scripts']['local']['select2']      = 'select2/select2.min.js';
    $meta['scripts']['local']['select2_i18n'] = 'select2/i18n/pt-BR.js';

    $meta['scripts']['raw']['form_table']     = 'window._appendrow = function(group) {
                                            var $table = $("#table_" + group + " tbody");
                                            var row_count = $table.data("row-count") + 1;
                                            var $row   = $("#row_" + group).clone().removeAttr("id").removeAttr("style").removeClass("dont_post").removeClass("dont-print");
                                            $table.data("row-count", row_count);

                                            $row.find(".order").val(row_count);

                                            $row.find("[name]").each(function(e) {
                                                var $e = $(this);
                                                var name = $e.attr("name").replace(group + "[0]", group + "[" + row_count + "]");

                                                $e.attr("name", name);
                                            });
                                            $row.find(".inputmask, [data-inputmask], [data-inputmask-mask], [data-inputmask-alias]").each(function (ndx, lmnt) {
                                                if (lmnt.inputmask === undefined) {
                                                    Inputmask().mask(lmnt);
                                                }
                                            });

                                            $row.find("select").addClass("select2").css("width", "100%").select2({theme: "bootstrap", minimumResultsForSearch: 5})

                                            $row.find(".date").datetimepicker();

                                            $table.append($row);
                                        }

                                        $("#main_form").submit(function() {
                                            $("input[type=submit]").prop("disabled", true);
                                            $(".dont_post").remove();
                                        }).find("select, input").trigger("change");

                                        $(".select2").css("width", "100%").select2({theme: "bootstrap", minimumResultsForSearch: 5});
                                        ';
    $meta['scripts']['raw']['sortable_table'] = 'jQuery(document).ready(function($) {
                                            function reorder($table) {
                                                var rows = $table.find("tr").get();

                                                rows.sort(function(a, b) { return $(a).find(".order").val() - $(b).find(".order").val(); });

                                                for (var i = 0; i < rows.length; i++)
                                                    rows[i].parentNode.appendChild(rows[i]);
                                            }

                                            $("table").on("click", ".order-prior", function(e) {
                                                e.preventDefault();

                                                var $this    = $(this);
                                                var $table   = $this.parents("tbody");
                                                var $current = $this.parents("tr").find(".order");
                                                var $previus = $this.parents("tr").prev("tr:not(.dont_post)").find(".order");

                                                if($previus.length) {
                                                    var old = $current.val();
                                                    $current.val($previus.val());
                                                    $previus.val(old);

                                                    reorder($table);
                                                }

                                                return false;
                                            });

                                            $("table").on("click", ".order-next", function(e) {
                                                e.preventDefault();

                                                var $this    = $(this);
                                                var $table   = $this.parents("tbody");
                                                var $current = $this.parents("tr").find(".order");
                                                var $next = $this.parents("tr").next("tr").find(".order");

                                                if($next.length) {
                                                    var old = $current.val();
                                                    $current.val($next.val());
                                                    $next.val(old);

                                                    reorder($table);
                                                }

                                                return false;
                                            });
                                        });
                                       ';

    $flash = flash('error');
    if($flash && isset($form['errors']) && !empty($form['errors']))
        $flash = $flash.'<br> verifique os campos para mais detalhes.';
    flash('error', $flash); ?>
<?php echo $this->render('hwapx/admin/admin/header.html',NULL,get_defined_vars(),0); ?>
<style>
.select2-container--bootstrap .select2-selection {
    border-radius: 0 !important;
}
.only-print {
    display: none;
}
.content {
    overflow: visible;
}
@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;
    }
    .tab-pane {
        display: block !important;
    }
    .only-print {
        display: block !important;
    }
}
</style>
<div class="row">
    <?php if(!isset($form['width']))
            $form['width'] = 10; ?>
    <div class="col-lg-<?= ($form['width']) ?> col-lg-offset-<?= (round((12 - $form['width']) / 2)) ?> col-md-12 col-md-offset-0">
        <!-- Custom tabs (Charts with tabs)-->
        <div class="nav-tabs-custom">
            <!-- Tabs within a box -->
            <ul class="dont-print nav nav-tabs pull-right">
                <?php if (isset($form['groups'])): ?>
                    <?php $_groups = array_reverse($form['groups'], true); ?>
                    <?php $counter=0; foreach (($_groups?:[]) as $groupname=>$group): $counter++; ?>
                        <li class="<?= ($counter == count($form['groups']) ? 'active' : '') ?>"><a href="#group_<?= ($groupname) ?>" data-toggle="tab"><?= ($group['legend']) ?></a></li>
                    <?php endforeach; ?>
                <?php endif; ?>
                <li class="pull-left header"><i class="glyphicon glyphicon-pencil"></i> <?= ($form['title']) ?></li>
            </ul>
            <!-- form start -->
            <form id="main_form" role="form" method="post" action="<?= ($URI) ?>" enctype="multipart/form-data" class="print">
                <div class="box" style="border:none">
                    <div class="box-body">
                        <?php if(!isset($form['groups'])) $form['groups']['main'] = array('legend' => 'main', 'fields' => $form['fields']); ?>
                        <div class="tab-content no-padding">
                            <?php $counter=0; foreach (($form['groups']?:[]) as $groupname=>$group): $counter++; ?>
                                <?php if (\Hwapx\Core\Library\ModuleManager::instance()->hasRole($group['role'])): ?>
                                    <div id="group_<?= ($groupname) ?>" class="tab-pane<?= ($counter == 1 ? ' active' : '') ?>">
                                        <h2 class="only-print" style="margin-top: 0"><?= ($group['legend'] !== 'main' ? $group['legend'] : $form['name']) ?></h2>
                                        <div class="row">
                                            <div class="col-lg-12">
                                                <div class="row">
                                                    <?php if (isset($group['fields'])): ?>
                                                        <?php foreach (($group['fields']?:[]) as $fieldname=>$field): ?>
                                                            <?php $_value = isset($form['values'][$fieldname]) ? $form['values'][$fieldname] : (isset($field['default']) ? $field['default'] : '');
                                                                if(!isset($field['width']))
                                                                    $field['width'] = in_array($field['type'], array('textarea', 'wysiwyg', 'separator')) ? '12' : '6'; ?>
                                                            <div class="col-lg-<?= ($field['width']) ?> col-md-<?= ($field['width']) ?> <?= ($field['type'] == 'hidden' ? 'hidden' : '') ?>">
                                                                <div class="form-group<?= (isset($form['errors'][$fieldname]) ? ' has-error' : '') ?>">
                                                                    <?php if ($field['label'] and $field['type'] != 'separator'): ?>
                                                                        <label><?= ($field['label']) ?><?= ((isset($field['rules']) and in_array('required', $field['rules'])) ? ' <span class="text-danger">*</span>' : '') ?></label>
                                                                    <?php endif; ?>
                                                                    <?php if (isset($field['button'])): ?>
                                                                        <div class="input-group">
                                                                    <?php endif; ?>
                                                                    <?php switch ($field['type']): ?><?php case 'separator': ?>
                                                                            <?php if ($field['label']): ?>
                                                                                
                                                                                    <div style="text-align: center; border-top: lightgray solid 1px; border-bottom: lightgray solid 1px;"><label><?= ($field['label']) ?></label></div>
                                                                                
                                                                                <?php else: ?>
                                                                                    <hr>
                                                                                
                                                                            <?php endif; ?>
                                                                        <?php break; ?><?php case 'hidden': ?>
                                                                            <input class="form-control" name="<?= ($fieldname) ?>" type="hidden" value="<?= (isset($form['values'][$fieldname]) ? $form['values'][$fieldname] : (isset($field['default']) ? $field['default'] : '')) ?>" <?= (isset($field['attributes']) ? packXMLAttributes($field['attributes']) : '') ?> />
                                                                        <?php break; ?><?php case 'static': ?>
                                                                            <input class="form-control" readonly value="<?= (isset($form['values'][$fieldname]) ? (isset($field['options'][$form['values'][$fieldname]]) ? $field['options'][$form['values'][$fieldname]] : $form['values'][$fieldname]) : (isset($field['default']) ? $field['default'] : '')) ?>" <?= (isset($field['attributes']) ? packXMLAttributes($field['attributes']) : '') ?> />
                                                                        <?php break; ?><?php case 'staticarea': ?>
                                                                            <textarea class="form-control" readonly style="height:200px"><?= (isset($form['values'][$fieldname]) ? (isset($field['options'][$form['values'][$fieldname]]) ? $field['options'][$form['values'][$fieldname]] : $form['values'][$fieldname]) : (isset($field['default']) ? $field['default'] : '')) ?>"</textarea>
                                                                        <?php break; ?><?php case 'statichtml': ?>
                                                                            <div style="height:200px;border: 1px solid #555; background-color: #eee; overflow: scroll;"><?= ($this->raw(isset($form['values'][$fieldname]) ? (isset($field['options'][$form['values'][$fieldname]]) ? $field['options'][$form['values'][$fieldname]] : $form['values'][$fieldname]) : (isset($field['default']) ? $field['default'] : ''))) ?>"</div>
                                                                        <?php break; ?><?php case 'placeholder': ?>
                                                                            <div style="height:60px"></div>
                                                                        <?php break; ?><?php case 'wysiwyg': ?>
                                                                            <textarea class="form-control trumbowyg" name="<?= ($fieldname) ?>" style="height:300px;margin: 0;width: 100%;"><?= (isset($form['values'][$fieldname]) ? $form['values'][$fieldname] : (isset($field['default']) ? $field['default'] : '')) ?></textarea>
                                                                        <?php break; ?><?php case 'code': ?>
                                                                            <textarea class="form-control" id="codemirror_<?= ($fieldname) ?>" name="<?= ($fieldname) ?>" style="height:300px;margin: 0;width: 100%;"><?= (isset($form['values'][$fieldname]) ? $form['values'][$fieldname] : (isset($field['default']) ? $field['default'] : '')) ?></textarea>
                                                                            <?php $meta['scripts']['raw']['codemirror_'.$fieldname] = 'CodeMirror.fromTextArea(document.getElementById("codemirror_'.$fieldname.'"), '.(isset($field['options']) ? json_encode((object)$field['options']) : '{lineNumbers: true}').');'; ?>
                                                                            </script>
                                                                        <?php break; ?><?php case 'textarea': ?>
                                                                            <textarea class="form-control" name="<?= ($fieldname) ?>" style="height:200px"><?= (isset($form['values'][$fieldname]) ? $form['values'][$fieldname] : (isset($field['default']) ? $field['default'] : '')) ?></textarea>
                                                                        <?php break; ?><?php case 'checkbox': ?>
                                                                            <?php if (isset($field['options'])): ?>
                                                                                
                                                                                    <?php foreach (($field['options']?:[]) as $opt=>$lbl): ?>
                                                                                        <div class="checkbox">
                                                                                            <label>
                                                                                                <input class="" value="<?= ($opt) ?>" type="checkbox" name="<?= ($fieldname) ?>[<?= ($opt) ?>]" <?= ((isset($form['values'][$fieldname]) and in_array($opt, (array)$form['values'][$fieldname])) ? 'checked' : '') ?>/>
                                                                                                <?= ($lbl)."
" ?>
                                                                                            </label>
                                                                                        </div>
                                                                                    <?php endforeach; ?>
                                                                                
                                                                                <?php else: ?>
                                                                                    <div class="checkbox">
                                                                                        <label>
                                                                                            <input value="<?= ($field['value']) ?>" type="checkbox" name="<?= ($fieldname) ?>" <?= ((isset($form['values'][$fieldname]) and ($form['values'][$fieldname] == $field['value'])) ? 'checked' : '') ?>/>
                                                                                            Sim/Não
                                                                                        </label>
                                                                                    </div>
                                                                                
                                                                            <?php endif; ?>
                                                                        <?php break; ?><?php case 'radio': ?>
                                                                            <div class="radio">
                                                                                <?php foreach (($field['options']?:[]) as $opt=>$lbl): ?>
                                                                                        <label class="radio-inline">
                                                                                            <input class="" value="<?= ($opt) ?>" type="radio" name="<?= ($fieldname) ?>" <?= (((isset($form['values'][$fieldname]) && ($opt == $form['values'][$fieldname])) || (!isset($form['values'][$fieldname]) && isset($field['default']) && ($field['default'] == $opt))) ? 'checked' : '') ?>/>
                                                                                            <?= ($lbl)."
" ?>
                                                                                        </label>
                                                                                <?php endforeach; ?>
                                                                                <div style="height: 3px"></div>
                                                                            </div>
                                                                        <?php break; ?><?php case 'dropdown': ?>
                                                                            <select class="form-control select2" name="<?= ($fieldname) ?>" <?= (isset($field['attributes']) ? packXMLAttributes($field['attributes']) : '') ?>>
                                                                                <?php foreach (($field['options']?:[]) as $key=>$desc): ?>
                                                                                    <option value="<?= ($key) ?>"
                                                                                        <?= (isset($desc['attributes']) ? packXMLAttributes($desc['attributes']) : '')."
" ?>
                                                                                        <?= ($key == (isset($form['values'][$fieldname]) ?
                                                                                                   $form['values'][$fieldname] : (isset($field['default']) ?
                                                                                                                               $field['default'] : null)) ? ' selected' : '') ?>>
                                                                                        <?= (is_array($desc) ? $desc['label'] : $desc)."
" ?>
                                                                                    </option>
                                                                                <?php endforeach; ?>
                                                                            </select>
                                                                        <?php break; ?><?php case 'date': ?>
                                                                            <div class="input-group date">
                                                                                <input class="form-control" value="<?= (isset($form['values'][$fieldname]) ? $form['values'][$fieldname] : (isset($field['default']) ? $field['default'] : '')) ?>" type="text" name="<?= ($fieldname) ?>" data-date-format="<?= ($app['date']['format2']) ?>" <?= (isset($field['attributes']) ? packXMLAttributes($field['attributes']) : '') ?> />
                                                                                <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
                                                                            </div>
                                                                        <?php break; ?><?php case 'time': ?>
                                                                            <div class="input-group date">
                                                                                <input class="form-control <?= ($field['type']) ?>" value="<?= (isset($form['values'][$fieldname]) ? $form['values'][$fieldname] : (isset($field['default']) ? $field['default'] : '')) ?>" type="text" name="<?= ($fieldname) ?>" data-date-format="<?= ($app['time']['format2']) ?>" <?= (isset($field['attributes']) ? packXMLAttributes($field['attributes']) : '') ?> />
                                                                                <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
                                                                            </div>
                                                                        <?php break; ?><?php case 'datetime2': ?>
                                                                            <div class="input-group date">
                                                                                <input class="form-control <?= ($field['type']) ?>" value="<?= (isset($form['values'][$fieldname]) ? $form['values'][$fieldname] : (isset($field['default']) ? $field['default'] : '')) ?>" type="text" name="<?= ($fieldname) ?>" data-date-format="<?= ($app['datetime']['format2']) ?>" <?= (isset($field['attributes']) ? packXMLAttributes($field['attributes']) : '') ?> />
                                                                                <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
                                                                            </div>
                                                                        <?php break; ?><?php case 'datetime': ?>
                                                                            <div class="input-group date">
                                                                                <input
                                                                                    class="form-control <?= ($field['type']) ?>"
                                                                                    value="<?= (isset($form['values'][$fieldname]) ? $form['values'][$fieldname] : (isset($field['default']) ? $field['default'] : '')) ?>"
                                                                                    type="text" name="<?= ($fieldname) ?>"
                                                                                    data-date-format="<?= (isset($field['format']) ? $field['format'] : $app['datetime']['format2']) ?>"
                                                                                    <?= (isset($field['attributes']) ? packXMLAttributes($field['attributes']) : '') ?> />
                                                                                <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
                                                                            </div>
                                                                        <?php break; ?><?php case 'password': ?>
                                                                            <?php $__p = 'password_field_'.time().rand() ?>
                                                                            <button class="btn btn-default form-control"
                                                                                    type="button"
                                                                                    onclick="document.querySelector('.<?= ($__p) ?>').style.display = '';this.style.display = 'none'">
                                                                                Alterar <?= ($field['label'])."
" ?>
                                                                            </button>
                                                                            <input style="display:none;" class="form-control <?= ($__p) ?>" type="password" name="<?= ($fieldname) ?>" value="<?= (isset($form['values'][$fieldname]) ? '********' : '') ?>" />
                                                                        <?php break; ?><?php case 'image': ?>
                                                                            <input class="form-control file_image" value="<?= (isset($form['values'][$fieldname]) ? $form['values'][$fieldname] : (isset($field['default']) ? $field['default'] : '')) ?>" type="file" name="<?= ($fieldname) ?>" <?= ((isset($field['multiple']) and $field['multiple']) ? 'multiple' : '') ?> />
                                                                            <?php $__img_tag = isset($form['values'][$fieldname]) && file_exists($UPLOADS . $form['values'][$fieldname]) ? "\"<img src='" . $BASE . "/" . $UPLOADS . $form['values'][$fieldname] . "' class='file-preview-image' alt='Imagem'>\"" : "";
                                                                                $meta['scripts']['raw']['fileinput_'.$fieldname] = 'jQuery(document).ready(function($) {
                                                                                                           $("[name='.$fieldname.']").fileinput({
                                                                                                                 showUpload: false,
                                                                                                                 overwriteInitial: true,
                                                                                                                 initialPreview: [
                                                                                                                    '.$__img_tag.'
                                                                                                                ],
                                                                                                               });
                                                                                                        });'; ?>
                                                                        <?php break; ?><?php case 'number': ?>
                                                                            <input
                                                                                class="form-control text-right"
                                                                                value="<?= (str_replace($this->f3->get('app.number.thousands'), '', $_value)) ?>"
                                                                                data-inputmask="'alias': 'numeric', 'groupSeparator': '<?= ($app['number']['thousands']) ?>', 'radixPoint': '<?= ($app['number']['radixpoint']) ?>', 'autoGroup': true, 'digits': <?= (isset($field['digits']) ? $field['digits'] : 0) ?>, 'digitsOptional': false, 'prefix': '', 'placeholder': '0'"
                                                                                type="text"
                                                                                name="<?= ($fieldname) ?>"
                                                                                <?= (isset($field['attributes']) ? packXMLAttributes($field['attributes']) : '')."
" ?>
                                                                            />
                                                                        <?php break; ?><?php default: ?>
                                                                            <?php if (isset($field['type']) && $field['type'] == 'text' && isset($field['options'])): ?>
                                                                                <datalist id="datalist_<?= ($fieldname) ?>">
                                                                                    <?php foreach (($field['options']?:[]) as $option_value=>$option_text): ?>
                                                                                        <option value="<?= ($option_text) ?>"><?= ($option_value) ?></option>
                                                                                    <?php endforeach; ?>
                                                                                </datalist>
                                                                            <?php endif; ?>
                                                                            <input
                                                                                class="form-control"
                                                                                value="<?= ($_value) ?>"
                                                                                type="<?= (isset($field['type']) ? $field['type'] : 'text') ?>"
                                                                                <?= (isset($field['mask']) ? 'data-inputmask-clearIncomplete="true" data-inputmask-skip-optional-part-character=" " data-inputmask data-inputmask-mask="'.$field['mask'].'"' : '')."
" ?>
                                                                                <?= (isset($field['type']) && $field['type'] == 'text' && isset($field['options']) ? 'list="datalist_'.$fieldname.'"' : '')."
" ?>
                                                                                <?= (isset($field['attributes']) ? packXMLAttributes($field['attributes']) : '')."
" ?>
                                                                                name="<?= ($fieldname) ?>" />
                                                                        <?php break; ?><?php endswitch; ?>
                                                                    <?php if (isset($field['button'])): ?>
                                                                        <span class="input-group-btn">
                                                                            <button class="btn btn-default" type="button" onclick="<?= ($field['button']['action']) ?>" id="<?= (isset($field['button']['id']) ? $field['button']['id'] : $fieldname.'_action') ?>" <?= (isset($field['button']['attributes']) ? packXMLAttributes($field['button']['attributes']) : '') ?>>
                                                                                <?php if (isset($field['button']['icon'])): ?>
                                                                                <span class="glyphicon glyphicon-<?= ($field['button']['icon']) ?>"></span>
                                                                                <?php endif; ?>
                                                                                <?php if (isset($field['button']['text'])): ?>
                                                                                <?= ($field['button']['text'])."
" ?>
                                                                                <?php endif; ?>
                                                                            </button>
                                                                        </span>
                                                                    </div>
                                                                    <?php endif; ?>
                                                                    
                                                                    <?php if (isset($form['errors'][$fieldname])): ?>
                                                                        
                                                                            
                                                                            <p class="help-block"><?= (implode($form['errors'][$fieldname], ', ')) ?></p>
                                                                        
                                                                        <?php else: ?>
                                                                            <?php if (isset($form['errors'])): ?>
                                                                            <p class="help-block">&nbsp;</p>
                                                                            <?php endif; ?>
                                                                        
                                                                    <?php endif; ?>
                                                                </div>
                                                            </div>
                                                        <?php endforeach; ?>
                                                    <?php endif; ?>
                                                    <?php if (isset($group['table'])): ?>
                                                        <div class="col-md-12" style="margin-bottom: 10px;">
                                                            <?php $_groupdata = (boolean)isset($group['group']);
                                                                $_table = array(null);
                                                                if(isset($form['values'][$groupname]))
                                                                    $_table += $form['values'][$groupname];
                                                                //$_table[0] = null;
                                                                //array_unshift($_table, null);
                                                                $_table_row_count = 0;//count($_table);
                                                                foreach ($_table as $idx =>$_dummy):
                                                                    $_table_row_count = max($_table_row_count,@$idx);
                                                                endforeach;
                                                                $_showactioncolumn = !isset($group['allow_insert']) || !isset($group['allow_delete']) || $group['allow_insert'] || $group['allow_delete']; ?>
                                                            <table class="table table-striped" <?= (isset($group['order']) && (!isset($group['sort']) || $group['sort']) ? 'data-sortfield="'.$group['order'].'"' : '') ?> id="table_<?= ($groupname) ?>">
                                                                <thead>
                                                                    <tr>
                                                                        <?php if (isset($group['order']) && (!isset($group['sort']) || $group['sort'])): ?>
                                                                            <th class="dont-print" style="width: 84px;min-width: 84px;max-width: 84px;">
                                                                            Ordem
                                                                            </th>
                                                                        <?php endif; ?>
                                                                        <?php foreach (($group['table']?:[]) as $fieldname=>$field): ?>
                                                                            <th data-fieldname="<?= ($fieldname) ?>" class="<?= ($field['type'] == 'hidden' ? 'hidden' : '') ?>"><?= ($field['label']) ?><?= ((isset($field['rules']) and in_array('required', $field['rules'])) ? ' <span class="text-danger">*</span>' : '') ?></th>
                                                                            <?php if (isset($field['type']) && $field['type'] == 'text' && isset($field['options'])): ?>
                                                                                <datalist id="datalist_<?= ($groupname) ?>[<?= ($fieldname) ?>]">
                                                                                    <?php foreach (($field['options']?:[]) as $option_value=>$option_text): ?>
                                                                                        <option value="<?= ($option_text) ?>"><?= ($option_value) ?></option>
                                                                                    <?php endforeach; ?>
                                                                                </datalist>
                                                                            <?php endif; ?>
                                                                        <?php endforeach; ?>
                                                                        <?php if ($_showactioncolumn): ?>
                                                                        <th class="dont-print" style="width: 10px">
                                                                            <?php if (!isset($group['allow_insert']) || $group['allow_insert']): ?>
                                                                                
                                                                                    <a href="javascript:_appendrow('<?= ($groupname) ?>')" class="btn btn-primary btn-md btn-flat"><i class="glyphicon glyphicon-plus"></i></a>
                                                                                
                                                                                <?php else: ?>
                                                                                    <i class="glyphicon glyphicon-flash"></i>
                                                                                
                                                                            <?php endif; ?>
                                                                        </th>
                                                                        <?php endif; ?>
                                                                    </tr>
                                                                </thead>
                                                                <tbody data-row-count="<?= ($_table_row_count) ?>">
                                                                    <?php $ic=0; foreach (($_table?:[]) as $c=>$row): $ic++; ?>
                                                                        <?php if ($ic != 1 && $_groupdata !== false && $_groupdata !== $row[$group['group']]): ?>
                                                                            <?php $_groupdata = $row[$group['group']] ?>
                                                                            <tr>
                                                                                <th colspan="<?= (count($group['table']) + (isset($group['order']) && (!isset($group['sort']) || $group['sort']) ? 1 : 0) + ($_showactioncolumn ? 1 : 0)) ?>">
                                                                                    <h4><strong><?= ($_groupdata) ?></strong></h4>
                                                                                </th>
                                                                            </tr>
                                                                        <?php endif; ?>
                                                                        <?php $_dont_post = $ic == 1 ?>
                                                                        <tr <?= ($_dont_post ? 'class="dont-print dont_post" style="display:none" id="row_'.$groupname.'"' : '') ?>>
                                                                            <td data-fieldname="id" style="display:none;">
                                                                                <input type="hidden" name="<?= ($groupname) ?>[<?= ($c) ?>][id]" value="<?= (isset($row['id']) ? $row['id'] : '') ?>">
                                                                            </td>
                                                                            <?php if (isset($group['order']) && (!isset($group['sort']) || $group['sort'])): ?>
                                                                            <td class="dont-print" data-fieldname="<?= ($group['order']) ?>">
                                                                                <div style="width: 84px;min-width: 84px;max-width: 84px;">
                                                                                    <?php if ((($ic == 1 || !$row['id']) && (!isset($group['allow_insert']) || $group['allow_insert'])) || (!isset($group['allow_edit']) || $group['allow_edit'])): ?>
                                                                                    
                                                                                        <input type="hidden" name="<?= ($groupname) ?>[<?= ($c) ?>][<?= ($group['order']) ?>]" value="<?= (isset($row[$group['order']]) ? $row[$group['order']] : '') ?>" class="order">
                                                                                        <a href="javascript:void(0);" class="btn btn-default btn-flat btn-md order-prior">
                                                                                            <i class="glyphicon glyphicon-chevron-up" style="cursor: pointer;"></i>
                                                                                        </a>
                                                                                        <a href="javascript:void(0);" class="btn btn-default btn-flat btn-md order-next">
                                                                                            <i class="glyphicon glyphicon-chevron-down" style="cursor: pointer;"></i>
                                                                                        </a>
                                                                                    
                                                                                    <?php else: ?>
                                                                                        <span><?= ($c) ?></span>
                                                                                    
                                                                                    <?php endif; ?>
                                                                                </div>
                                                                            </td>
                                                                            <?php endif; ?>
                                                                            <?php foreach (($group['table']?:[]) as $fieldname=>$field): ?>
                                                                                <?php $_value = isset($row[$fieldname]) ? (isset($field['options'][$row[$fieldname]]) ? $field['options'][$row[$fieldname]] : $row[$fieldname]) : (isset($field['default']) ? $field['default'] : '') ?>
                                                                                <?php $_value_raw = isset($row[$fieldname]) ? $row[$fieldname] : (isset($field['default']) ? $field['default'] : '') ?>
                                                                                <td data-fieldname="<?= ($fieldname) ?>" style="vertical-align: middle;<?= ($field['type'] == 'hidden' ? 'display:none;' : '') ?>">
                                                                                    <div class="<?= (isset($form['errors'][$groupname][$c][$fieldname]) ? 'form-group has-error' : '') ?>">
                                                                                        <?php switch ((($ic == 1 || !$row['id']) && (!isset($group['allow_insert']) || $group['allow_insert'])) || (!isset($group['allow_edit']) || $group['allow_edit']) ? $field['type'] : 'static'): ?><?php case 'static': ?>
                                                                                                <p class="form-control-static"><?= (nl2br($_value)) ?></p>
                                                                                                <input
                                                                                                    value="<?= ($_value_raw) ?>"
                                                                                                    type="hidden"
                                                                                                    name="<?= ($groupname) ?>[<?= ($c) ?>][<?= ($fieldname) ?>]"
                                                                                                />
                                                                                            <?php break; ?><?php case 'wysiwyg': ?>
                                                                                                <textarea class="form-control trumbowyg" name="<?= ($groupname) ?>[<?= ($c) ?>][<?= ($fieldname) ?>]" style="height:300px;margin: 0;width: 100%;"><?= (isset($form['values'][$fieldname]) ? $row[$fieldname] : (isset($field['default']) ? $field['default'] : '')) ?></textarea>
                                                                                            <?php break; ?><?php case 'textarea': ?>
                                                                                                <textarea class="form-control" name="<?= ($groupname) ?>[<?= ($c) ?>][<?= ($fieldname) ?>]" style="height:200px"><?= (isset($row[$fieldname]) ? $row[$fieldname] : (isset($field['default']) ? $field['default'] : '')) ?></textarea>
                                                                                            <?php break; ?><?php case 'checkbox': ?>
                                                                                                <?php if (isset($field['options'])): ?>
                                                                                                    
                                                                                                        <?php foreach (($field['options']?:[]) as $opt=>$lbl): ?>
                                                                                                            <div class="checkbox">
                                                                                                                <label>
                                                                                                                    <input class="" value="<?= ($opt) ?>" type="checkbox" name="<?= ($groupname) ?>[<?= ($c) ?>][<?= ($fieldname) ?>][<?= ($opt) ?>]" <?= ((isset($row[$fieldname]) and in_array($opt, $row[$fieldname])) ? 'checked' : '') ?>/>
                                                                                                                    <?= ($lbl)."
" ?>
                                                                                                                </label>
                                                                                                            </div>
                                                                                                        <?php endforeach; ?>
                                                                                                    
                                                                                                    <?php else: ?>
                                                                                                        <div class="checkbox">
                                                                                                            <label>
                                                                                                                <input value="<?= ($field['value']) ?>" type="checkbox" name="<?= ($groupname) ?>[<?= ($c) ?>][<?= ($fieldname) ?>]" <?= ((isset($row[$fieldname]) and ($row[$fieldname] == $field['value'])) ? 'checked' : '') ?>/>
                                                                                                                Sim/Não
                                                                                                            </label>
                                                                                                        </div>
                                                                                                    
                                                                                                <?php endif; ?>
                                                                                            <?php break; ?><?php case 'radio': ?>
                                                                                                <div class="radio">
                                                                                                    <?php foreach (($field['options']?:[]) as $opt=>$lbl): ?>
                                                                                                            <label class="radio-inline">
                                                                                                                <input class="" value="<?= ($opt) ?>" type="radio" name="<?= ($groupname) ?>[<?= ($c) ?>][<?= ($fieldname) ?>]" <?= (((isset($row[$fieldname]) && ($opt == $row[$fieldname])) || (!isset($form['values'][$fieldname]) && isset($field['default']) && ($field['default'] == $opt))) ? 'checked' : '') ?>/>
                                                                                                                <?= ($lbl)."
" ?>
                                                                                                            </label>
                                                                                                    <?php endforeach; ?>
                                                                                                    <div style="height: 3px"></div>
                                                                                                </div>
                                                                                            <?php break; ?><?php case 'dropdown': ?>
                                                                                                <select class="form-control<?= ($_dont_post ? '' : ' select2') ?>" name="<?= ($groupname) ?>[<?= ($c) ?>][<?= ($fieldname) ?>]">
                                                                                                    <?php foreach (($field['options']?:[]) as $key=>$desc): ?>
                                                                                                        <option value="<?= ($key) ?>" <?= ($key == (isset($row[$fieldname]) ?
                                                                                                                                               $row[$fieldname] : (isset($field['default']) ?
                                                                                                                                                                          $field['default'] : null)) ? ' selected' : '') ?>><?= ($desc) ?></option>
                                                                                                    <?php endforeach; ?>
                                                                                                </select>
                                                                                            <?php break; ?><?php case 'date': ?>
                                                                                                <div class="input-group date">
                                                                                                    <input
                                                                                                        class="form-control"
                                                                                                        value="<?= (isset($row[$fieldname]) ? $row[$fieldname] : (isset($field['default']) ? $field['default'] : '')) ?>"
                                                                                                        type="text"
                                                                                                        name="<?= ($groupname) ?>[<?= ($c) ?>][<?= ($fieldname) ?>]"
                                                                                                        data-date-format="<?= ($app['date']['format2']) ?>"
                                                                                                        <?= (isset($field['attributes']) ? packXMLAttributes($field['attributes']) : '') ?>>
                                                                                                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                                                                                                </div>
                                                                                            <?php break; ?><?php case 'time': ?>
                                                                                                <div class="input-group date">
                                                                                                    <input
                                                                                                        class="form-control"
                                                                                                        value="<?= (isset($row[$fieldname]) ? $row[$fieldname] : (isset($field['default']) ? $field['default'] : '')) ?>"
                                                                                                        type="text"
                                                                                                        name="<?= ($groupname) ?>[<?= ($c) ?>][<?= ($fieldname) ?>]"
                                                                                                        data-date-format="<?= ($app['time']['format2']) ?>"
                                                                                                        <?= (isset($field['attributes']) ? packXMLAttributes($field['attributes']) : '') ?>>
                                                                                                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                                                                                                </div>
                                                                                            <?php break; ?><?php case 'datetime': ?>
                                                                                                <div class="input-group date">
                                                                                                    <input
                                                                                                        class="form-control"
                                                                                                        value="<?= (isset($row[$fieldname]) ? $row[$fieldname] : (isset($field['default']) ? $field['default'] : '')) ?>"
                                                                                                        type="text"
                                                                                                        name="<?= ($groupname) ?>[<?= ($c) ?>][<?= ($fieldname) ?>]"
                                                                                                        data-date-format="<?= (isset($field['format']) ? $field['format'] : $app['datetime']['format2']) ?>"
                                                                                                        <?= (isset($field['attributes']) ? packXMLAttributes($field['attributes']) : '') ?>>
                                                                                                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                                                                                                </div>
                                                                                            <?php break; ?><?php case 'password': ?>
                                                                                                <input class="form-control" type="password" name="<?= ($groupname) ?>[<?= ($c) ?>][<?= ($fieldname) ?>]" value="<?= (isset($row[$fieldname]) ? '********' : '') ?>" />
                                                                                            <?php break; ?><?php case 'image': ?>
                                                                                                <input class="form-control file_image" value="<?= (isset($row[$fieldname]) ? $row[$fieldname] : (isset($field['default']) ? $field['default'] : '')) ?>" type="file" name="<?= ($groupname) ?>[<?= ($c) ?>][<?= ($fieldname) ?>]" <?= ((isset($field['multiple']) and $field['multiple']) ? 'multiple' : '') ?> />
                                                                                                <?php $__img_tag = isset($row[$fieldname]) && file_exists($UPLOADS . $row[$fieldname]) ? "\"<img src='/" . $UPLOADS . $row[$fieldname] . "' class='file-preview-image' alt='Imagem'>\"" : "";
                                                                                                    $meta['scripts']['raw']['fileinput_'.$fieldname] = 'jQuery(document).ready(function($) {
                                                                                                                               $("[name='.$fieldname.']").fileinput({
                                                                                                                                     showUpload: false,
                                                                                                                                     showPreview: false,
                                                                                                                                     overwriteInitial: true,
                                                                                                                                     initialPreview: [
                                                                                                                                        '.$__img_tag.'
                                                                                                                                    ],
                                                                                                                                   });
                                                                                                                            });'; ?>
                                                                                            <?php break; ?><?php case 'number': ?>
                                                                                                <input
                                                                                                    class="form-control text-right"
                                                                                                    value="<?= (str_replace($this->f3->get('app.number.thousands'), '', $_value)) ?>"
                                                                                                    data-inputmask="'alias': 'numeric', 'groupSeparator': '<?= ($app['number']['thousands']) ?>', 'radixPoint': '<?= ($app['number']['radixpoint']) ?>', 'autoGroup': true, 'digits': <?= (isset($field['digits']) ? $field['digits'] : 0) ?>, 'digitsOptional': false, 'prefix': '', 'placeholder': '0'"
                                                                                                    type="text"
                                                                                                    name="<?= ($groupname) ?>[<?= ($c) ?>][<?= ($fieldname) ?>]"
                                                                                                />
                                                                                            <?php break; ?><?php default: ?>
                                                                                                <input
                                                                                                    class="form-control"
                                                                                                    value="<?= ($_value) ?>"
                                                                                                    type="<?= (isset($field['type']) ? $field['type'] : 'text') ?>"
                                                                                                    <?= (isset($field['mask']) ? 'data-inputmask-clearIncomplete="true" data-inputmask-skipOptionalPartCharacter: " " data-inputmask data-inputmask-mask="'.$field['mask'].'"' : '')."
" ?>
                                                                                                    <?= (isset($field['attributes']) ? packXMLAttributes($field['attributes']) : '')."
" ?>
                                                                                                    name="<?= ($groupname) ?>[<?= ($c) ?>][<?= ($fieldname) ?>]"
                                                                                                    <?= (isset($field['type']) && $field['type'] == 'text' && isset($field['options']) ? "list=\"datalist_{$groupname}[$fieldname]\"" : '')."
" ?>
                                                                                                />
                                                                                            <?php break; ?><?php endswitch; ?>
                                                                                        <?php if (isset($form['errors'][$groupname][$c][$fieldname])): ?>
                                                                                            
                                                                                                
                                                                                                <p class="help-block"><?= (implode($form['errors'][$groupname][$c][$fieldname], ', ')) ?></p>
                                                                                            
                                                                                            <?php else: ?>
                                                                                                <?php if (isset($form['errors'][$groupname][$c])): ?>
                                                                                                <p class="help-block">&nbsp;</p>
                                                                                                <?php endif; ?>
                                                                                            
                                                                                        <?php endif; ?>
                                                                                    </div>
                                                                                </td>
                                                                            <?php endforeach; ?>
                                                                            <?php if (!isset($group['allow_delete']) || $group['allow_delete']): ?>
                                                                            <td class="dont-print" style="width: 10px"><a href="javascript:void(0);" onclick="jQuery(this).parent().parent().remove();" class="btn btn-danger btn-md btn-flat"><i class="glyphicon glyphicon-remove"></i></a></td>
                                                                            <?php endif; ?>
                                                                        </tr>
                                                                    <?php endforeach; ?>
                                                                </tbody>
                                                            </table>
                                                        </div>
                                                    <?php endif; ?>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </div>
                        <div class="dont-print">
                            <input type="submit" class="btn btn-primary btn-flat" value="Salvar">
                            <!--<input type="reset" class="btn btn-default btn-flat" value="Redefinir" onclick="return confirm('Confima a redefinição dos dados do formulário?');">-->
                            <a href="<?= ($BASE) ?><?= (isset($form['table']) ? F3::alias($form['table']) : F3::alias('admin_dashboard')) ?>" class="btn btn-warning btn-flat">Cancelar</a>
                            <?php if (isset($form['actions'])): ?>
                                <div class="pull-right">
                                <?php $counter=0; foreach (($form['actions']?:[]) as $action): $counter++; ?>
                                    <?php if (\Hwapx\Core\Library\ModuleManager::instance()->hasRole($action['role'])): ?>
                                        
                                            <?php if (isset($action['params'])): ?>
                                                <?php foreach (($action['params']?:[]) as $param=>$field): ?>
                                                    <?php F3::set('PARAMS.'.$param, $row[$field]) ?>
                                                <?php endforeach; ?>
                                            <?php endif; ?>
                                            <?php if ($counter < $app['table']['maxactions'] or count($form['actions']) <= $app['table']['maxactions']): ?>
                                                
                                                    <a href="<?= (isset($ALIASES[$action['route']]) ? $BASE.\F3::alias($action['route']) : $action['route']) ?>" class="btn <?= (isset($action['class']) ? $action['class'] : 'btn-default') ?> btn-flat" onclick="<?= ((isset($action['confirm']) and $action['confirm']) ? 'return confirm(\''.addslashes(is_string($action['confirm']) ? $action['confirm'] : 'Confirma esta ação?').'\');' : '') ?>"><?= ($action['description']) ?></a>
                                                
                                                <?php else: ?>
                                                    <?php if ($counter == $app['table']['maxactions']): ?>
                                                        <div class="btn-group">
                                                            <button type="button" class="btn btn-default btn-flat dropdown-toggle" data-toggle="dropdown">
                                                                Mais Ações
                                                                <span class="caret"></span>
                                                            </button>
                                                            <ul class="dropdown-menu pull-right" role="menu" style="top: auto; bottom: 36px;">
                                                    <?php endif; ?>
                                                        <li>
                                                            <?php if (is_array($action)): ?>
                                                                
                                                                    <a href="<?= (isset($ALIASES[$action['route']]) ? $BASE.\F3::alias($action['route']) : $action['route']) ?>" onclick="<?= ((isset($action['confirm']) and $action['confirm']) ? 'return confirm(\''.addslashes(is_string($action['confirm']) ? $action['confirm'] : 'Confirma esta ação?').'\');' : '') ?>"><?= ($action['description']) ?></a>
                                                                
                                                                <?php else: ?>
                                                                    <li class="divider"></li>
                                                                
                                                            <?php endif; ?>
                                                        </li>
                                                    <?php if ($counter == count($form['actions'])): ?>
                                                            </ul>
                                                        </div>
                                                    <?php endif; ?>
                                                
                                            <?php endif; ?>
                                        
                                        <?php else: ?>
                                            <?php $counter-- ?>
                                        
                                    <?php endif; ?>
                                <?php endforeach; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </form>
        </div><!-- /.nav-tabs-custom -->
    </div>
</div>   <!-- /.row -->
<?php echo $this->render('hwapx/admin/admin/footer.html',NULL,get_defined_vars(),0); ?>
