<?php 
    $data = array();
    $data['title'] = $meta_title . " | " . $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'] = [$App->url.'/public/panel/assets/js/pages/account/address.js'];
    $menu_items = $menu->get(array('position'=>'footer'));

    $data['cards']["1"] = 0;
    $data['cards']["2"] = 0;
    $data['cards']["3"] = 0;

    $data['results']["1"] = 0;
    $data['results']["2"] = 0;
    $data['results']["3"] = 0;
    $data['results']["4"] = 0;

    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' => "Anasayfa",
        'href' => "/panel",
        'index' => 1
    );
    $data['breadcrumbs'][] = array(
        'text' => "Profilim",
        'href' => "/panel/kullanici-bilgilerim",
        'index' => 2
    );

    $user_details = $user->getUserDetails();

	if(isset($_POST['update'])){
		$name       = $_POST['name'];
		$surname    = $_POST['surname'];
		$phone      = $_POST['phone'];
		$birthdate  = $_POST['birthdate'];
		$password   = $_POST['password'];

		if(isset($_POST['password']) && !empty($_POST["password"])){
			$hashed_password = password_hash($_POST['password'], PASSWORD_DEFAULT);
			$stmt = $db->query("UPDATE {$db->prefix}users SET `name`=:name, `surname`=:surname, `phone`=:phone, `birthdate`=:birthdate, `password`=:password WHERE `id`=:id",array(':name'=>$name, ':surname'=>$surname, ':phone'=>$phone, ':birthdate'=>$birthdate, ':password'=>$hashed_password, ':id'=>$session->data['user_id']));
		}else{
			$stmt = $db->query("UPDATE {$db->prefix}users SET `name`=:name, `surname`=:surname, `phone`=:phone, `birthdate`=:birthdate WHERE `id`=:id",array(':name'=>$name, ':surname'=>$surname, ':phone'=>$phone, ':birthdate'=>$birthdate, ':id'=>$session->data['user_id']));
		}

		if($stmt->error){
            $this->utils->errorLog("error", "Cannot update user 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 kullanıcı bilgileriniz güncellenemedi."; 
			header("Location: /panel/kullanici-bilgilerim");
			exit();
        }else{
            $session->data['messageType'] = "success"; 
			$session->data['messageTitle'] = "Başarılı!"; 
			$session->data['message'] = "Kullanıcı bilgileriniz başarıyla güncellendi."; 
			header("Location: /panel/kullanici-bilgilerim");
			exit();
        }
	}
?>
<?php include DIR_PUBLIC."panel/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="card-header">
                                <h5 class="card-title mb-0">Hesap Ayarları</h5>
                            </div>
                            <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="#accountTab" aria-selected="true">Kullanıcı Bilgilerim</a>
                                <a class="list-group-item list-group-item-action" data-bs-toggle="tab" href="#addressTab" aria-selected="false">Adres Bilgilerim</a>
                                <a class="list-group-item list-group-item-action" data-action="logout" href="/panel/cikis-yap">Çıkış Yap</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="accountTab" role="tabpanel">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title"><i class="bi bi-person-check"></i> Kullanıcı Bilgilerim</h5>
                                    </div>
                                    <div class="card-body">
                                        <form action="" method="post">
                                            <div class="row mb-3">
                                                <div class="col-12">
                                                    <label for="user_name" class="form-label font-weight-bold">Ad <span class="text-danger">*</span></label>
                                                    <input type="text" class="form-control" name="name" id="user_name" placeholder="adınız" value="<?php echo $user_details["name"]; ?>">
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-12">
                                                    <label for="user_surname" class="form-label font-weight-bold">Soyad <span class="text-danger">*</span></label>
                                                    <input type="text" class="form-control" name="surname" id="user_surname" placeholder="soyadınız" value="<?php echo $user_details["surname"]; ?>">
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-12">
                                                    <label for="user_email" class="form-label font-weight-bold">Eposta <span class="text-danger">*</span></label>
                                                    <input type="email" class="form-control" name="email" id="user_email" placeholder="eposta adresiniz" value="<?php echo $user_details["email"]; ?>" style="cursor: not-allowed;" disabled>
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-12">
                                                    <label for="user_phone" class="form-label font-weight-bold">Telefon <span class="text-danger">*</span></label>
                                                    <input type="tel" class="form-control" name="phone" id="user_phone" placeholder="(___)___-__-__" value="<?php echo $user_details["phone"]; ?>">
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-12">
                                                    <label for="user_birthdate" class="form-label font-weight-bold">Doğum Tarihi</label>
                                                    <input type="date" class="form-control" name="birthdate" id="user_birthdate" value="<?php echo $user_details["birthdate"]; ?>">
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-12">
                                                    <label for="user_password" class="form-label font-weight-bold">Parola</label>
                                                    <input type="password" class="form-control" name="password" id="user_password" placeholder="******" value="">
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-12">
                                                    <label for="user_group" class="form-label font-weight-bold">Kullanıcı Grubu</label>
                                                    <input type="text" class="form-control" name="user_group" id="user_group" value="<?php echo $user->getUserGroup($user_details["user_group"]); ?>" style="cursor: not-allowed;" disabled>
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-12">
                                                    <input type="submit" name="update" class="btn btn-primary" value="Güncelle">
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                            <div class="tab-pane fade" id="addressTab" role="tabpanel">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title mb-0">Adres Bilgilerim</h5>
                                    </div>
                                    <div class="card-body">
                                        <div class="row">
                                            <div class="col-md-6">	
                                                <div class="card new-address mb-3">
                                                    <div class="card-body text-center">
                                                        <i class="bi bi-plus"></i>
                                                        <span>Yeni Adres Ekle</span>
                                                    </div>
                                                </div>
                                            </div>
                                            <?php
                                                $filter_data = array(
                                                    'user_id'			 => $user_id,
                                                    'search_keywords'    => "",
                                                    'default'            => 0
                                                );
                                                $address_list = $user->getAdresses($filter_data);
                                                foreach($address_list as $key => $row){
                                                    $default_addr = $row["default"] == 1 ? "<i class=\"fas fa-check default-address\"></i>" : "";
                                                    echo "<div class=\"col-md-6\">	
                                                                <div class=\"card edit-address mb-3\">
                                                                    <div class=\"card-body\">
                                                                        <div class=\"d-block\">
                                                                            <span class=\"title update-address\" data-id=\"{$row["id"]}\" title=\"Bu adresi Düzenle\">{$row["title"]} $default_addr</span>
                                                                            <span class=\"float-end bi bi-trash delete delete-address\" title=\"Bu adresi sil\" data-id=\"{$row["id"]}\"></span>
                                                                            <span class=\"float-end bi bi-pencil-square edit update-address\" data-id=\"{$row["id"]}\" title=\"Bu adresi Düzenle\"></span>
                                                                        </div>
                                                                        <div class=\"update-address\" data-id=\"{$row["id"]}\" title=\"Bu adresi Düzenle\">
                                                                            <span>{$row["firstname"]} {$row["lastname"]}</span>
                                                                            <span>{$row["address"]}</span>
                                                                            <span>{$row["town_name"]} - {$row["city_name"]}</span>
                                                                            <span>{$row["phone"]}</span>
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                            </div>";
                                                }
                                            ?>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="tab-pane fade" id="accountSecurityTab" role="tabpanel">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title mb-0"><?php echo $data['translation']['text_tfa']; ?></h5>
                                    </div>
                                    <div class="card-body">
                                        <div class="text-center">
                                            <span class="icon">
                                                <svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" width="60" height="60" viewBox="0 0 74 74" xml:space="preserve">
                                                    <path d="m34.32 49.05h-2.2a1 1 0 1 1 0-2h2.2a1 1 0 1 1 0 2z"></path>
                                                    <path d="m51.54 49.05h-4.36a1 1 0 1 1 0-2h4.36a.456.456 0 0 0 .46-.45v-17.03a.455.455 0 0 0 -.46-.46h-29.08a.455.455 0 0 0 -.46.46v17.03a.456.456 0 0 0 .46.45h4.36a1 1 0 1 1 0 2h-4.36a2.458 2.458 0 0 1 -2.46-2.45v-17.03a2.463 2.463 0 0 1 2.46-2.46h29.08a2.463 2.463 0 0 1 2.46 2.46v17.03a2.458 2.458 0 0 1 -2.46 2.45z"></path>
                                                    <path d="m41.88 49.05h-2.25a1 1 0 0 1 0-2h2.25a1 1 0 1 1 0 2z"></path>
                                                    <path d="m47.182 29.113h-20.364a1 1 0 0 1 -1-1v-8.727a11.182 11.182 0 0 1 22.364 0v8.727a1 1 0 0 1 -1 1zm-19.364-2h18.364v-7.727a9.182 9.182 0 0 0 -18.364 0z"></path>
                                                    <path d="m37 42.781a1 1 0 0 1 -1-1v-2.6a1 1 0 0 1 2 0v2.6a1 1 0 0 1 -1 1z"></path>
                                                    <path d="m37 40.176a3.4 3.4 0 1 1 3.4-3.4 3.4 3.4 0 0 1 -3.4 3.4zm0-4.791a1.4 1.4 0 1 0 1.4 1.4 1.4 1.4 0 0 0 -1.4-1.4z"></path>
                                                    <path d="m64.912 64.307a1 1 0 0 1 -.619-1.786l6.089-4.787a1 1 0 0 1 1.236 1.572l-6.089 4.787a1 1 0 0 1 -.617.214z"></path>
                                                    <path d="m71 64.307a1 1 0 0 1 -.618-.214l-6.089-4.787a1 1 0 1 1 1.236-1.572l6.089 4.787a1 1 0 0 1 -.618 1.786z"></path>
                                                    <path d="m67.956 65.8a1 1 0 0 1 -1-1v-7.768a1 1 0 0 1 2 0v7.768a1 1 0 0 1 -1 1z"></path>
                                                    <path d="m49.434 64.307a1 1 0 0 1 -.619-1.786l6.085-4.787a1 1 0 0 1 1.236 1.572l-6.088 4.787a1 1 0 0 1 -.614.214z"></path>
                                                    <path d="m55.521 64.307a1 1 0 0 1 -.617-.214l-6.089-4.787a1 1 0 1 1 1.237-1.572l6.088 4.787a1 1 0 0 1 -.619 1.786z"></path>
                                                    <path d="m52.478 65.8a1 1 0 0 1 -1-1v-7.768a1 1 0 0 1 2 0v7.768a1 1 0 0 1 -1 1z"></path>
                                                    <path d="m33.956 64.307a1 1 0 0 1 -.619-1.786l6.089-4.787a1 1 0 1 1 1.237 1.572l-6.089 4.787a1 1 0 0 1 -.618.214z"></path>
                                                    <path d="m40.044 64.307a1 1 0 0 1 -.618-.214l-6.089-4.787a1 1 0 1 1 1.237-1.572l6.089 4.787a1 1 0 0 1 -.619 1.786z"></path>
                                                    <path d="m37 65.8a1 1 0 0 1 -1-1v-7.768a1 1 0 0 1 2 0v7.768a1 1 0 0 1 -1 1z"></path>
                                                    <path d="m18.479 64.307a1 1 0 0 1 -.619-1.786l6.088-4.787a1 1 0 1 1 1.237 1.572l-6.085 4.787a1 1 0 0 1 -.621.214z"></path>
                                                    <path d="m24.566 64.307a1 1 0 0 1 -.618-.214l-6.088-4.787a1 1 0 0 1 1.24-1.572l6.089 4.787a1 1 0 0 1 -.619 1.786z"></path>
                                                    <path d="m21.522 65.8a1 1 0 0 1 -1-1v-7.768a1 1 0 0 1 2 0v7.768a1 1 0 0 1 -1 1z"></path>
                                                    <path d="m3 64.307a1 1 0 0 1 -.618-1.786l6.089-4.787a1 1 0 0 1 1.236 1.572l-6.089 4.787a1 1 0 0 1 -.618.214z"></path>
                                                    <path d="m9.088 64.307a1 1 0 0 1 -.617-.214l-6.089-4.787a1 1 0 0 1 1.236-1.572l6.089 4.787a1 1 0 0 1 -.619 1.786z"></path>
                                                    <path d="m6.044 65.8a1 1 0 0 1 -1-1v-7.768a1 1 0 0 1 2 0v7.768a1 1 0 0 1 -1 1z"></path>
                                                </svg>
                                            </span>
                                            <?php if($data['otp']): ?>
                                            <h2 class="text-center mt-3"><?php echo $data['translation']['otp_enabled']; ?></h2>
                                            <p class="mb-3"><?php echo $data['translation']['otp_description']; ?></p>
                                            <a href="#" class="btn btn-danger" data-action="disable-otp"><?php echo $data['translation']['disable_otp']; ?></a>
                                            <?php else: ?>
                                                <h2 class="text-center mt-3"><?php echo $data['translation']['otp_not_configured']; ?></h2>
                                                <p class="mb-3"><?php echo $data['translation']['otp_description']; ?></p>
                                                <a href="/account/otp-setup" class="btn btn-success"><?php echo $data['translation']['enable_otp']; ?></a>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title mb-0"><?php echo $data['translation']['text_sessions']; ?></h5>
                                    </div>
                                    <div class="card-body">
                                        <?php if(isset($data['sessions'])): ?>
                                            <?php foreach($data['sessions'] as $session): ?>
                                                <div class="d-flex align-items-center">
                                                    <?php if($session['device'] == 'desktop'): ?>
                                                        <span class="icon"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" class="bi bi-display" viewBox="0 0 16 16"><path d="M0 4s0-2 2-2h12s2 0 2 2v6s0 2-2 2h-4c0 .667.083 1.167.25 1.5H11a.5.5 0 0 1 0 1H5a.5.5 0 0 1 0-1h.75c.167-.333.25-.833.25-1.5H2s-2 0-2-2V4zm1.398-.855a.758.758 0 0 0-.254.302A1.46 1.46 0 0 0 1 4.01V10c0 .325.078.502.145.602.07.105.17.188.302.254a1.464 1.464 0 0 0 .538.143L2.01 11H14c.325 0 .502-.078.602-.145a.758.758 0 0 0 .254-.302 1.464 1.464 0 0 0 .143-.538L15 9.99V4c0-.325-.078-.502-.145-.602a.757.757 0 0 0-.302-.254A1.46 1.46 0 0 0 13.99 3H2c-.325 0-.502.078-.602.145z"/></svg></span>
                                                    <?php elseif($session['device'] == 'mobile'): ?>
                                                        <span class="icon"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" class="bi bi-phone" viewBox="0 0 16 16"> <path d="M11 1a1 1 0 0 1 1 1v12a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1h6zM5 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H5z"/> <path d="M8 14a1 1 0 1 0 0-2 1 1 0 0 0 0 2z"/> </svg></span>
                                                    <?php else: ?>
                                                        <span class="icon"><svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" class="bi bi-emoji-dizzy" viewBox="0 0 16 16"> <path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"/> <path d="M9.146 5.146a.5.5 0 0 1 .708 0l.646.647.646-.647a.5.5 0 0 1 .708.708l-.647.646.647.646a.5.5 0 0 1-.708.708l-.646-.647-.646.647a.5.5 0 1 1-.708-.708l.647-.646-.647-.646a.5.5 0 0 1 0-.708zm-5 0a.5.5 0 0 1 .708 0l.646.647.646-.647a.5.5 0 1 1 .708.708l-.647.646.647.646a.5.5 0 1 1-.708.708L5.5 7.207l-.646.647a.5.5 0 1 1-.708-.708l.647-.646-.647-.646a.5.5 0 0 1 0-.708zM10 11a2 2 0 1 1-4 0 2 2 0 0 1 4 0z"/> </svg></span>
                                                    <?php endif; ?>
                                                    <div class="flex-grow-1 ms-3">
                                                        <div class="row">
                                                            <div class="col-md-8 mb-2">
                                                                <h5 class="my-1"><?php echo $session['ip']; ?></h5>
                                                                <p class="mb-1"><?php echo $session['os']; ?> - <?php echo $session['browser']; ?></p>
                                                                <p class="mb-1"><?php echo $session['last_access']; ?></p>
                                                            </div>
                                                            <div class="col-md-4 text-md-end my-auto">
                                                                <?php if (!$session['current_session']): ?>
                                                                    <a class="btn btn-secondary btn-sm" data-action="revoke-session" data-id="<?php echo $session['session_id']; ?>"><?php echo $data['translation']['text_end_session']; ?></a>
                                                                <?php endif; ?>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <hr class="my-2">
                                            <?php endforeach; ?>
                                        <?php else: ?>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
            </div>
        </main>
<?php include DIR_PUBLIC."panel/parts/footer.php"; ?>