<?php 
    $data = array();
    $data['title'] = "INBOX Gönderimler | ".$App->title;
    $data['links'] = [];
    $data['styles'] = ['/admin/public/assets/css/jstable.css'];
    $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/inbox/contacts.js', '/admin/public/assets/js/jstable.min.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' => "INBOX Gönderimler",
        'href' => "/admin/inbox/campaigns/list",
        'index' => 2
    );
    
    $inboxApi  	          = new InboxApi($db, $utils);
    $campaigns            = $inboxApi->getCampaigns();
?>
<?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">Gönderimler</h5>
                        <a href="/admin/inbox/campaigns/add" class="btn btn-primary btn-sm me-2"><i class="bi bi-plus-circle-dotted"></i> Yeni</a>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-striped table-hover table-bordered mb-0" id="contacts-table">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Konu</th>
                                    <th>Alıcılar</th>
                                    <th>Gönderilen</th>
                                    <th>Ulaşan</th>
                                    <th>Açılan</th>
                                    <th>Planlanan Tarihi</th>
                                    <th>Kayıt Tarihi</th>
                                    <th style="width: 120px"></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                    if(count($campaigns)>0) {
                                        foreach($campaigns as $row) {
                                            echo "<tr>
                                                        <td>".$row['id']."</td>
                                                        <td>".$row['subject']."</td>
                                                        <td>".$row['totalRecipients']."</td>
                                                        <td>".$row['sent']."</td>
                                                        <td>".$row['delivered']."</td>
                                                        <td>".$row['opened']."</td>
                                                        <td>".date('d M Y h:i', strtotime($row['plannedTime']))."</td>
                                                        <td>".date('d M Y h:i', strtotime($row['createTime']))."</td>
                                                        <td style=\"width: 150px;vertical-align: middle;\" class=\"text-center\">
                                                            <button class=\"btn btn-sm btn-danger disabled\" data-delete data-href=\"/admin/inbox/contacts/delete?id=".$row['id']."\"><i class=\"bi bi-trash\"></i></button>
                                                        </td>
                                                    </tr>";
                                        }
                                    }else{
                                        echo "<tr class=\"text-center\"><td colspan=\"6\">Hiçbir kayıt bulunamadı!</td></tr>";
                                    }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </main>
<?php include DIR_VIEWS."parts/footer.php"; ?>
<script>
    var table = new JSTable("#contacts-table", {
        serverSide : false,
        perPage : 15,
        labels: {
            placeholder: "Ara...",
            perPage: "sayfa başına kayıt",
            noRows: "Hiçbir kayıt bulunamadı!",
            info: "{rows} kayıttan {start} - {end} arası görüntüleniyor",
            loading: "Yükleniyor...",
            infoFiltered: "{rows} kayıttan {start} - {end} arası görüntüleniyor ({rowsTotal} kayıt arasından filtrelendi)"
        },
        layout: {
            top: "{search}",
            bottom: "{info}{pager}"
        }
    });
</script>