<?php 
    if(!isset($_GET["id"]) || empty($_GET["id"])){
        header("Location: /admin/product/list");
        exit();
    }
    $product_details       = $product->getProduct($_GET['id']);
    if(empty($product_details)) {
        header("Location: /admin/product/list");
        exit();
    }

    $data = array();
    $data['title'] = "Ürün 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/product/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' => "Ürünler",
        'href' => "/admin/product/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Ürün Düzenle",
        'href' => "#",
        'index' => 3
    );

    $redirect_url             = isset($_REQUEST["redirect"]) ? $_REQUEST["redirect"] : "/admin/product/list";

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        if(isset($_POST['action']) && !empty($_POST['action'])) {
            switch($_POST['action']) {
                case 'product-details':
                    $id                 = $_GET['id'];
                    $status             = $_POST['status'];
                    $title              = $_POST['title'];
                    $sku                = $_POST['sku'];
                    $ean                = $_POST['ean'];
                    $brand_id           = $_POST['brand_id'];
                    $tag_id             = $_POST['tag_id'];
                    $seller_id          = $_POST['seller_id'];
                    $supplier_id        = $_POST['supplier_id'];
                    $category           = $_POST['category'];
                    $delivery_areas     = isset($_POST['delivery_areas']) ? $_POST['delivery_areas'] : null;
                    $shipping_agencies  = isset($_POST['shipping_agencies']) ? $_POST['shipping_agencies'] : null;
                    $badges             = isset($_POST['badges']) ? $_POST['badges'] : null;
                    $featured           = isset($_POST['featured']) ? 1 : 0;
                    $free_shipping      = isset($_POST['free_shipping']) ? 1 : 0;
                    $checkout_offer     = isset($_POST['checkout_offer']) ? 1 : 0;
                    $new                = isset($_POST['new']) ? 1 : 0;

                    if(!isset($_FILES['image']) || !file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name'])){
                        $image               = $product_details["image"];
                    } else {
                        $title_seo           = $utils->generateUrl($title);
                        $image_parts         = pathinfo($_FILES['image']["name"]);
                        $image               = $title_seo."-".uniqid().".".$image_parts['extension'];
                        $utils->resizeCropImage(500, 500, $_FILES['image']['tmp_name'], "../public/assets/images/products/medium/".$image);
                        $utils->resizeCropImage(170, 170, $_FILES['image']['tmp_name'], "../public/assets/images/products/thumb/".$image);
                        move_uploaded_file($_FILES['image']['tmp_name'], "../public/assets/images/products/large/".$image);
                    }

                    $stmt = $db->query("UPDATE {$db->prefix}products SET `status`=:status, `title`=:title, `sku`=:sku, `ean`=:ean, `brand_id`=:brand_id, `tag_id`=:tag_id, `seller_id`=:seller_id, `supplier_id`=:supplier_id, `featured`=:featured, `checkout_offer`=:checkout_offer, `image`=:image, `free_shipping`=:free_shipping WHERE `id`=:id", array(':status'=>$status, ':title'=>$title, ':sku'=>$sku, ':ean'=>$ean, ':brand_id'=>$brand_id, ':tag_id'=>$tag_id, ':seller_id'=>$seller_id, ':supplier_id'=>$supplier_id, ':featured'=>$featured, ':checkout_offer'=>$checkout_offer, ':image'=>$image, ':free_shipping'=>$free_shipping, ':id'=>$id));
                    if($stmt->error){
                        $utils->errorLog("error", "Cannot update product 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 ürün bilgileri güncellenemedi."; 
                        header("Location: 0;");
                        exit();
                    }else{
                        $db->query("DELETE FROM {$db->prefix}product_to_category WHERE product_id=:product_id", array(':product_id'=>$id));
                        if(!empty($category)){
                            foreach($category as $c) {
                                $db->query("INSERT INTO {$db->prefix}product_to_category (product_id, category_id) VALUES (:product_id, :category_id)", array(':category_id'=>$c, ':product_id'=>$id));
                            }
                        }

                        $db->query("DELETE FROM {$db->prefix}delivery_areas WHERE product_id=:product_id", array(':product_id'=>$id));
                        if(!empty($delivery_areas)){
                            foreach($delivery_areas as $c) {
                                $db->query("INSERT INTO {$db->prefix}delivery_areas (product_id, city) VALUES (:product_id, :city)", array(':city'=>$c, ':product_id'=>$id));
                            }
                        }

                        $db->query("DELETE FROM {$db->prefix}product_shipping WHERE product_id=:product_id", array(':product_id'=>$id));
                        if(!empty($shipping_agencies)){
                            foreach($shipping_agencies as $c) {
                                $db->query("INSERT INTO {$db->prefix}product_shipping (product_id, shipping_agency) VALUES (:product_id, :shipping_agency)", array(':shipping_agency'=>$c, ':product_id'=>$id));
                            }
                        }

                        $db->query("DELETE FROM {$db->prefix}product_to_badge WHERE product_id=:product_id", array(':product_id'=>$id));
                        if(!empty($badges)){
                            foreach($badges as $c) {
                                $db->query("INSERT INTO {$db->prefix}product_to_badge (product_id, badge_id) VALUES (:product_id, :badge_id)", array(':badge_id'=>$c, ':product_id'=>$id));
                            }
                        }
                        
                        $session->data['messageType'] = "success"; 
                        $session->data['messageTitle'] = "Başarılı!"; 
                        $session->data['message'] = "Ürün bilgileri başarıyla güncellendi."; 
                        header("Location: ".$redirect_url);
                        exit();
                    }
                break;
                case 'pricing-details':
                    $id                  = $_GET['id'];
                    $price               = $_POST['price'];
                    $discount_rate       = $_POST['discount_rate'];
                    $discounted_price    = $_POST['discounted_price'];
                    $buying_price        = $_POST['buying_price'];
                    $vat                 = $_POST['vat'];
                    $cart_discount_rate  = $_POST['cart_discount_rate'];
                    $cart_discounted_price = $_POST['cart_discounted_price'];

                    if($cart_discount_rate != 0) {
                        $selling_price = $cart_discounted_price;
                    }else if($discount_rate != 0) {
                        $selling_price = $discounted_price;
                    }else{
                        $selling_price = $price;
                    }

                    $stmt = $db->query("UPDATE {$db->prefix}products SET `price`=:price, `discount_rate`=:discount_rate, `discounted_price`=:discounted_price, `cart_discount_rate`=:cart_discount_rate, `cart_discounted_price`=:cart_discounted_price, `buying_price`=:buying_price, `vat`=:vat, selling_price=:selling_price WHERE `id`=:id", array(':price'=>$price, ':discount_rate'=>$discount_rate, ':discounted_price'=>$discounted_price, ':cart_discount_rate'=>$cart_discount_rate, ':cart_discounted_price'=>$cart_discounted_price, ':vat'=>$vat, ':selling_price'=>$selling_price, ':buying_price'=>$buying_price, ':id'=>$id));
                    if($stmt->error){
                        $utils->errorLog("error", "Cannot update product price. 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 fiyatı güncellenemedi."; 
                        header("Refresh: 0;");
                        exit();
                    }else{
                        $session->data['messageType'] = "success"; 
                        $session->data['messageTitle'] = "Başarılı!"; 
                        $session->data['message'] = "Ürün fiyatı başarıyla güncellendi."; 
                        header("Refresh: 0;");
                        exit();
                    }
                break;
                case 'description-details':
                    $id                  = $_GET['id'];
                    $description         = $_POST['description'];
                    $info_1              = $_POST['info_1'];
                    $info_2              = $_POST['info_2'];
                    $info_3              = $_POST['info_3'];
                    $short_description   = $_POST['short_description'];
                    $return_policy       = $_POST['return_policy'];

                    $stmt = $db->query("UPDATE {$db->prefix}products SET `description`=:description, `info_1`=:info_1, `info_2`=:info_2, `info_3`=:info_3, `short_description`=:short_description, `return_policy`=:return_policy WHERE `id`=:id", array(':description'=>$description, ':info_1'=>$info_1, ':info_2'=>$info_2, ':info_3'=>$info_3, ':short_description'=>$short_description, ':return_policy'=>$return_policy, ':id'=>$id));
                    if($stmt->error){
                        $utils->errorLog("error", "Cannot update product details. 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 açıklamaları güncellenemedi."; 
                        header("Refresh: 0;");
                        exit();
                    }else{
                        $session->data['messageType'] = "success"; 
                        $session->data['messageTitle'] = "Başarılı!"; 
                        $session->data['message'] = "Ürün açıklamaları başarıyla güncellendi."; 
                        header("Refresh: 0;");
                        exit();
                    }
                break;
                case 'stock-details':
                    $id                         = $_GET['id'];
                    $stock_tracking             = $_POST['stock_tracking'];
                    $stock_unit                 = $_POST['stock_unit'];
                    $stock_level                = $_POST['stock_level'];
                    $stock_critical_level       = $_POST['stock_critical_level'];

                    $stmt = $db->query("UPDATE {$db->prefix}products SET `stock_tracking`=:stock_tracking, `stock_unit`=:stock_unit, `stock_level`=:stock_level, `stock_critical_level`=:stock_critical_level WHERE `id`=:id", 
                    array(':stock_tracking'=>$stock_tracking, ':stock_unit'=>$stock_unit, ':stock_level'=>$stock_level, ':stock_critical_level'=>$stock_critical_level, ':id'=>$id));
                    if($stmt->error){
                        $utils->errorLog("error", "Cannot update stock details. Product ID: {$id} 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 stok bilgileri güncellenemedi."; 
                        header("Refresh: 0;");
                        exit();
                    }else{
                        $session->data['messageType'] = "success"; 
                        $session->data['messageTitle'] = "Başarılı!"; 
                        $session->data['message'] = "Stok bilgileri başarıyla güncellendi."; 
                        header("Refresh: 0;");
                        exit();
                    }
                break;
                case 'extra-details':
                    $id                         = $_GET['id'];
                    $min_order_quantity         = (int)$_POST['min_order_quantity'];
                    $max_order_quantity         = (int)$_POST['max_order_quantity'];
                    $search_keywords            = $_POST['search_keywords'];
                    $delivery_time              = $_POST['delivery_time'];
                    $delivery_description       = $_POST['delivery_description'];
                    $meta_title                 = $_POST['meta_title'];
                    $meta_description           = $_POST['meta_description'];

                    if($max_order_quantity < 1) {
                        $max_order_quantity = 9999;
                    }

                    $stmt = $db->query("UPDATE {$db->prefix}products SET `min_order_quantity`=:min_order_quantity, `max_order_quantity`=:max_order_quantity, `search_keywords`=:search_keywords, `delivery_time`=:delivery_time, `delivery_description`=:delivery_description WHERE `id`=:id", 
                    array(':min_order_quantity'=>$min_order_quantity, ':max_order_quantity'=>$max_order_quantity, ':search_keywords'=>$search_keywords, ':delivery_time'=>$delivery_time, ':delivery_description'=>$delivery_description, ':id'=>$id));
                    if($stmt->error){
                        $utils->errorLog("error", "Cannot update extra details. Product ID: {$id} 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 ek bilgiler güncellenemedi."; 
                        header("Refresh: 0;");
                        exit();
                    }else{
                        $db->query("UPDATE {$db->prefix}urls SET `title`=:title, `description`=:description WHERE `element`=:id AND url_type='product'", array(':title'=>$meta_title, ':description'=>$meta_description, ':id'=>$id));
                        $session->data['messageType'] = "success"; 
                        $session->data['messageTitle'] = "Başarılı!"; 
                        $session->data['message'] = "Ek bilgiler başarıyla güncellendi."; 
                        header("Refresh: 0;");
                        exit();
                    }
                break;
            }
        }
	}
    
    $brands 	            = $product->getBrands();
    $badges 	            = $product->getBadges(array('status'=>1));
    $sellers 	            = $seller->getSellers(array('status'=>1));
    $suppliers 	            = $product->getSuppliers();
    $categories 	        = $product->getCategories();
    $tags 	                = $product->getTags();
    $product_images         = $product->getProductImages($_GET["id"]);
    $cities 	            = $utils->getCities();
    $seo 	                = $utils->getItemURL($_GET["id"], "product");
    $shipping_agencies      = $order->getShippingAgenciesList();
?>
<?php include DIR_VIEWS."parts/header.php"; ?>
        <main class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-4 col-xl-3">
                        <div class="card">
                            <div class="list-group list-group-flush" id="profile-settings" role="tablist">
                                <a class="list-group-item list-group-item-action active" data-bs-toggle="tab" href="#generalTab" aria-selected="true">Genel Bilgiler</a>
                                <a class="list-group-item list-group-item-action" data-bs-toggle="tab" href="#pricingTab" aria-selected="false">Fiyatlandırma</a>
                                <a class="list-group-item list-group-item-action" data-bs-toggle="tab" href="#descriptionsTab" aria-selected="false">Açıklamalar</a>
                                <a class="list-group-item list-group-item-action" data-bs-toggle="tab" href="#stockTab" aria-selected="false">Stok</a>
                                <a class="list-group-item list-group-item-action" data-bs-toggle="tab" href="#extrasTab" aria-selected="false">Ek Bilgiler</a>
                                <a class="list-group-item list-group-item-action" data-bs-toggle="tab" href="#extrasImages" aria-selected="false">Ek Görseller</a>
                            </div>
                        </div>
                    </div>

                    <div class="col-md-8 col-xl-9">
                        <div class="tab-content" id="tab-content">
                            <div class="tab-pane fade active show" id="generalTab" role="tabpanel">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title">Genel Bilgiler</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">Ürün Adı</label>
                                                </div>
                                                <div class="col-md-9">
                                                    <input type="text" class="form-control" name="title" id="title" placeholder="Ürün adı" value="<?php echo htmlspecialchars($product_details["title"], ENT_QUOTES); ?>">
                                                </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-4">
                                                    <select name="status" id="status" class="form-control form-select">
                                                        <option value="1" <?php echo $product_details["status"] == "1" ? 'selected' : ''; ?>>Satışta</option>
                                                        <option value="0" <?php echo $product_details["status"] == "0" ? 'selected' : ''; ?>>Satışa Kapalı</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="sku" class="col-form-label fw-bold">Stok Kodu</label>
                                                </div>
                                                <div class="col-md-4">
                                                    <input type="text" class="form-control" name="sku" id="sku" placeholder="sku" value="<?php echo htmlspecialchars($product_details["sku"], ENT_QUOTES); ?>">
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="ean" class="col-form-label fw-bold">Barkod</label>
                                                </div>
                                                <div class="col-md-4">
                                                    <input type="text" class="form-control" name="ean" id="ean" placeholder="ör: 98765432198" value="<?php echo htmlspecialchars($product_details["ean"], ENT_QUOTES); ?>">
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="brand_id" class="col-form-label fw-bold">Marka</label>
                                                </div>
                                                <div class="col-md-4">
                                                    <select name="brand_id" id="brand_id" class="form-control form-select js-choice">
                                                        <option value="" <?php echo empty($product_details["brand_id"]) ? 'selected' : ''; ?>>Seçiniz</option>
                                                        <?php foreach ($brands as $b): ?>
                                                            <option value="<?php echo $b['id']; ?>" <?php echo $b['id'] == $product_details["brand_id"] ? 'selected' : ''; ?>><?php echo htmlspecialchars($b['title'], ENT_QUOTES); ?></option>
                                                        <?php endforeach; ?>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="tag_id" class="col-form-label fw-bold">Tag</label>
                                                </div>
                                                <div class="col-md-4">
                                                    <select name="tag_id" id="tag_id" class="form-control form-select js-choice">
                                                        <option value="" <?php echo empty($product_details["tag_id"]) ? 'selected' : ''; ?>>Seçiniz</option>
                                                        <?php foreach ($tags as $b): ?>
                                                            <option value="<?php echo $b['id']; ?>" <?php echo $b['id'] == $product_details["tag_id"] ? 'selected' : ''; ?>><?php echo htmlspecialchars($b['title'], ENT_QUOTES); ?></option>
                                                        <?php endforeach; ?>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="seller_id" class="col-form-label fw-bold">Satıcı</label>
                                                </div>
                                                <div class="col-md-4">
                                                    <select name="seller_id" id="seller_id" class="form-control form-select js-choice">
                                                        <option value="">Seçiniz</option>
                                                        <?php foreach ($sellers as $b): ?>
                                                            <option value="<?php echo $b['id']; ?>" <?php echo $b['id'] == $product_details["seller_id"] ? 'selected' : ''; ?>><?php echo htmlspecialchars($b['short_name'], ENT_QUOTES); ?></option>
                                                        <?php endforeach; ?>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="row mb-3 d-none">
                                                <div class="col-md-3">
                                                    <label for="supplier_id" class="col-form-label fw-bold">Tedarikçi</label>
                                                </div>
                                                <div class="col-md-4">
                                                    <select name="supplier_id" id="supplier_id" class="form-control form-select js-choice">
                                                        <option value="">Seçiniz</option>
                                                        <?php foreach ($suppliers as $s): ?>
                                                            <option value="<?php echo $s['id']; ?>" <?php echo $s['id'] == $product_details["supplier_id"] ? 'selected' : ''; ?>><?php echo htmlspecialchars($s['title'], ENT_QUOTES); ?></option>
                                                        <?php endforeach; ?>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="category" class="col-form-label fw-bold">Kategori</label>
                                                </div>
                                                <div class="col-md-9">
                                                    <select name="category[]" id="category" class="form-control form-select js-choice" multiple>
                                                        <option value="">Seçiniz</option>
                                                        <?php foreach ($categories as $c): ?>
                                                            <option value="<?php echo $c['id']; ?>" <?php echo in_array($c['id'], $product_details["categories"]) ? 'selected' : ''; ?> class="fw-bold"><?php echo htmlspecialchars($c['title'], ENT_QUOTES); ?></option>
                                                            <?php if (!empty($c['sub_items'])): ?>
                                                                <?php foreach ($c['sub_items'] as $s1): ?>
                                                                    <option value="<?php echo $s1['id']; ?>" <?php echo in_array($s1['id'], $product_details["categories"]) ? 'selected' : ''; ?>>&nbsp;&nbsp;<?php echo htmlspecialchars($c['title'], ENT_QUOTES); ?> > <?php echo htmlspecialchars($s1['title'], ENT_QUOTES); ?></option>
                                                                    <?php if (!empty($s1['sub_items'])): ?>
                                                                        <?php foreach ($s1['sub_items'] as $s2): ?>
                                                                            <option value="<?php echo $s2['id']; ?>" <?php echo in_array($s2['id'], $product_details["categories"]) ? 'selected' : ''; ?>>&nbsp;&nbsp;&nbsp;&nbsp;<?php echo htmlspecialchars($c['title'], ENT_QUOTES); ?> > <?php echo htmlspecialchars($s1['title'], ENT_QUOTES); ?> > <?php echo htmlspecialchars($s2['title'], ENT_QUOTES); ?></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="badges" class="col-form-label fw-bold">Ürün Rozetleri</label>
                                                </div>
                                                <div class="col-md-9">
                                                    <select name="badges[]" id="badges" class="form-control form-select js-choice" multiple>
                                                        <option value="">Seçiniz</option>
                                                        <?php foreach ($badges as $c): ?>
                                                            <option value="<?php echo $c['id']; ?>" <?php echo in_array($c['id'], $product_details["badges"]) ? 'selected' : ''; ?> class="fw-bold"><?php echo htmlspecialchars($c['description'], ENT_QUOTES); ?></option>
                                                        <?php endforeach; ?>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="shipping_agencies" class="col-form-label fw-bold">Kargo Şirketleri</label>
                                                </div>
                                                <div class="col-md-9">
                                                    <select name="shipping_agencies[]" id="shipping_agencies" class="form-control form-select js-choice" multiple required>
                                                        <option value="">Seçiniz</option>
                                                        <?php foreach ($shipping_agencies as $s): ?>
                                                            <option value="<?php echo $s['id']; ?>" <?php echo in_array($s['id'], $product_details["shipping_agencies"]) ? 'selected' : ''; ?>><?php echo $s['title']; ?></option>
                                                        <?php endforeach; ?>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="delivery_areas" class="col-form-label fw-bold">Teslimat Bölgeleri</label>
                                                </div>
                                                <div class="col-md-4">
                                                    <select name="delivery_areas[]" id="delivery_areas" class="form-control form-select js-choice" multiple>
                                                        <option value="">Tüm Türkiye</option>
                                                        <?php foreach ($cities as $s): ?>
                                                            <option value="<?php echo $s['plate_no']; ?>" <?php echo in_array($s['plate_no'], $product_details["delivery_areas"]) ? 'selected' : ''; ?>><?php echo $s['name']; ?></option>
                                                        <?php endforeach; ?>
                                                    </select>
                                                </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/products/large/<?php echo $product_details["image"]; ?>" class="glightbox text-reset"><i class="bi bi-image"></i></a></label>
                                                </div>
                                                <div class="col-md-4">
                                                    <input type="file" class="form-control" name="image" id="image">
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="featured" class="col-form-label fw-bold">Öne Çıkanlar?</label>
                                                </div>
                                                <div class="col-md-9 position-relative">
                                                    <input type="checkbox" class="col-form-label" name="featured" id="featured" <?php echo $product_details["featured"] == 1 ? 'checked' : ''; ?>>
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="checkout_offer" class="col-form-label fw-bold">Kasa Önü?</label>
                                                </div>
                                                <div class="col-md-9 position-relative">
                                                    <input type="checkbox" class="col-form-label" name="checkout_offer" id="checkout_offer" <?php echo $product_details["checkout_offer"] == 1 ? 'checked' : ''; ?>>
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="new" class="col-form-label fw-bold">Yeni Gelenler?</label>
                                                </div>
                                                <div class="col-md-9 position-relative">
                                                    <input type="checkbox" class="col-form-label" name="new" id="new" <?php echo $product_details["new"] == 1 ? 'checked' : ''; ?>>
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="free_shipping" class="col-form-label fw-bold">Ücretsiz Kargo?</label>
                                                </div>
                                                <div class="col-md-9 position-relative">
                                                    <input type="checkbox" class="col-form-label" name="free_shipping" id="free_shipping" <?php echo $product_details["free_shipping"] == 1 ? 'checked' : ''; ?>>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-12 text-end">
                                                    <a href="<?php echo $redirect_url; ?>" class="btn btn-secondary me-2">Listeye Dön</a>
                                                    <input type="submit" name="update" class="btn btn-primary" value="Güncelle">
                                                </div>
                                            </div>
                                            <input type="hidden" name="action" value="product-details">
                                        </form>
                                    </div>
                                </div>
                            </div>

                            <div class="tab-pane fade" id="pricingTab" role="tabpanel">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title">Fiyatlandırma</h5>
                                    </div>
                                    <div class="card-body">
                                        <form action="" method="post">
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="price" class="col-form-label fw-bold">Etiket Fiyatı</label>
                                                </div>
                                                <div class="col-md-3">
                                                    <input type="number" step="0.01" class="form-control" name="price" id="price" placeholder="00.00" value="<?php echo $product_details["price"]; ?>">
                                                </div>
                                                <div class="col-md-3">
                                                    <label for="buying_price" class="col-form-label fw-bold">Alış Fiyatı</label>
                                                </div>
                                                <div class="col-md-3">
                                                    <input type="number" step="0.01" class="form-control" name="buying_price" id="buying_price" placeholder="00.00" value="<?php echo $product_details["buying_price"]; ?>">
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="discount_rate" class="col-form-label fw-bold">İndirim Oranı</label>
                                                </div>
                                                <div class="col-md-3">
                                                    <input type="number" step="0.01" class="form-control" name="discount_rate" id="discount_rate" placeholder="00.00" value="<?php echo $product_details["discount_rate"]; ?>">
                                                </div>
                                                <div class="col-md-3">
                                                    <label for="discounted_price" class="col-form-label fw-bold">Satış Fiyatı</label>
                                                </div>
                                                <div class="col-md-3">
                                                    <input type="number" step="0.01" class="form-control" name="discounted_price" id="discounted_price" placeholder="00.00" value="<?php echo $product_details["discounted_price"]; ?>">
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="cart_discount_rate" class="col-form-label fw-bold">Sepette İndirim Oranı (opsiyonel)</label>
                                                </div>
                                                <div class="col-md-3">
                                                    <input type="number" step="0.01" class="form-control" name="cart_discount_rate" id="cart_discount_rate" placeholder="00.00" value="<?php echo $product_details["cart_discount_rate"]; ?>">
                                                </div>
                                                <div class="col-md-3">
                                                    <label for="cart_discounted_price" class="col-form-label fw-bold">Sepette Satış Fiyatı (opsiyonel)</label>
                                                </div>
                                                <div class="col-md-3">
                                                    <input type="number" step="0.01" class="form-control" name="cart_discounted_price" id="cart_discounted_price" placeholder="00.00" value="<?php echo $product_details["cart_discounted_price"]; ?>">
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="vat" class="col-form-label fw-bold">KDV Oranı</label>
                                                </div>
                                                <div class="col-md-3">
                                                    <input type="number" step="0.01" class="form-control" name="vat" id="vat" placeholder="00.00" value="<?php echo $product_details["vat"]; ?>">
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-12 text-end">
                                                    <a href="<?php echo $redirect_url; ?>" class="btn btn-secondary me-2">Listeye Dön</a>
                                                    <input type="submit" name="update" class="btn btn-primary" value="Güncelle">
                                                </div>
                                            </div>
                                            <input type="hidden" name="action" value="pricing-details">
                                        </form>
                                    </div>
                                </div>
                            </div>

                            <div class="tab-pane fade" id="descriptionsTab" role="tabpanel">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title">Açıklamalar</h5>
                                    </div>
                                    <div class="card-body">
                                        <form action="" method="post">
                                            <div class="row mb-3">
                                                <div class="col-md-12">
                                                    <label for="short_description" class="col-form-label fw-bold">Kısa Açıklama</label>
                                                </div>
                                                <div class="col-md-12">
                                                    <textarea class="form-control editor" name="short_description" id="short_description" rows="10"><?php echo $product_details["short_description"]; ?></textarea>
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-12">
                                                    <label for="description" class="col-form-label fw-bold">Açıklama</label>
                                                </div>
                                                <div class="col-md-12">
                                                    <textarea class="form-control editor" name="description" id="description" rows="10"><?php echo $product_details["description"]; ?></textarea>
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-12">
                                                    <label for="info_1" class="col-form-label fw-bold">Aktif İçerikler</label>
                                                </div>
                                                <div class="col-md-12">
                                                    <textarea class="form-control editor" name="info_1" id="info_1" rows="10"><?php echo $product_details["info_1"]; ?></textarea>
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-12">
                                                    <label for="info_2" class="col-form-label fw-bold">Kullanım Şekli</label>
                                                </div>
                                                <div class="col-md-12">
                                                    <textarea class="form-control editor" name="info_2" id="info_2" rows="10"><?php echo $product_details["info_2"]; ?></textarea>
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-12">
                                                    <label for="info_3" class="col-form-label fw-bold">Ek Bilgi 3</label>
                                                </div>
                                                <div class="col-md-12">
                                                    <textarea class="form-control editor" name="info_3" id="info_3" rows="10"><?php echo $product_details["info_3"]; ?></textarea>
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-12">
                                                    <label for="return_policy" class="col-form-label fw-bold">İade Açıklaması</label>
                                                </div>
                                                <div class="col-md-12">
                                                    <textarea class="form-control editor" name="return_policy" id="return_policy" rows="10"><?php echo $product_details["return_policy"]; ?></textarea>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-12 text-end">
                                                    <a href="<?php echo $redirect_url; ?>" class="btn btn-secondary me-2">Listeye Dön</a>
                                                    <input type="submit" name="update" class="btn btn-primary" value="Güncelle">
                                                </div>
                                            </div>
                                            <input type="hidden" name="action" value="description-details">
                                        </form>
                                    </div>
                                </div>
                            </div>

                            <div class="tab-pane fade" id="stockTab" role="tabpanel">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title">Stok</h5>
                                    </div>
                                    <div class="card-body">
                                        <form action="" method="post">
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="stock_tracking" class="col-form-label fw-bold">Stok Takibi Yapılsın mı?</label>
                                                </div>
                                                <div class="col-md-3">
                                                    <select name="stock_tracking" id="stock_tracking" class="form-control form-select">
                                                        <option value="1" <?php echo $product_details["stock_tracking"] == "1" ? 'selected' : ''; ?>>Evet</option>
                                                        <option value="0" <?php echo $product_details["stock_tracking"] == "0" ? 'selected' : ''; ?>>Hayır</option>
                                                    </select>
                                                </div>
                                                <div class="col-md-3">
                                                    <label for="stock_unit" class="col-form-label fw-bold">Stok Birimi</label>
                                                </div>
                                                <div class="col-md-3">
                                                    <select name="stock_unit" id="stock_unit" class="form-control form-select">
                                                        <?php
                                                            $stmt  	= $db->query("SELECT * FROM ".$db->prefix."stock_units");
                                                            foreach ($stmt->rows as $row): ?>
                                                                <option value="<?php echo $row["id"]; ?>" <?php echo $row["id"] == $product_details["stock_unit"] ? 'selected' : ''; ?>><?php echo $row["title"]; ?></option>
                                                            <?php endforeach; ?>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="stock_level" class="col-form-label fw-bold">Stok Miktarı</label>
                                                </div>
                                                <div class="col-md-3">
                                                    <input type="number" step="1" class="form-control" name="stock_level" id="stock_level" placeholder="0" value="<?php echo $product_details["stock_level"]; ?>">
                                                </div>
                                                <div class="col-md-3">
                                                    <label for="stock_critical_level" class="col-form-label fw-bold">Kritik Stok Seviyesi</label>
                                                </div>
                                                <div class="col-md-3">
                                                    <input type="number" step="1" class="form-control" name="stock_critical_level" id="stock_critical_level" placeholder="0" value="<?php echo $product_details["stock_critical_level"]; ?>">
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-12 text-end">
                                                    <a href="<?php echo $redirect_url; ?>" class="btn btn-secondary me-2">Listeye Dön</a>
                                                    <input type="submit" name="update" class="btn btn-primary" value="Güncelle">
                                                </div>
                                            </div>
                                            <input type="hidden" name="action" value="stock-details">
                                        </form>
                                    </div>
                                </div>
                            </div>

                            <div class="tab-pane fade" id="extrasTab" role="tabpanel">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title">Ek Bilgiler</h5>
                                    </div>
                                    <div class="card-body">
                                        <form action="" method="post">
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="min_order_quantity" class="col-form-label fw-bold">Minimum Adet <span class="bi bi-info-circle-fill" role="button" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Müşteriler belirtilen adetin altında bu üründen satın alamaz."></span></label>
                                                </div>
                                                <div class="col-md-3">
                                                    <input type="number" step="1" class="form-control" name="min_order_quantity" id="min_order_quantity" placeholder="1" value="<?php echo $product_details["min_order_quantity"]; ?>">
                                                </div>
                                                <div class="col-md-3">
                                                    <label for="max_order_quantity" class="col-form-label fw-bold">Maksimum Adet <span class="bi bi-info-circle-fill" role="button" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Müşteriler bu üründen tek seferde en fazla belirtilen adet kadar satın alabilir"></span></label>
                                                </div>
                                                <div class="col-md-3">
                                                    <input type="number" step="1" class="form-control" name="max_order_quantity" id="max_order_quantity" placeholder="2" value="<?php echo $product_details["max_order_quantity"]; ?>">
                                                </div>
                                            </div>
                                            <hr>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="delivery_time" class="col-form-label fw-bold">Teslimat Süresi (gün)</label>
                                                </div>
                                                <div class="col-md-3">
                                                    <input type="number" step="1" class="form-control" name="delivery_time" id="delivery_time" placeholder="1" value="<?php echo $product_details["delivery_time"]; ?>">
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="delivery_time" class="col-form-label fw-bold">Teslimat Açıklaması</label>
                                                </div>
                                                <div class="col-md-9">
                                                    <input type="text" class="form-control" name="delivery_description" id="delivery_description" placeholder="" value="<?php echo $product_details["delivery_description"]; ?>">
                                                </div>
                                            </div>
                                            <hr>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="search_keywords" class="col-form-label fw-bold">Arama Kelimeleri <span class="bi bi-info-circle-fill" role="button" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Site içi aramada ürünlerin daha kolay bulunmasını sağlar. Kelimeler arasında boşluk kullanabilirsiniz."></span></label>
                                                </div>
                                                <div class="col-md-9">
                                                    <input type="text" class="form-control" name="search_keywords" id="search_keywords" placeholder="" value="<?php echo htmlspecialchars($product_details["search_keywords"], ENT_QUOTES); ?>">
                                                </div>
                                            </div>
                                            <hr>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="meta_title" class="col-form-label fw-bold">Meta Başlığı <span class="bi bi-info-circle-fill" role="button" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Ürünün arama motorları tarafından görüntülenen adı. Boş bırakılırsa ürün adını alır."></span></label>
                                                </div>
                                                <div class="col-md-9">
                                                    <input type="text" class="form-control" name="meta_title" id="meta_title" placeholder="0" value="<?php echo htmlspecialchars($seo["title"], ENT_QUOTES); ?>">
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="meta_title" class="col-form-label fw-bold">Meta Açıklama <span class="bi bi-info-circle-fill" role="button" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Arama motorları tarafından görüntülenen ürün açıklaması. Boş bırakılırsa ürün kısa açıklamasını alır."></span></label>
                                                </div>
                                                <div class="col-md-9">
                                                    <textarea name="meta_description" id="meta_description" rows="2" class="form-control"><?php echo $seo["description"]; ?></textarea>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-12 text-end">
                                                    <a href="<?php echo $redirect_url; ?>" class="btn btn-secondary me-2">Listeye Dön</a>
                                                    <input type="submit" name="update" class="btn btn-primary" value="Güncelle">
                                                </div>
                                            </div>
                                            <input type="hidden" name="action" value="extra-details">
                                        </form>
                                    </div>
                                </div>
                            </div>

                            <div class="tab-pane fade" id="extrasImages" role="tabpanel">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title">Ek Görseller</h5>
                                    </div>
                                    <div class="card-body px-0 pt-0">
                                        <div class="table-responsive">
                                            <table class="table table-bordered table-hover table-condensed table-striped table-cf" id="images-table">
                                                <thead>
                                                    <tr>
                                                        <th width="60px">Dosya</th>
                                                        <th>Dosya Adı</th>
                                                        <th>Meta Açıklaması</th>
                                                        <th width="40px">Sıra</th>
                                                        <th width="30px">Sil</th>
                                                    </tr>
                                                </thead>
                                                <tbody class="text-left products">
                                                    <?php
                                                        $image_order = 1;
                                                        if(!empty($product_images)):
                                                            foreach($product_images as $row):
                                                        ?>
                                                            <tr>
                                                                <td><a href="/public/assets/images/products/large/<?php echo $row["image"]; ?>" class="glightbox text-reset"><img src="/public/assets/images/products/thumb/<?php echo $row["image"]; ?>" alt=""></a></td>
                                                                <td><a href="/public/assets/images/products/medium/<?php echo $row["image"]; ?>" target="_blank">/public/assets/images/products/medium/<?php echo $row["image"]; ?></a></td>
                                                                <td><?php echo $row["alt_text"]; ?></td>
                                                                <td class="text-center"><?php echo $row["sort_order"]; ?></td>
                                                                <td class="text-center"><button type="button" class="btn btn-danger btn-sm" onclick="deleteImage(this, <?php echo $row['id']; ?>)"><i class="bi bi-trash"></i></button></td>
                                                            </tr>
                                                        <?php
                                                                $image_order = $row["sort_order"]+1;
                                                            endforeach;
                                                        endif;
                                                    ?>
                                                </tbody>
                                            </table>
                                        </div>
                                        <div class="row">
                                            <div class="col-12 text-center">
                                                <button type="button" data-bs-toggle="modal" data-bs-target="#new-image-modal" class="btn btn-primary btn-sm"><i class="bi bi-plus-circle-dotted"></i> Yeni Görsel</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
        <form method='post' id="new-image-form" enctype="multipart/form-data">
            <div id="new-image-modal" class="modal fade" role="dialog">
                <div class="modal-dialog modal-dialog-centered">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title">Yeni Görsel</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                        </div>
                        <div class="modal-body">
                            <div class="row mb-3">
                                <label for="image_additional" class="col-form-label">Görsel seç</label>
                                <div class="col-12">
                                    <input type="file" name="image" id="image_additional" class='form-control' >
                                </div>
                            </div>
                            <div class="row mb-3">
                                <label for="alt_text" class="col-form-label">Meta Açıklaması</label>
                                <div class="col-12">
                                    <input type='text' name='alt_text' id='alt_text' class='form-control' value="<?php echo htmlspecialchars($product_details["title"], ENT_QUOTES); ?>">
                                </div>
                            </div>
                            <div class="row mb-3">
                                <label for="order" class="col-form-label">Sıra</label>
                                <div class="col-md-6">
                                    <input type='number' name='order' id='order' class='form-control' value="<?php echo $image_order;?>">
                                </div>
                            </div>
                            <input type="hidden" name="product_id" value="<?php echo $_GET["id"];?>">
                            <input type="hidden" name="product_title" value="<?php echo htmlspecialchars($seo["url"], ENT_QUOTES); ?>">
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Vazgeç</button>
                            <button type="submit" class="btn btn-primary" id='btn-upload'>Yükle</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
<?php include DIR_VIEWS."parts/footer.php"; ?>