<?php 
    if(!isset($_GET["id"]) || empty($_GET["id"])){
        header("Location: /admin/video-library/list");
        exit();
    }
    $video_details       = $vimeoApi->getVideoDb($_GET['id']);
    if(empty($video_details)) {
        header("Location: /admin/video-library/list");
        exit();
    }

    $data = array();
    $data['title'] = "Video Bölümü Düzenle | ".$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'] = ['https://player.vimeo.com/api/player.js', '/admin/public/assets/js/pages/vimeo/chapters.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/video-library/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Video Bölümleri",
        'href' => "/admin/video-library/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Video Bölüm Düzenle",
        'href' => "#",
        'index' => 4
    );

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        if(!empty($_POST['time'])) {
            $chapter_values = array();
            for($i=0; $i<count($_POST['time']); $i++) {
                array_push($chapter_values, array(
                    'time' => $_POST['time'][$i],
                    'title' => $_POST['title'][$i]
                ));
            }
            $stmt = $db->query("UPDATE {$db->prefix}vimeo_videos SET chapters=:chapters WHERE vimeo_id=:vimeo_id", array(':vimeo_id'=>$_GET['id'], ':chapters'=>json_encode($chapter_values)));
            if($stmt->error){
                $utils->errorLog("error", "Cannot set video chapters. Error: ".$stmt->error_info['message'], $session->data['user_id'], $_SERVER['REQUEST_URI']);
                $session->data['messageType'] = "error"; 
                $session->data['messageTitle'] = "Hata!"; 
                $session->data['message'] = "Bir hata oluştu ve bölümler kaydedilemedi."; 
                header("Refresh: 0;");
                exit();
            }else{
                $session->data['messageType'] = "success"; 
                $session->data['messageTitle'] = "Başarılı!"; 
                $session->data['message'] = "Video bölümleri başarıyla kaydedildi."; 
                header("Refresh: 0;");
                exit();
            }
        }else{
            $stmt = $db->query("UPDATE {$db->prefix}vimeo_videos SET chapters='' WHERE vimeo_id=:vimeo_id", array(':vimeo_id'=>$_GET['id']));
            if($stmt->error){
                $utils->errorLog("error", "Cannot delete video chapters. Error: ".$stmt->error_info['message'], $session->data['user_id'], $_SERVER['REQUEST_URI']);
                $session->data['messageType'] = "error"; 
                $session->data['messageTitle'] = "Hata!"; 
                $session->data['message'] = "Bir hata oluştu ve bölümler silinemedi."; 
                header("Refresh: 0;");
                exit();
            }else{
                $session->data['messageType'] = "success"; 
                $session->data['messageTitle'] = "Başarılı!"; 
                $session->data['message'] = "Video bölümleri başarıyla silindi."; 
                header("Refresh: 0;");
                exit();
            }
        }
	}
?>
<?php include DIR_VIEWS."parts/header.php"; ?>
        <main class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-header">
                                <h5 class="card-title">Video Bölümleri</h5>
                            </div>
                            <div class="card-body">
                                <form action="" method="post">
                                    <?php if(!empty($video_details)): ?>
                                        <div class="row">
                                            <div class="col-lg-7 mb-3">
                                                <div class="interactive-video-wrapper ratio ratio-16x9 mb-5">
                                                    <iframe src="<?php echo $video_details['link']; ?>" title="<?php echo $video_details['name']; ?>" id="course-video" allow="autoplay" allowfullscreen></iframe>
                                                </div>
                                            </div>
                                            <div class="col-lg-5 mb-3">
                                                <table class="table table-condensed" id="chapters-table">
                                                    <thead>
                                                        <tr>
                                                            <th>Zaman</th>
                                                            <th>Başlık</th>
                                                            <th style="width: 50px;">Sil</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php if($video_details['chapters'] != ''): ?>
                                                        <?php $chapters = json_decode($video_details['chapters'], true); ?>
                                                            <?php foreach($chapters as $chapter): ?>
                                                                <tr>
                                                                    <td><input type="text" class="form-control" name="time[]" placeholder="Zaman" value="<?php echo $chapter["time"]; ?>"></td>
                                                                    <td><input type="text" class="form-control" name="title[]" placeholder="Başlık" value="<?php echo $chapter["title"]; ?>"></td>
                                                                    <td><button type="button" class="btn btn-danger btn-sm" onclick="deleteRow(this)"><i class="bi bi-trash"></i></button></td>
                                                                </tr>
                                                            <?php endforeach; ?>
                                                        <?php endif; ?>
                                                    </tbody>
                                                </table>
                                                <hr>
                                                <div class="row">
                                                    <div class="col-12 text-center">
                                                        <button type="button" class="btn btn-primary btn-sm" id="add-row"><i class="bi bi-plus-circle-dotted"></i> Yeni Bölüm</button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                            <hr>
                                            <div class="row mb-3">
                                                <div class="col-12 text-end">
                                                    <a href="/admin/video-library/list" class="btn btn-secondary me-2">Geri</a>
                                                    <input type="submit" name="update" class="btn btn-primary" value="Kaydet">
                                                </div>
                                            </div>
                                    <?php endif; ?>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
<?php include DIR_VIEWS."parts/footer.php"; ?>