<?php 
    $data = array();
    $data['title'] = "Video Kütüphanesi | ".$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/vimeo/index.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' => "Video Kütüphanesi",
        'href' => "/admin/orders/list",
        'index' => 2
    );

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

    $filter_data = array(
        'folder'             => $folder,
        'query'              => $search_keywords,
        'direction'          => $sort_order,
        'sort'               => $sort,
        'page'               => $page,
        'per_page'           => $limit
    );
    
    $filter_bar         = !empty($search_keywords) || $folder != "" ? true : false;

    $videos             = $vimeoApi->getVideosFromDb($filter_data);
    $total_videos       = $videos['total'];
    $folders            = $vimeoApi->getFolders();
    
    $pagination->total  = $total_videos;
    $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">Videolar</h5>
                        <button type="button" class="btn btn-success btn-sm me-2" data-action="confirm" data-title=" " data-message="Video listesi güncellenecek, devam etmek istediğinizden emin misiniz?" data-href="/admin/video-library/refresh?id=all"><i class="bi bi-arrow-clockwise"></i> Güncelle</button>
                        <button type="button" class="btn btn-primary btn-sm me-2" id="new-video-btn"><i class="bi bi-camera-video"></i> Yeni Video</button>
                        <button type="button" class="btn btn-primary btn-sm me-2" data-bs-toggle="modal" data-bs-target="#create-folder-modal"><i class="bi bi-folder"></i> Yeni Klasör</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="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="folder" class="col-form-label">Klasör</label>
                                        <select name="folder" id="folder" class="form-control form-select">
                                            <option value="" <?php echo $folder == "" ? "selected" : ""; ?>></option>
                                            <?php foreach ($folders as $u): ?>
                                                <option value="<?php echo $u['name']; ?>" <?php echo $folder == $u['name'] ? "selected" : ""; ?>><?php echo $u['name']; ?></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>
                    <div class="table-responsive">
                        <table class="table table-striped table-hover table-bordered table-sortable mb-0">
                            <thead>
                                <tr>
                                    <th>Görsel</th>
                                    <th class="sorting" data-column="name" data-order="">Başlık</th>
                                    <th>Açıklama</th>
                                    <th>Klasör</th>
                                    <th class="sorting" data-column="duration" data-order="">Süre</th>
                                    <th class="sorting" data-column="created_time" data-order="">Kayıt Tarihi</th>
                                    <th style="width: 210px"></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                    if($total_videos>0) {
                                        $i = 1;
                                        foreach($videos['items'] as $row) {
                                            echo "<tr>
                                                        <td style=\"width: 80px;\" class=\"text-center\"><a href=\"".$row['image']."\" class=\"glightbox\" data-glightbox=\"type: image\"><img src=\"".$row['image']."\"></a></td>
                                                        <td><a href=\"".$row['link']."\" class=\"glightbox\">".$row['name']."</a></td>
                                                        <td>".$row['description']."</td>
                                                        <td>".$row['folder']."</td>
                                                        <td>".$utils->secToTime($row['duration'])."</td>
                                                        <td>".date('d M Y G:i', strtotime($row['created_time']))."</td>
                                                        <td style=\"width: 150px;vertical-align: middle;\" class=\"text-center\">
                                                            <a href=\"/admin/video-library/refresh?id=".$row['id']."\" class=\"btn btn-sm btn-success\"><i class=\"bi bi-arrow-clockwise\"></i></a>
                                                            <a href=\"/admin/video-library/chapters?id=".$row['id']."\" class=\"btn btn-sm btn-info text-white\"><i class=\"bi bi-skip-end-circle\"></i></a>
                                                            <button class=\"btn btn-sm btn-primary\" data-bs-toggle=\"modal\" data-bs-target=\"#update-video-modal\" data-id=\"".$row['id']."\"><i class=\"bi bi-pencil-square\"></i></button>
                                                            <div class=\"dropdown d-inline-block\">
                                                                <button class=\"btn btn-sm btn-dark text-white dropdown-toggle\" type=\"button\" id=\"dropdown-{$i}\" data-bs-toggle=\"dropdown\" aria-expanded=\"false\">
                                                                    <i class=\"bi bi-cloud-download-fill\"></i>
                                                                </button>
                                                                <ul class=\"dropdown-menu\" aria-labelledby=\"dropdown-{$i}\">";
                                                                    foreach($row['download_links'] as $link) {
                                                                        echo "<li><a class=\"dropdown-item\" href=\"{$link['link']}\" target=\"_blank\">{$link['quality']}</a></li>";
                                                                    }
                                                                echo "</ul>
                                                            </div>
                                                            <button class=\"btn btn-sm btn-danger\" data-delete data-href=\"/admin/video-library/delete?id=".$row['id']."\"><i class=\"bi bi-trash\"></i></button>
                                                        </td>
                                                    </tr>";
                                        }
                                    }else{
                                        echo "<tr class=\"text-center\"><td colspan=\"7\">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>
        <form action="" id="new-video-upload-form">
            <div class="modal" tabindex="-1" id="new-video-modal" data-bs-backdrop="static" data-bs-keyboard="false">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title fw-bold">Yeni Video</h5>
                        </div>
                        <div class="modal-body">
                                <div class="row mb-3">
                                    <div class="col-12">
                                        <label for="new-video-title" class="col-form-label">Video Başlığı *</label>
                                        <input type="text" class="form-control" name="new-video-title" id="new-video-title" value="" required>
                                    </div>
                                </div>
                                <div class="row mb-3">
                                    <div class="col-12">
                                        <label for="new-video-folder" class="col-form-label">Klasör</label>
                                        <select name="new-video-folder" id="new-video-folder" class="form-control form-select">
                                            <option value=""></option>
                                            <?php foreach ($folders as $u): ?>
                                                <option value="<?php echo $u['id']; ?>"><?php echo $u['name']; ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="row mb-3">
                                    <div class="col-12">
                                        <label for="new-video-description" class="col-form-label">Video Açıklaması</label>
                                        <textarea type="text" class="form-control" name="new-video-description" id="new-video-description"></textarea>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-12 mb-3">
                                        <label for="new-video" class="col-form-label">Video Seç *</label>
                                        <input type="file" class="form-control" name="new-video" id="new-video" accept="video/mp4,video/x-m4v,video/*" required>
                                    </div>
                                    <div class="col-12 mb-3">
                                        <div class="progress">
                                            <div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" id="new-video-upload-progress"></div>
                                        </div>
                                    </div>
                                </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" id="cancel-video-upload">Vazgeç</button>
                            <button type="submit" class="btn btn-primary" id="start-video-upload">Yüklemeyi başlat</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
        <form action="" id="create-folder-form">
            <div class="modal" tabindex="-1" id="create-folder-modal">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title fw-bold">Yeni Klasör</h5>
                        </div>
                        <div class="modal-body">
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="new-folder-name" class="col-form-label">Klasör Adı *</label>
                                    <input type="text" class="form-control" name="name" id="new-folder-name" value="" required>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="parent-folder" class="col-form-label">Ebeveyn Klasör</label>
                                    <select name="parent_folder" id="parent-folder" class="form-control form-select">
                                        <option value=""></option>
                                        <?php foreach ($folders as $u): ?>
                                            <option value="<?php echo $u['id']; ?>"><?php echo $u['name']; ?></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-video-form">
            <div class="modal" tabindex="-1" id="update-video-modal">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title fw-bold">Video Güncelle</h5>
                        </div>
                        <div class="modal-body">
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="update-video-title" class="col-form-label">Video Başlığı *</label>
                                    <input type="text" class="form-control" name="name" id="update-video-title" value="" required>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="update-video-description" class="col-form-label">Açıklama</label>
                                    <input type="text" class="form-control" name="description" id="update-video-description" value="">
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="privacy.embed" class="col-form-label">Gizlilik</label>
                                    <select name="privacy_embed" id="privacy.embed" class="form-control form-select">
                                        <option value="public">Herkes</option>
                                        <option value="private">Gizli</option>
                                        <option value="whitelist">Sadece Mustela Profesyonel</option>
                                    </select>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <label for="privacy.view" class="col-form-label">Görüntüleme</label>
                                    <select name="privacy_view" id="privacy.view" class="form-control form-select">
                                        <option value="anybody">Herkes</option>
                                        <option value="nobody">Hiçkimse</option>
                                        <option value="disable">Devre Dışı (öndeğer)</option>
                                        <option value="password">Parola Korumalı</option>
                                        <option value="unlisted">Gizli Link</option>
                                        <option value="users">Vimeo Kullanıcıları</option>
                                    </select>
                                </div>
                            </div>
                            <div class="row mb-3">
                                <div class="col-12">
                                    <div class="form-check">
                                        <input type="checkbox" class="form-check-input" name="privacy_download" id="privacy.download">
                                        <label class="form-check-label" for="privacy.download">
                                            Kullanıcılar videoyu indirebilsin?
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <input type="hidden" name="uri" id="update-video-id">
                        <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>
<?php include DIR_VIEWS."parts/footer.php"; ?>