<?php 
    $data = array();
    $data['title'] = "Log Kayıtları | ".$config->get('site_title');
    $data['links'] = [];
    $data['styles'] = [];
    $data['header']['scripts'] = [];
    $data['version'] = $config->get('version');
    $data['csrf_token'] = $csrf_generator->create();
    $data['sidebar_menu'] = $menu->get(array('position'=>'sidebar'));
    $data['sidebar_active_menu'] = $menu->getActiveMenuItemId(trim($_SERVER['REQUEST_URI']));
    $data['footer']['scripts'] = [];
    $menu_items = $menu->get(array('position'=>'footer'));
    foreach($menu_items as $item){
        if($item['col'] == "1"){
            $data['menu']['footer']["1"][] = $item;
        }
        if($item['col'] == "2"){
            $data['menu']['footer']["2"][] = $item;
        }
        if($item['col'] == "3"){
            $data['menu']['footer']["3"][] = $item;
        }
        if($item['col'] == "4"){
            $data['menu']['footer']["4"][] = $item;
        }
    }

    $data['breadcrumbs'] = array();
    $data['breadcrumbs'][] = array(
        'text' => "Pano",
        'href' => "/admin",
        'index' => 1
    );
    $data['breadcrumbs'][] = array(
        'text' => "Log Kayıtları",
        'href' => "/admin/system-logs/list",
        'index' => 2
    );

    $search_keywords    = isset($_GET['q']) ? htmlspecialchars($_GET['q'], ENT_QUOTES, 'UTF-8') : "";
    $type               = isset($_GET["type"]) ? $_GET["type"] : "";
    $sort               = isset($_GET["sort"]) ? $_GET["sort"] : "id";
    $sort_order         = isset($_GET["order"]) ? $_GET["order"] : "DESC";

    $page               = !isset($_GET["page"]) || $_GET["page"] < 1 ? 1 : (int)$_GET["page"];
    $limit	            = isset($_GET["limit"]) ? (int)$_GET["limit"] : 25;
    $offset             = ($page-1)*$limit;
    $filter_data = array(
        'search_keywords'    => $search_keywords,
        'type'               => $type,
        'sort'               => $sort,
        'order'              => $sort_order,
        'offset'             => $offset,
        'limit'              => $limit
    );
    
    $test_rows             = $systemLog->getLogs($filter_data);
    $total_rows 	       = $systemLog->getTotalLogs($filter_data);
    
    $pagination->total  = $total_rows;
    $pagination->page   = $page;
    $pagination->limit  = $limit;
    $pagination->url    = '#';
?>
<?php include DIR_VIEWS."parts/header.php"; ?>
        <main class="content">
            <div class="container-fluid">
                <div class="card">
                    <div class="card-header">
                    <form action="" method="GET" id="filter-form">
                            <div class="row">
                                <div class="col-md-4 text-center text-md-start">
                                    <label for="q" class="col-form-label">Anahtar Kelime</label>
                                    <input type="text" name="q" id="q" class="form-control" value="<?php echo $search_keywords; ?>">
                                </div>
                                <div class="col-md-2 text-center text-md-start">
                                    <label for="type" class="col-form-label">Kayıt Türü</label>
                                    <select name="type" id="type" class="form-control form-select">
                                        <option value="" <?php echo $type == '' ? 'selected' : ''; ?>>Seçiniz</option>
                                        <option value="info" <?php echo $type == "info" ? 'selected' : ''; ?>>Info</option>
                                        <option value="error" <?php echo $type == "error" ? 'selected' : ''; ?>>Error</option>
                                    </select>
                                </div>
                                <div class="col-md-2 text-center text-md-start">
                                    <label for="" class="col-form-label">&nbsp;</label>
                                    <button type="submit" class="btn btn-primary w-100">Filtrele</button>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-striped table-hover table-sortable mb-0">
                            <thead>
                                <tr>
                                    <th class="sorting" data-column="id" data-order="">ID</th>
                                    <th class="sorting" data-column="type" data-order="">Tür</th>
                                    <th class="sorting" data-column="message" data-order="">Mesaj</th>
                                    <th class="sorting" data-column="request_uri" data-order="">Adres</th>
                                    <th class="sorting" data-column="date" data-order="">Kayıt Tarihi</th>
                                    <th style="width: 90px;"></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                    if($total_rows>0) {
                                        foreach($test_rows as $row) {
                                            echo "<tr>
                                                        <td>".$row['id']."</td>
                                                        <td>".$row['type']."</td>
                                                        <td>".htmlspecialchars($row['message'], ENT_QUOTES, 'UTF-8')."</td>
                                                        <td>".$row['request_uri']."</td>
                                                        <td>".$row['date']."</td>
                                                        <td style=\"width: 90px;vertical-align: middle;\">
                                                                <button class=\"btn btn-sm btn-danger\" data-delete data-href=\"/admin/system-logs/delete?id=".$row['id']."\"><i class=\"bi bi-trash\"></i></button>
                                                            </td>
                                                    </tr>";
                                        }
                                    }else{
                                        echo "<tr class=\"text-center\"><td colspan=\"9\">Hiçbir kayıt  bulunamadı!</td></tr>";
                                    }
                                ?>
                            </tbody>
                        </table>
                    </div>
                    <div class="d-flex table-responsive align-items-center justify-content-center mt-3">
                        <div class="table-responsive">
                            <nav aria-label="Page navigation">
                                <ul class="pagination" id="pagination">
                                    <?php echo $pagination->render(); ?>
                                </ul>
                            </nav>
                        </div>
                    </div>
                </div>
            </div>
        </main>
<?php include DIR_VIEWS."parts/footer.php"; ?>