<?php 
    if(!isset($_GET["id"]) || empty($_GET["id"])){
        header("Location: /admin/brands/list");
        exit();
    }

    $data = array();
    $data['title'] = "Marka 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' => "Markalar",
        'href' => "/admin/brands/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Marka Güncelle",
        'href' => "#",
        'index' => 3
    );

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $id                 = $_GET['id'];
        $title              = $_POST['title'];
        $featured           = $_POST['featured'];
        $status             = $_POST['status'];
        $image_old          = $_POST['image_old'];

        if(!isset($_FILES['image']) || !file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name'])){
            $image               = $image_old;
        } else {
            $title_seo           = $utils->generateUrl($title);
            $image_parts         = pathinfo($_FILES['image']["name"]);
            $image               = $title_seo."-".uniqid().".".$image_parts['extension'];
			move_uploaded_file($_FILES['image']['tmp_name'], "../public/assets/images/brands/".$image);
        }


        $stmt = $db->query("UPDATE {$db->prefix}brands SET `title`=:title, `status`=:status, `image`=:image, `featured`=:featured WHERE `id`=:id",array(':title'=>$title, ':status'=>$status, ':featured'=>$featured, ':image'=>$image, ':id'=>$id));
        if($stmt->error){
            $utils->errorLog("error", "Cannot update brand. 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 marka eklenemedi."; 
            header("Refresh: 0;");
            exit();
        }else{
            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Marka başarıyla kaydedildi."; 
            header("Refresh: 0;");
            exit();
        }
	}
    
    $brand           = $product->getBrand($_GET['id']);

    if(empty($brand)) {
        header("Location: /admin/brands/list");
        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">Marka Bilgileri</h5>
                            </div>
                            <div class="card-body">
                                <form action="" method="post" enctype="multipart/form-data">
                                    <input type="hidden" name="image_old" value="<?php echo $brand["image"]; ?>">
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="title" class="form-label font-weight-bold">Başlık</label>
                                        </div>
                                        <div class="col-md-9">
                                            <input type="text" class="form-control" name="title" id="title" placeholder="adı" value="<?php echo $brand["title"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="status" class="form-label font-weight-bold">Durum</label>
                                        </div>
                                        <div class="col-md-3">
                                            <select name="status" id="status" class="form-control form-select">
                                                <option value="1" <?php echo $brand["status"] == "1" ? 'selected' : ''; ?>>Aktif</option>
                                                <option value="0" <?php echo $brand["status"] == "0" ? 'selected' : ''; ?>>Pasif</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="featured" class="form-label font-weight-bold">Anasayfada göster?</label>
                                        </div>
                                        <div class="col-md-3">
                                            <select name="featured" id="featured" class="form-control form-select">
                                                <option value="1" <?php echo $brand["featured"] == "1" ? 'selected' : ''; ?>>Evet</option>
                                                <option value="0" <?php echo $brand["featured"] == "0" ? 'selected' : ''; ?>>Hayır</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="image" class="form-label font-weight-bold">Görsel <a href="/public/assets/images/brands/<?php echo $brand["image"]; ?>" class="glightbox text-reset"><i class="bi bi-image"></i></a></label>
                                        </div>
                                        <div class="col-md-9">
                                            <input type="file" class="form-control-file" name="image" id="image" accept="image/*">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-12 text-end">
                                            <a href="/admin/brands/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"; ?>