<?php 
    $data = array();
    $data['title'] = "Kargo Yöntemi Ekle | ".$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/shipping/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' => "Kargo Yönetimi",
        'href' => "/admin/shipping/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Kargo Yöntemi Ekle",
        'href' => "#",
        'index' => 3
    );

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $status         = $_POST['status'];
        $title          = $_POST['title'];
        $is_default     = $_POST['is_default'];
        $sort_order     = $_POST['sort_order'];

        if($is_default == 1) {
            $db->query("UPDATE {$db->prefix}shipping_agencies SET `is_default`=0");
        }

        if(!isset($_FILES['image']) || !file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name'])){
            $image               = "default.png";
        } 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/shipping-agencies/".$image);
        }

        $stmt = $db->query("INSERT INTO {$db->prefix}shipping_agencies (`status`, `title`, `is_default`, `sort_order`, image) VALUES (:status, :title, :is_default, :sort_order, :image)", array(':status'=>$status, ':title'=>$title, ':is_default'=>$is_default, ':sort_order'=>$sort_order, ':image'=>$image));
        if($stmt->error){
            $utils->errorLog("error", "Cannot add shipping agency. 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 kargo yöntemi eklenemedi."; 
            header("Refresh: 0;");
            exit();
        }else{
            $inserted_id = $db->getLastId();
            if(!empty($_POST['shipping-prices-min'])) {
                for($i=0; $i<count($_POST['shipping-prices-min']); $i++) {
                    $shipping_prices_min = $_POST['shipping-prices-min'][$i];
                    $shipping_prices_max = $_POST['shipping-prices-max'][$i];
                    $shipping_prices_price = $_POST['shipping-prices-price'][$i];
                    $db->query("INSERT INTO {$db->prefix}shipping_prices (`minimum`, `maximum`, `price`, shipping_agency_id) VALUES (:minimum, :maximum, :price, :shipping_agency_id)", array(':shipping_agency_id'=>(int)$inserted_id, ':minimum'=>$shipping_prices_min, ':maximum'=>$shipping_prices_max, ':price'=>$shipping_prices_price));
                }
            }


            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Kargo yöntemi başarıyla eklendi."; 
            header("Refresh: 0;");
            exit();
        }
	}
    
?>
<?php include DIR_VIEWS."parts/header.php"; ?>
        <main class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-header">
                                <h5 class="card-title">Kargo Yöntemi Detayları</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="form-label fw-bold">Kargo Firması</label>
                                        </div>
                                        <div class="col-md-9">
                                            <input type="text" class="form-control" name="title" id="title" placeholder="title" value="">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="status" class="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" selected>Aktif</option>
                                                <option value="0">Pasif</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="is_default" class="form-label fw-bold">Varsayılan mı?</label>
                                        </div>
                                        <div class="col-md-4">
                                            <select name="is_default" id="is_default" class="form-control form-select">
                                                <option value="1">Evet</option>
                                                <option value="0" selected>Hayır</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="sort_order" class="form-label fw-bold">Sıralama</label>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="number" class="form-control" name="sort_order" id="sort_order" placeholder="sıralama" value="1">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="image" class="form-label fw-bold">Görsel <a href="/public/assets/images/shipping-agencies/default.png" 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 class="form-label fw-bold">Tutar Bilgileri</label>
                                        </div>
                                        <div class="col-md-9">
                                            <table class="table table-condensed" id="receivers-table">
                                                <thead>
                                                    <tr>
                                                        <th class="text-center">Minimum</th>
                                                        <th class="text-center">Maksimum</th>
                                                        <th class="text-center">Tutar</th>
                                                        <th style="width: 50px;">Sil</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    
                                                </tbody>
                                            </table>
                                            <div class="row">
                                                <div class="col-12 text-center">
                                                    <button type="button" class="btn btn-primary btn-sm" id="add-row"><i class="bi bi-plus-circle-dotted"></i></button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <hr>
                                    <div class="row mb-3">
                                        <div class="col-12 text-end">
                                            <a href="/admin/shipping/list" class="btn btn-secondary me-2">Listeye Dön</a>
                                            <input type="submit" name="update" class="btn btn-primary" value="Kaydet">
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
<?php include DIR_VIEWS."parts/footer.php"; ?>