<!-- FormPrefix --><?php
$table_fields = array();
foreach ($component->get_fields() as $field) {
    if ($field['in_table_view']) {
        $table_fields[$field['name']] = $field;
    }
}

$standart_table_fields = array();
foreach ($component->get_standart_fields() as $field) {
    if ($field['in_table_view']) {
        $standart_table_fields[$field['name']] = $field;
    }
}
?>

<div id='nc_table_view_<?= $cc ?>' class="nc-padding-20" style='padding-left:0 !important'>
    <table class='nc-table nc--bordered nc--striped nc--wide'>
        <thead>
        <tr>
            <th width='1%'>#</th>
            <? if ($nc_core->get_settings('PacketOperations')): ?>
                <th width='1%'></th>
            <? endif ?>
            <th width='1%'></th>
            <? if (!$table_fields && !$standart_table_fields): ?>
                <th></th>
            <? endif ?>
            <? foreach (array_merge($table_fields, $standart_table_fields) as $field): ?>
                <th><?= $field['description'] ?></th>
            <? endforeach ?>
            <th class="nc-text-center">
                <div class="icons icon_prior" title="Приоритет"></div>
            </th>
            <th colspan="4" class='nc-text-right'>
                <a href='#' id='nc_table_view_settings_btn' title=" <?= STRUCTURE_TAB_SETTINGS ?>"><i class='nc-icon nc--settings'></i></a>
            </th>
        </tr>
        </thead>
        <tbody>
        <!-- /FormPrefix -->

        <!-- RecordTemplate --><? /* Служебная часть */
        for ($f_RowNum = 0;
        $f_RowNum < $rowCount;
        $f_RowNum++) {
        if ($fetch_row[$f_RowNum] instanceof Iterator) {
            extract($fetch_row[$f_RowNum]->to_array(), EXTR_PREFIX_ALL, "f");
        } else {
            extract($fetch_row[$f_RowNum], EXTR_PREFIX_ALL, "f");
        }
        foreach ($iteration_RecordTemplate[$f_RowNum] as $value) {
            extract($value);
        }
        eval($cc_env["convert2txt"]);
        ob_start();
        /* Конец служебной части */
        ?>

        <tr>
            <td><?= $f_RowID ?></td>
            <? if ($nc_core->get_settings('PacketOperations')): ?>
                <td>
                    <input class='nc_multi_check' type='checkbox' onchange='nc_package_obj.select(<?= $f_RowID ?>, <?= $cc ?>)'>
                </td>
            <? endif ?>
            <td>
                <a href="<?= $f_AdminButtons_check ?>" class="nc--wide nc-label nc--<?= $f_Checked ? 'green' : 'red' ?>" title="<?= constant('NETCAT_MODERATION_OBJ_' . ($f_Checked ? 'ON' : 'OFF')) ?>">
                    <?= constant('NETCAT_MODERATION_OBJ_' . ($f_Checked ? 'ON' : 'OFF')) ?>
                </a>
            </td>

            <? if (!$table_fields && !$standart_table_fields): ?>
                <td><?= NETCAT_MODULE_COMMENTS_ADMIN_MESSAGE . ' #' . $f_RowID ?></td>
            <? else: ?>
                <? foreach (array_merge($table_fields, $standart_table_fields) as $fieldname => $field): ?>
                    <td<?= $f_Checked ? '' : " class='nc-text-gray'" ?>>
                        <?php
                        if ($fieldname == 'User') {
                            $user_id = $f_User_ID;
                            $f_User = '';
                            if ($user_id) {
                                try {
                                    $user_object = nc_core('user')->get_by_id($user_id);
                                    $f_User = $user_object['Login'];
                                } catch (Exception $e) {
                                }
                            }
                            $value = $f_User;
                        } else if ($fieldname == 'LastUser') {
                            $user_id = $f_LastUser_ID;
                            $f_LastUser = '';
                            if ($user_id) {
                                try {
                                    $user_object = nc_core('user')->get_by_id($user_id);
                                    $f_LastUser = $user_object['Login'];
                                } catch (Exception $e) {
                                }
                            }
                            $value = $f_LastUser;
                        } else {
                            $value = ${'f_' . $fieldname};

                            if ($field['type'] == NC_FIELDTYPE_FILE) { // File
                                if (strpos($field['format'], 'image/')) { // Image
                                    $value = "<img src='{$value}' alt='' style='max-height:80px;max-width:80px'>";
                                }
                            }
                        }
                        ?>

                        <?= $value ?>
                    </td>
                <? endforeach ?>
            <? endif ?>

            <? /* PRIORITY */ ?>
            <td width="1">
                <div class="nc-form">
                    <input type="hidden" name="Checked[<?= $f_RowID; ?>]" value="<?= $f_Checked; ?>"/>
                    <input type="text" name="priority[<?= $f_RowID; ?>]" size="3" value="<?= $f_Priority; ?>" class="nc--mini" maxlength="5" style="margin-top: -5px; margin-bottom: -7px;">
                </div>
            </td>

            <? /* ACTIONS */ ?>
            <td class='nc--action' width="1">
                <a onclick="window.open('<?= $f_AdminButtons_copy ?>', 'nc_popup_test1', 'width=800,height=500,menubar=no,resizable=no,scrollbars=no,toolbar=no,resizable=yes'); return false;" href="#" title="<?= NETCAT_MODERATION_COPY_OBJECT ?>"><i class='nc-icon nc--copy'></i></a>
            </td>
            <td class='nc--action' width="1">
                <a onclick="parent.nc_form(this.href); return false;" href="<?= $f_AdminButtons_change ?>" title="<?= NETCAT_MODERATION_CHANGE ?>"><i class='nc-icon nc--edit'></i></a>
            </td>
            <? if (!empty($f_AdminButtons_version)) : ?>
                <td class='nc--action' width="1">
                    <a onclick="parent.nc_form(this.href); return false;" href="<?= $f_AdminButtons_version ?>" title="<?= NETCAT_MODERATION_VERSION ?>"><i class='nc-icon nc--clock'></i></a>
                </td>
            <? else: ?>
                <td width="1" style='padding: 0px; margin: 0px;'></td>
            <? endif; ?>
            <td class='nc--action' width="1">
                <a onclick="parent.nc_action_message(this.href); return false;" href="<?= $f_AdminButtons_delete ?>" title="<?= NETCAT_MODERATION_DELETE ?>"><i class='nc-icon nc--remove'></i></a>
            </td>
        </tr>


        <?/* Служебная часть */
    echo nc_finishing_RecordTemplate(ob_get_clean(), $inside_admin, $classID, $f_RowID, $parent_message, $cc, $cc_env["Class_Name"], $no_cache_marks);
}
/* Конец служебной части */?><!-- /RecordTemplate -->

        <!-- FormSuffix --></tbody>
    </table>
    <br>

    <div class="nc-pagination nc--right">
        <?= CONTROL_CLASS_CLASS_OBJECTSLIST_SHOW ?>:
        <!--span class='nc--active'>по умолчанию <?=$maxRows ?></span-->
        <? foreach (array(10, 20, 50, 100) as $per_page): ?>
            <? if ($per_page == $maxRows): ?>
                <span class='nc--active'><?= $per_page ?></span>
            <? else: ?>
                <a href="<?= $SUB_FOLDER . $HTTP_ROOT_PATH ?>?inside_admin=1&amp;cc=<?= $cc ?>&amp;maxRows=<?= $per_page ?>"><?= $per_page ?></a>
            <? endif ?>
        <? endforeach ?>
    </div>

    <?=
    browse_messages($cc_env, 15, array(
        'prefix' => "<div class='nc-pagination'>",
        'suffix' => "</div>",
        'active' => "<span class='nc--active'>%PAGE</span>",
        'unactive' => "<a href='%URL'>%PAGE</a>",
        'divider' => " ",
    )) ?>
</div>

<div id='nc_table_view_settings' class='nc-padding-20 nc--hide' style='padding-left:0 !important'>

    <form class='nc-panel nc-padding-15' method='post' action='<?= $SUB_FOLDER . $HTTP_ROOT_PATH ?>?inside_admin=1&amp;cc=<?= $cc ?>'>
        <? foreach (array_merge($component->get_fields(), $component->get_standart_fields()) as $field): ?>
            <div>
                <label>
                    <input name='in_table_view<?= isset($field['standart']) && $field['standart'] ? '_standart' : ''; ?>[]' value='<?= $field['id'] ?>' type="checkbox" <?= $field['in_table_view'] ? 'checked="checked"' : '' ?>>
                    <?= $field['description'] ?>
                </label>
            </div>
        <? endforeach ?>
        <br>
        <input type="hidden" name="table_view_settings" value="1">
        <button type='submit' class='nc-btn nc--blue nc--small'><?= NETCAT_CUSTOM_ONCE_SAVE ?></button>
        <button onclick="return nc_toggle_table_view_settings()" type='button' class='nc-btn nc--red nc--small nc--bordered'><?= CONTROL_BUTTON_CANCEL ?></button>
    </form>
</div>


<script>
    function nc_toggle_table_view_settings() {
        nc('#nc_table_view_<?=$cc ?>').toggle();
        nc('#nc_table_view_settings').toggle();
        return false;
    }
    nc('#nc_table_view_settings_btn').click(nc_toggle_table_view_settings);

    $nc(function () {
        $nc(document).on('change', 'INPUT[name^="priority["]', function () {
            var $this = $nc(this);
            var message = /^priority\[(\d+)\]/.exec($this.attr('name'));
            message = message[1];
            var priority = parseInt($this.val());
            priority = isNaN(priority) ? 0 : Math.abs(priority);
            $this.val(priority);

            var checked = $nc('INPUT[name="Checked[' + message + ']"]').val() - 0 + 1;

            var url = '<?= $SUB_FOLDER . $HTTP_ROOT_PATH . "message.php?inside_admin=1&catalogue=" . $catalogue . "&sub=" . $sub . "&cc=" . $cc . "&classID=" . $classID . "&posting=1" . ($curPos ? "&curPos=" . $curPos : "") . ($admin_mode ? "&admin_mode=1" : ""); ?>';
            url += '&message=' + message + '&priority=' + priority + '&checked=' + checked;

            var priorities = [];

            $this.closest('TABLE').find('TBODY TR').each(function () {
                priorities.push({
                    priority: parseInt($(this).find('INPUT[name^="priority["]').val()),
                    row: $(this)
                });
            });

            priorities.sort(function (a, b) {
                return a.priority - b.priority;
            });

            priorities.reverse();

            for (var i in priorities) {
                $this.closest('TABLE').find('TBODY').append(priorities[i].row);
            }

            nc.process_start('priority_change');
            $nc.get(url, function () {
                nc.process_stop('priority_change');
            });

            return true;
        });
    });
</script><!-- /FormSuffix -->