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

    $data = array();
    $data['title'] = "Ürün Kategorisi 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' => "Ürün Kategorileri",
        'href' => "/admin/category/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Ürün Kategorisi Güncelle",
        'href' => "#",
        'index' => 3
    );

    $category           = $product->getCategory($_GET['id']);
    $categories 	    = $product->getCategories();

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $id                 = $_GET['id'];
        $title              = $_POST['title'];
        $description        = $_POST['description'];
        $status             = $_POST['status'];
        $parent             = $_POST['parent'];
        $installment        = $_POST['installment'];
        $google_product_category = $_POST['google_product_category'];
        $installment_count  = $_POST['installment_count'];
        $installment_count_business  = $_POST['installment_count_business'];


        $seo_url	        = $utils->generateUrl($title);

        if(isset($_POST['remove_image'])) {
            $image                = "";
        }else{
            if(!file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name'])){
                $image                = $category["image"];
            } else {
                $image_parts          = pathinfo($_FILES["image"]["name"]);
                $image                = $seo_url."-".time().".".$image_parts['extension'];
                move_uploaded_file($_FILES['image']['tmp_name'], "../public/assets/images/banners/".$image);
            }
        }


        $stmt = $db->query("UPDATE {$db->prefix}categories SET `title`=:title, `status`=:status, `description`=:description, parent=:parent, installment=:installment, google_product_category=:google_product_category, installment_count=:installment_count, installment_count_business=:installment_count_business, image=:image WHERE `id`=:id", 
        array(':title'=>$title, ':status'=>$status, ':description'=>$description, ':parent'=>$parent, ':installment'=>$installment, ':google_product_category'=>$google_product_category, ':installment_count'=>$installment_count, ':installment_count_business'=>$installment_count_business, ':image'=>$image, ':id'=>$id));
        if($stmt->error){
            $utils->errorLog("error", "Cannot update product category. 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 ürün kategorisi güncellenemedi."; 
            header("Refresh: 0;");
            exit();
        }else{
            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Ürün kategorisi başarıyla güncellendi."; 
            header("Refresh: 0;");
            exit();
        }
	}
    

    if(empty($category)) {
        header("Location: /admin/category/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">Ürün Kategorisi Bilgileri</h5>
                            </div>
                            <div class="card-body">
                                <form action="" method="post" enctype="multipart/form-data">
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="title" class="col-form-label fw-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 $category["title"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="status" class="col-form-label fw-bold">Durum</label>
                                        </div>
                                        <div class="col-md-3">
                                            <select name="status" id="status" class="form-control form-select">
                                                <option value="1" <?php echo $category["status"] == "1" ? 'selected' : ''; ?>>Aktif</option>
                                                <option value="0" <?php echo $category["status"] == "0" ? 'selected' : ''; ?>>Pasif</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="installment" class="col-form-label fw-bold">Taksit</label>
                                        </div>
                                        <div class="col-md-3">
                                            <select name="installment" id="installment" class="form-control form-select">
                                                <option value="1" <?php echo $category["installment"] == "1" ? 'selected' : ''; ?>>Yapılsın</option>
                                                <option value="0" <?php echo $category["installment"] == "0" ? 'selected' : ''; ?>>Yapılmasın</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="installment_count" class="col-form-label fw-bold">Taksit Sayısı</label>
                                        </div>
                                        <div class="col-md-3">
                                            <input type="text" class="form-control" name="installment_count" id="installment_count" placeholder="" value="<?php echo $category["installment_count"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="installment_count_business" class="col-form-label fw-bold">Taksit Sayısı (Ticari Kart)</label>
                                        </div>
                                        <div class="col-md-3">
                                            <input type="text" class="form-control" name="installment_count_business" id="installment_count_business" placeholder="" value="<?php echo $category["installment_count_business"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="parent" class="col-form-label fw-bold">Üst Kategori</label>
                                        </div>
                                        <div class="col-md-3">
                                            <select name="parent" id="parent" class="form-control form-select">
                                                <option value="">Seçiniz</option>
                                                <?php foreach ($categories as $c): ?>
                                                    <option value="<?php echo $c['id']; ?>" <?php echo $category['parent'] != '' && $category['parent'] == $c['id'] ? 'selected' : ''; ?> class="fw-bold"><?php echo $c['title']; ?></option>
                                                    <?php if (!empty($c['sub_items'])): ?>
                                                        <?php foreach ($c['sub_items'] as $s1): ?>
                                                            <option value="<?php echo $s1['id']; ?>" <?php echo $category['parent'] != '' && $category['parent'] == $s1['id'] ? 'selected' : ''; ?>>&nbsp;&nbsp;<?php echo $c['title']; ?> > <?php echo $s1['title']; ?></option>
                                                            <?php if (!empty($s1['sub_items'])): ?>
                                                                <?php foreach ($s1['sub_items'] as $s2): ?>
                                                                    <option value="<?php echo $s2['id']; ?>" <?php echo $category['parent'] != '' && $category['parent'] == $s2['id'] ? 'selected' : ''; ?>>&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $c['title']; ?> > <?php echo $s1['title']; ?> > <?php echo $s2['title']; ?></option>
                                                                <?php endforeach; ?>
                                                            <?php endif; ?>
                                                        <?php endforeach; ?>
                                                    <?php endif; ?>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="google_product_category" class="col-form-label fw-bold">Google Ürün Kategorisi</label>
                                        </div>
                                        <div class="col-md-3">
                                            <input type="text" class="form-control" name="google_product_category" id="google_product_category" placeholder="" value="<?php echo $category["google_product_category"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="image" class="col-form-label fw-bold">Görsel <a href="/public/assets/images/banners/<?php echo $category["image"]; ?>" class="glightbox text-reset"><i class="bi bi-image"></i></a></label>
                                        </div>
                                        <div class="col-md-3">
                                            <input type="file" class="form-control" name="image" id="image">
                                        </div>
                                    </div>
                                    <?php if(isset($category["image"]) && !empty($category["image"])): ?>
                                        <div class="row mb-3">
                                            <div class="col-md-3">
                                                <label for="image" class="col-form-label fw-bold"></label>
                                            </div>
                                            <div class="col-md-3">
                                                <div class="form-check form-switch">
                                                    <input class="form-check-input" type="checkbox" role="switch" id="remove_image" name="remove_image">
                                                    <label class="form-check-label" for="remove_image">Mevcut görseli kaldır?</label>
                                                </div>
                                            </div>
                                        </div>
                                    <?php endif; ?>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="description" class="col-form-label fw-bold">Açıklama</label>
                                        </div>
                                        <div class="col-md-9">
                                            <textarea name="description" id="description" cols="3" class="form-control"><?php echo $category["description"]; ?></textarea>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-12 text-end">
                                            <a href="/admin/category/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"; ?>