<?php 
    $data = array();
    $data['title'] = "Anketler | ".$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'] = ['/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' => "Anketler",
        'href' => "/admin/survey/list",
        'index' => 2
    );

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

    $filter_bar         = !empty($search_keywords) || $status != "" ? true : false;
    
    $surveys            = $survey->getSurveys($filter_data);
    $total_surveys 	    = $survey->getTotalSurveys($filter_data);
    
    $pagination->total  = $total_surveys;
    $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">Anketler</h5>
                        <a href="/admin/survey/add" class="btn btn-primary btn-sm me-2"><i class="bi bi-plus-circle-dotted"></i> Yeni</a>
                        <button type="button" class="btn btn-success btn-sm me-2" data-action="/admin/survey/excel" data-export><i class="bi bi-file-earmark-excel"></i> Excel</button>
                        <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 text-center text-md-start">
                                        <label for="q" class="col-form-label">Anahtar Kelime</label>
                                        <input type="search" 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>
                                            <option value="1" <?php echo $status == "1" ? 'selected' : ''; ?>>Aktif</option>
                                            <option value="0" <?php echo $status == "0" ? 'selected' : ''; ?>>Pasif</option>
                                            <option value="2" <?php echo $status == "2" ? 'selected' : ''; ?>>Taslak</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>
                    <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="status" data-order="">Durum</th>
                                    <th class="sorting" data-column="title" data-order="">Başlık</th>
                                    <th>Açıklama</th>
                                    <th class="sorting" data-column="date_added" data-order="">Kayıt Tarihi</th>
                                    <th style="width:180px;"></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                    if($total_surveys>0) {
                                        foreach($surveys as $row) {
                                            if($row['status'] == 0){
                                                $status_ = "Pasif";
                                            }else if($row['status'] == 1){
                                                $status_ = "Aktif";
                                            }else if($row['status'] == 2){
                                                $status_ = "Taslak";
                                            } else {
                                                $status_ = $row['status'];
                                            }
                                            echo "<tr>
                                                        <td class=\"text-center\">".$row['id']."</td>
                                                        <td>".$status_."</td>
                                                        <td>".$row['title']."</td>
                                                        <td>".$row['description']."</td>
                                                        <td>".$row['date_added']."</td>
                                                        <td style=\"width: 150px;vertical-align: middle;\" class=\"text-center\">
                                                            <a class=\"btn btn-sm btn-primary me-1\" href=\"/admin/survey/edit?id=".$row['guid']."\"><i class=\"bi bi-pencil-square\"></i></a>
                                                            <a class=\"btn btn-sm btn-info text-white me-1\" href=\"/anket/".$row['guid']."\" target=\"_blank\"><i class=\"bi bi-send\"></i></a>
                                                            <button class=\"btn btn-sm btn-warning me-1\" type=\"button\" data-bs-toggle=\"modal\" data-bs-target=\"#reports-modal\" data-id=\"".$row['guid']."\"><i class=\"bi bi-graph-up\"></i></button>
                                                            <button class=\"btn btn-sm btn-danger\" data-delete data-href=\"/admin/survey/delete?id=".$row['id']."\"><i class=\"bi bi-trash\"></i></button>
                                                        </td>
                                                    </tr>";
                                        }
                                    }else{
                                        echo "<tr class=\"text-center\"><td colspan=\"11\">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>
        <div class="modal fade" id="reports-modal" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Anket Raporları</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <button type="button" class="btn btn-primary w-100" data-action="/admin/survey/excel" data-report="applications" data-id="" data-export>Katılımcı Listesi</button>
                            </div>
                            <div class="col-md-6 mb-3">
                                <button type="button" class="btn btn-primary w-100" data-action="/admin/survey/excel" data-report="answers" data-id="" data-export>Anket Sonuçları</button>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Vazgeç</button>
                    </div>
                </div>
            </div>
        </div>
<?php include DIR_VIEWS."parts/footer.php"; ?>