<?php 
    $data = array();
    $data['title'] = "Ziyaret İstatistikleri | ".$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'] = [$config->get('site_url').'admin/public/assets/js/jquery-3.6.0.min.js', $config->get('site_url').'admin/public/assets/js/moment.min.js', $config->get('site_url').'admin/public/assets/js/daterangepicker.js', $config->get('site_url').'admin/public/assets/js/pages/traffic/index.js', '/admin/public/assets/js/pages/report.js'];
    $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' => "Ziyaret İstatistikleri",
        'href' => "/admin/traffic/list",
        'index' => 2
    );
    $trafficSource = new TrafficSource($db);

    $search_keywords    = isset($_GET['q']) ? htmlspecialchars($_GET['q'], ENT_QUOTES, 'UTF-8') : "";
    $referrer           = isset($_GET["referrer"]) ? $_GET["referrer"] : "";
    $date_start         = isset($_GET['date_start']) ? htmlspecialchars($_GET['date_start'], ENT_QUOTES, 'UTF-8') : "";
    $date_end           = isset($_GET['date_end']) ? htmlspecialchars($_GET['date_end'], ENT_QUOTES, 'UTF-8') : "";
    $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,
        'referrer'           => $referrer,
        'date_start'         => $date_start,
        'date_end'           => $date_end,
        'sort'               => $sort,
        'order'              => $sort_order,
        'offset'             => $offset,
        'limit'              => $limit
    );

    $filter_bar         = !empty($search_keywords) || $referrer != "" || $date_start != "" || $date_end != "" ? true : false;
    
    $referrers          = $trafficSource->getReferrers();
    $records            = $trafficSource->getRecords($filter_data);
    $total_records 	    = $trafficSource->getTotalRecords($filter_data);
    
    $pagination->total  = $total_records;
    $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">
                        <h5 class="card-title">Ziyaret İstatistikleri <span class="fs-6 text-muted">(<?php echo $total_records; ?> kayıt)</span></h5>
                        <a href="/admin/traffic/add" class="btn btn-primary btn-sm me-2  d-none"><i class="bi bi-plus-circle-dotted"></i> Yeni</a>
                        <button type="button" class="btn btn-light btn-sm <?php echo $filter_bar ? '' : 'collapsed'; ?>" data-bs-toggle="collapse" data-bs-target="#filter-bar" aria-expanded="<?php echo $filter_bar ? 'true' : 'false'; ?>" aria-controls="filter-bar">
                            <i class="bi bi-funnel"></i> Filtre
                        </button>
                    </div>
                    <div class="collapse <?php echo $filter_bar ? 'show' : ''; ?>" id="filter-bar">
                        <div class="card p-3 mb-0 border-0">
                            <form action="" method="GET" id="filter-form">
                                <div class="row">
                                    <div class="col-md-4">
                                        <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">
                                        <label for="referrer" class="col-form-label">Kaynak</label>
                                        <select name="referrer" id="referrer" class="form-control form-select">
                                            <option value="" <?php echo $referrer == '' ? 'selected' : ''; ?>>Seçiniz</option>
                                            <?php foreach($referrers as $row): ?>
                                                <option value="<?php echo $row['referrer']; ?>" <?php echo $referrer == $row['referrer'] ? 'selected' : ''; ?>><?php echo $row['referrer']; ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                    <div class="col-md-2 text-center text-md-start">
                                        <label for="date" class="col-form-label">Tarih</label>
                                        <input type="text" id="date" class="form-control" value="<?php echo isset($_GET["date_start"]) && !empty($_GET["date_start"]) ? date('m-d-Y', strtotime($date_start))." - ".date('m-d-Y', strtotime($date_end)) : ""; ?>">
                                        <input type="hidden" name="date_start" id="date_start" value="<?php echo $date_start; ?>">
                                        <input type="hidden" name="date_end" id="date_end" value="<?php echo $date_end; ?>">
                                    </div>
                                    <div class="col-md-2">
                                        <label for="" class="col-form-label">&nbsp;</label>
                                        <button type="submit" class="btn btn-primary w-100">Filtrele</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-striped table-hover table-bordered table-sortable mb-0">
                            <thead>
                                <tr>
                                    <th style="width:50px;" class="sorting" data-column="id" data-order="">ID</th>
                                    <th class="sorting" data-column="referrer" data-order="">Kaynak</th>
                                    <th class="sorting" data-column="url" data-order="">Hedef</th>
                                    <th class="sorting" data-column="ip" data-order="">IP</th>
                                    <th class="sorting" data-column="date" data-order="">Tarih</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                    if($total_records>0) {
                                        foreach($records as $row) {
                                            echo "<tr>
                                                        <td class=\"text-center\">".$row['id']."</td>
                                                        <td>".$row['referrer']."</td>
                                                        <td>".$row['url']."</td>
                                                        <td>".$row['ip']."</td>
                                                        <td>".$row['date']."</td>
                                                    </tr>";
                                        }
                                    }else{
                                        echo "<tr class=\"text-center\"><td colspan=\"5\">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"; ?>