<?php 
    $data = array();
    $data['title'] = "Link Düzenle | ".$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'] = ['/admin/public/assets/js/pages/user/edit.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' => "Link Yönetimi",
        'href' => "/admin/seo/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Link Düzenle",
        'href' => "#",
        'index' => 3
    );

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $id             = $_GET['id'];
        $url            = $_POST['url'];
        $title          = $_POST['title'];
        $description    = $_POST['description'];
        $index_         = $_POST['index_'];
        $follow         = $_POST['follow'];

        $stmt = $db->query("UPDATE {$db->prefix}urls SET `url`=:url, `title`=:title, `description`=:description, `index_`=:index_, `follow`=:follow WHERE `id`=:id", array(':url'=>$url, ':title'=>$title, ':description'=>$description, ':index_'=>$index_, ':follow'=>$follow, ':id'=>$id));
        if($stmt->error){
            $utils->errorLog("error", "Cannot update user password. 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 link güncellenemedi."; 
            header("Refresh: 0;");
            exit();
        }else{
            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Link başarıyla güncellendi."; 
            header("Refresh: 0;");
            exit();
        }
	}
    
    $url = new Url($db, $utils);
    $link_details = $url->getUrl($_GET['id']);
?>
<?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">Link Detayları</h5>
                            </div>
                            <div class="card-body">
                                <form action="" method="post">
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="url" class="form-label font-weight-bold">URL</label>
                                        </div>
                                        <div class="col-md-9">
                                            <input type="text" class="form-control" name="url" id="url" placeholder="url" value="<?php echo $link_details["url"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="title" class="form-label font-weight-bold">Meta Başlığı</label>
                                        </div>
                                        <div class="col-md-9">
                                            <input type="text" class="form-control" name="title" id="title" placeholder="title" value="<?php echo $link_details["title"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="description" class="form-label font-weight-bold">Meta Açıklaması</label>
                                        </div>
                                        <div class="col-md-9">
                                            <textarea name="description" id="description" class="form-control" rows="2"><?php echo $link_details["description"]; ?></textarea>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="url_type" class="form-label font-weight-bold">Tip</label>
                                        </div>
                                        <div class="col-md-9">
                                            <select name="url_type" id="url_type" class="form-control form-select" disabled>
                                                <option value="" <?php echo $link_details["url_type"] == '' ? 'selected' : ''; ?>>Seçiniz</option>
                                                <option value="system" <?php echo $link_details["url_type"] == "system" ? 'selected' : ''; ?>>Sistem</option>
                                                <option value="information" <?php echo $link_details["url_type"] == "information" ? 'selected' : ''; ?>>Bilgi</option>
                                                <option value="brand" <?php echo $link_details["url_type"] == "brand" ? 'selected' : ''; ?>>Marka</option>
                                                <option value="product" <?php echo $link_details["url_type"] == "product" ? 'selected' : ''; ?>>Ürün</option>
                                                <option value="product-list" <?php echo $link_details["url_type"] == "product-list" ? 'selected' : ''; ?>>Ürün Listesi</option>
                                                <option value="product-category" <?php echo $link_details["url_type"] == "product-category" ? 'selected' : ''; ?>>Ürün Kategorisi</option>
                                                <option value="feed" <?php echo $link_details["url_type"] == "feed" ? 'selected' : ''; ?>>Besleme Linki</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="index_" class="form-label font-weight-bold">Index</label>
                                        </div>
                                        <div class="col-md-9">
                                            <select name="index_" id="index_" class="form-control form-select">
                                                <option value="1" <?php echo $link_details["index_"] == '' ? '1' : ''; ?>>index</option>
                                                <option value="0" <?php echo $link_details["index_"] == "0" ? 'selected' : ''; ?>>noindex</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="follow" class="form-label font-weight-bold">Follow</label>
                                        </div>
                                        <div class="col-md-9">
                                            <select name="follow" id="follow" class="form-control form-select">
                                                <option value="1" <?php echo $link_details["follow"] == '' ? '1' : ''; ?>>follow</option>
                                                <option value="0" <?php echo $link_details["follow"] == "0" ? 'selected' : ''; ?>>nofollow</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-12 text-end">
                                            <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"; ?>