<?php 
    $data = array();
    $data['title'] = "INBOX Kişiler | ".$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 Kişiler",
        'href' => "/admin/inbox/contacts",
        'index' => 2
    );
    
    $inboxApi  	          = new InboxApi($db, $utils);
    $contacts             = $inboxApi->getContacs();
    $contactLists         = $inboxApi->getContactLists();
?>
<?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">Kişiler</h5>
                        <button type="button" class="btn btn-primary btn-sm me-2" data-bs-toggle="modal" data-bs-target="#add-contact-modal"><i class="bi bi-plus-circle-dotted"></i> Yeni</button>
                    </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>Email</th>
                                    <th>Rank</th>
                                    <th>Kayıt Tarihi</th>
                                    <th>Güncelleme Tarihi</th>
                                    <th style="width: 120px"></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                    if(count($contacts)>0) {
                                        foreach($contacts as $row) {
                                            echo "<tr>
                                                        <td>".$row['id']."</td>
                                                        <td>".$row['email']."</td>
                                                        <td>".$row['rank']."</td>
                                                        <td>".date('d M Y h:i', strtotime($row['createTime']))."</td>
                                                        <td>".date('d M Y h:i', strtotime($row['updateTime']))."</td>
                                                        <td style=\"width: 150px;vertical-align: middle;\" class=\"text-center\">
                                                            <button type=\"button\" class=\"btn btn-sm btn-primary\" data-edit data-bs-toggle=\"modal\" data-bs-target=\"#update-contact-modal\" data-id=\"".$row['id']."\" data-name=\"".$row['email']."\"><i class=\"bi bi-pencil-square\"></i></button>
                                                            <button class=\"btn btn-sm btn-danger\" 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>
        <form action="" id="add-contact-form">
            <div class="modal" tabindex="-1" id="add-contact-modal">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title fw-bold">Yeni Kişi</h5>
                        </div>
                        <div class="modal-body">
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="email" class="col-form-label">Email *</label>
                                    <input type="text" class="form-control" name="email" id="email" value="" required>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="list" class="col-form-label">Kişi Listesi</label>
                                    <select name="list" id="list" class="form-control form-select" required>
                                        <?php foreach ($contactLists as $u): ?>
                                            <option value="<?php echo $u['id']; ?>"><?php echo $u['listName']; ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Vazgeç</button>
                            <button type="submit" class="btn btn-primary">Kaydet</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
        <form action="" id="update-contact-form">
            <div class="modal" tabindex="-1" id="update-contact-modal">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title fw-bold">Kişi Güncelle</h5>
                        </div>
                        <div class="modal-body">
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="list-name" class="col-form-label">Email *</label>
                                    <input type="text" class="form-control" name="name" id="list-name" value="" required>
                                </div>
                            </div>
                            <input type="hidden" name="id" id="list-id" value="">
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Vazgeç</button>
                            <button type="submit" class="btn btn-primary">Güncelle</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
<?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>