<?php
    require_once($_SERVER['DOCUMENT_ROOT'] . '/conf.php');
    session_start();

    if (session("groups") !== 'admin') {
        $res = message_success("Доступы к серверу: =>", [".!.."]);
        send_json($res);
    }

    //log_write($_POST);

    $id = (int)post('id');
    $status = (int)post('status');

    $delete = false;
    if (post('delete')) {
        $delete = (int)post('delete');
    }

    $table_name = post('table_name');
    $where = "1 = 1";
    $limit = "LIMIT ".$settings_records_per_page;

    if ($id) {
        $where = " `id` < ".escape_db($id);
    }

    if ($id < 0) {
        $limit = '';
        $where = " `id` < ".escape_db($id * -1);
    }

    if (!isset($_POST['status'])) {
        $res = message_error("Ошибка получения данных, не передано поле статус");
        send_json($res);
    }

    if (isset($status)) {
        if ($where === "1 = 1") {
            $where = " `status` = ".escape_db($status);
        } else {
            $where .= " AND `status` = ".escape_db($status);
        }
    }

    if ($delete) {
        $where .= " AND `delete` = ".escape_db($delete);
    }

    $data = get_list($table_name, $where, 'id DESC '.$limit);

    $res = message_error("Ошибка получения данных");
    if  ($data) {
        $res = message_success("Данные успешно получены", $data);
    }

    $res['data']['show'] = 'templates';

    send_json($res);