<?php
    $data = array();
    $data['title'] = "Duyuru Güncelle | ".$config->get('site_title');;
    $data['links'] = [];
    $data['styles'] = [];
    $data['header']['scripts'] = [];
    $data['version'] = $config->get('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'] = [];
    $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' => "Tasarım",
        'href' => "/admin/visual-settings",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Üst Şerit Duyurular",
        'href' => "/admin/topbar/list",
        'index' => 3
    );
    $data['breadcrumbs'][] = array(
        'text' => "Güncelle",
        'href' => "#",
        'index' => 4
    );

    if(isset($_GET['id']) && !empty($_GET['id'])) {
        $stmt = $db->query("SELECT * FROM {$db->prefix}topbar_slider WHERE id=:id", array(':id'=>$_GET['id']));
        $details = $stmt->row;
    }else{
        header("Location: /admin/topbar/list");
        exit();
    }

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $id             = $_GET['id'];
        $message        = $_POST['message'];
        $bg_color       = $_POST['bg_color'];
        $text_color     = $_POST['text_color'];
        $status         = $_POST['status'];
        $sort_order     = $_POST['sort_order'];

        $stmt = $db->query("UPDATE {$db->prefix}topbar_slider SET `message`=:message, `bg_color`=:bg_color, `text_color`=:text_color, `status`=:status, sort_order=:sort_order WHERE `id`=:id", 
        array(':message'=>$message, ':bg_color'=>$bg_color, ':text_color'=>$text_color, ':status'=>$status, ':sort_order'=>$sort_order, ':id'=>$id));
        if($stmt->error){
            $utils->errorLog("error", "Cannot update topbar_slider info. 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 duyuru güncellenemedi."; 
            header("Refresh: 0;");
            exit();
        }else{
            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Duyuru başarıyla güncellendi."; 
            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">Duyuru Detayları</h5>
                            </div>
                            <div class="card-body">
                                <form action="" method="post" enctype="multipart/form-data">
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="message" class="col-form-label font-weight-bold">Metin</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="message" id="message" placeholder="Mesajınız" value="<?php echo $details["message"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="bg_color" class="col-form-label font-weight-bold">Arkaplan Rengi</label>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="color" class="form-control form-control-color" name="bg_color" id="bg_color" value="<?php echo $details["bg_color"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="text_color" class="col-form-label font-weight-bold">Metin Rengi</label>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="color" class="form-control form-control-color" name="text_color" id="text_color" value="<?php echo $details["text_color"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="sort_order" class="col-form-label font-weight-bold">Sıralama</label>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="number" class="form-control" name="sort_order" id="sort_order" placeholder="Sıralama" value="<?php echo $details["sort_order"]; ?>" required>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="status" class="col-form-label font-weight-bold">Durum</label>
                                        </div>
                                        <div class="col-md-4">
                                            <select name="status" id="status" class="form-control form-select">
                                                <option value="1" <?php echo $details["status"] == "1" ? 'selected' : ''; ?>>Aktif</option>
                                                <option value="0" <?php echo $details["status"] == "0" ? 'selected' : ''; ?>>Pasif</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-12 text-end">
                                            <a href="/admin/topbar/list" class="btn btn-secondary me-2">Geri</a>
                                            <input type="submit" name="update" class="btn btn-primary" value="Güncelle">
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
<?php include DIR_VIEWS."parts/footer.php"; ?>