<?php 
    if(!isset($_GET["id"]) || empty($_GET["id"])){
        header("Location: /admin/tutor/list");
        exit();
    }
    $tutor_details = $course->getTutor($_GET["id"]);
    if(empty($tutor_details)){
        header("Location: /admin/tutor/list");
        exit();
    }

    $data = array();
    $data['title'] = "Eğitmen Güncelle | ".$App->title;
    $data['links'] = [];
    $data['styles'] = [];
    $data['header']['scripts'] = [];
    $data['version'] = $App->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' => "Eğitmenler",
        'href' => "/admin/tutor/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Eğitmen Güncelle",
        'href' => "#",
        'index' => 3
    );

    $response = array();
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $name               = $_POST['name'];
        $title              = $_POST['title'];
        $status             = $_POST['status'];
        $short_description  = $_POST['short_description'];
        $description        = $_POST['description'];
        $resume             = $_POST['resume'];

        $title_seo          = $utils->generateUrl($title);
        $meta_title	        = $title;
        $meta_description   = $short_description;
		
		if(!isset($_FILES['image']) || !file_exists($_FILES['image']['tmp_name']) || !is_uploaded_file($_FILES['image']['tmp_name'])){
            $image               = $tutor_details["image"];
        } else {
            $image_parts         = pathinfo($_FILES['image']["name"]);
            $image               = $title_seo."-".uniqid().".".$image_parts['extension'];
            $utils->resizeCropImage(400, 500, $_FILES['image']['tmp_name'], "../assets/images/tutors/".$image);
            $utils->resizeCropImage(170, 170, $_FILES['image']['tmp_name'], "../assets/images/tutors/thumb/".$image);
        }

        $stmt = $db->query("UPDATE {$db->prefix}tutors SET `status`=:status, `name`=:name, `description`=:description, `image`=:image, `title`=:title, `short_description`=:short_description, `resume`=:resume WHERE id=:id",array(':title'=>$title, ':status'=>$status, ':name'=>$name, ':image'=>$image, ':resume'=>$resume, ':description'=>$description, ':short_description'=>$short_description, ':id'=>$_GET["id"]));
        if($stmt->error){
            $utils->errorLog("error", "Cannot update tutor. 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 eğitmen güncellenemedi."; 
        }else{
            $utils->updateUrl("tutor", $_GET["id"], $meta_title, $meta_description);

            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Eğitmen başarıyla güncellendi."; 
        }
	}

?>
<?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">Eğitmen Bilgileri</h5>
                            </div>
                            <div class="card-body">
                                <form action="" method="post" enctype="multipart/form-data">
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="name" class="form-label font-weight-bold">Eğitmen Adı</label>
                                        </div>
                                        <div class="col-md-10">
                                            <input type="text" class="form-control" name="name" id="name" placeholder="Eğitmen Adı" value="<?php echo $tutor_details["name"]; ?>" required>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="title" class="form-label font-weight-bold">Unvan</label>
                                        </div>
                                        <div class="col-md-6">
                                            <input type="text" class="form-control" name="title" id="title" placeholder="unvan" value="<?php echo $tutor_details["title"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-2">
                                            <label for="image" class="form-label font-weight-bold">Görsel</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-2">
                                            <label for="status" class="form-label font-weight-bold">Durum</label>
                                        </div>
                                        <div class="col-md-4">
                                            <select name="status" id="status" class="form-control form-select">
                                                <option value="1" <?php echo $tutor_details["status"] == "1" ? 'selected' : ''; ?>>Aktif</option>
                                                <option value="0" <?php echo $tutor_details["status"] == "0" ? 'selected' : ''; ?>>Pasif</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-12">
                                            <label for="short_description" class="form-label font-weight-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 $tutor_details["short_description"]; ?></textarea>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-12">
                                            <label for="description" class="form-label font-weight-bold">Açıklama</label>
                                        </div>
                                        <div class="col-md-12">
                                            <textarea class="form-control editor" name="description" id="description" rows="10"><?php echo $tutor_details["description"]; ?></textarea>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-12">
                                            <label for="resume" class="form-label font-weight-bold">Özgeçmiş</label>
                                        </div>
                                        <div class="col-md-12">
                                            <textarea class="form-control editor" name="resume" id="resume" rows="10"><?php echo $tutor_details["resume"]; ?></textarea>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-12 text-end">
                                            <a href="/admin/tutor/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"; ?>