<?php 
    $data = array();
    $data['title'] = "Destek Talepleri | ".$App->title;
    $data['links'] = [];
    $data['styles'] = [];
    $data['header']['scripts'] = [];
    $data['version'] = $App->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' => "Destek Talepleri",
        'href' => "/admin/support-tickets/list",
        'index' => 2
    );

    $search_keywords    = isset($_GET['q']) ? htmlspecialchars($_GET['q'], ENT_QUOTES, 'UTF-8') : "";
    $status             = isset($_GET["status"]) ? $_GET["status"] : "";

    $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,
        'status'             => $status,
        'sort'               => 'id',
        'order'              => 'DESC',
        'offset'             => $offset,
        'limit'              => $limit
    );
    
    $test_rows             = $support->getTickets($filter_data);
    $total_rows 	       = $support->getTotalTickets($filter_data);

    $support_status	       = $support->getSupportStatus();
    
    $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="status" class="col-form-label">Durum</label>
                                    <select name="status" id="status" class="form-control form-select">
                                        <option value="" <?php echo $status == '' ? 'selected' : ''; ?>>Seçiniz</option>
                                        <?php foreach ($support_status as $s): ?>
                                            <option value="<?php echo $s['id']; ?>" <?php echo $status == $s['id'] ? 'selected' : ''; ?>><?php echo $s['title']; ?></option>
                                        <?php endforeach; ?>
                                    </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-bordered mb-0">
                            <thead>
                                <tr>
                                    <th>Kod</th>
                                    <th>Durum</th>
                                    <th>Öncelik</th>
                                    <th>Konu</th>
                                    <th>Kullanıcı</th>
                                    <th>Kayıt Tarihi</th>
                                    <th>Son Güncelleme</th>
                                    <th style="width: 90px;"></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                    if($total_rows>0) {
                                        foreach($test_rows as $row) {
                                            echo "<tr>
                                                        <td>".$row['code']."</td>
                                                        <td class=\"text-center\"><span class=\"badge bg-".$row['status_color']."\">".$row['status']."</span></td>
                                                        <td class=\"text-center\">".$row['priority']."</td>
                                                        <td>".$row['subject']."</td>
                                                        <td><a href=\"/admin/users/edit?id=".$row['user_id']."\" target=\"_blank\">".$row['user_name']."</a></td>
                                                        <td>".$row['date_added']."</td>
                                                        <td>".$row['date_updated']."</td>";
                                                        echo "<td style=\"width: 90px;vertical-align: middle;\">
                                                                <a class=\"btn btn-sm btn-primary me-1\" href=\"/admin/support-tickets/edit?id=".$row['id']."\"><i class=\"bi bi-pencil-square\"></i></a>
                                                                <button class=\"btn btn-sm btn-danger\" data-delete data-href=\"/admin/support-tickets/delete?id=".$row['code']."\"><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"; ?>