<?php 
    $data = array();
    $data['title'] = "Kullanıcı 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'] = [];
    $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' => "Kullanıcılar",
        'href' => "/admin/admins/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Kullanıcı Düzenle",
        'href' => "#",
        'index' => 3
    );

    if(isset($_GET['id']) && !empty($_GET['id'])) {
        $stmt = $db->query("SELECT * FROM {$db->prefix}admin WHERE id=:id", array(':id'=>$_GET['id']));
        $user_details = $stmt->row;
    }else{
        header("Location: /admin/admins/list");
        exit();
    }

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $id             = $_GET['id'];
        $name           = $_POST['name'];
        $user_name      = $_POST['user_name'];
        $email_         = $_POST['email'];
        $status         = $_POST['status'];
        $user_group     = $_POST['user_group'];
        $password       = isset($_POST['password']) && !empty($_POST['password']) ? password_hash($_POST['password'], PASSWORD_DEFAULT) : $user_details["password"];
        $avatar         = $utils->strtolower_tr(mb_substr($name, 0, 1, 'UTF-8'));
        $avatar .= ".jpg";

        $stmt = $db->query("UPDATE {$db->prefix}admin SET `user_group`=:user_group, `name`=:name, `user_name`=:user_name, `email`=:email, password=:password, `status`=:status, avatar=:avatar WHERE `id`=:id", array(':name'=>$name, ':user_group'=>$user_group, ':user_name'=>$user_name, ':email'=>$email_, ':password'=>$password, ':status'=>$status, ':avatar'=>$avatar, ':id'=>$id));
        if($stmt->error){
            $utils->errorLog("error", "Cannot update admin 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 kullanıcı bilgileri güncellenemedi."; 
            header("Refresh: 0;");
            exit();
        }else{
            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Kullanıcı bilgileri başarıyla güncellendi."; 
            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">Kullanıcı Bilgileri</h5>
                            </div>
                            <div class="card-body">
                                <form action="" method="post">
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="name" class="form-label font-weight-bold">Adı</label>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="text" class="form-control" name="name" id="name" placeholder="adı" value="<?php echo $user_details["name"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="user_name" class="form-label font-weight-bold">Kullanıcı Adı</label>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="text" class="form-control" name="user_name" id="user_name" placeholder="Kullanıcı Adı" value="<?php echo $user_details["user_name"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="user_email" class="form-label font-weight-bold">Eposta</label>
                                        </div>
                                        <div class="col-md-4">
                                            <input type="email" class="form-control" name="email" id="user_email" placeholder="john@example.com" value="<?php echo $user_details["email"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="user_password" class="form-label font-weight-bold">Yeni Parola</label>
                                        </div>
                                        <div class="col-md-4 position-relative">
                                            <input type="password" class="form-control" name="password" id="user_password" placeholder="******" value="">
                                            <span class="password-visibility-toggler" data-password="user_password">
                                                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-eye" viewBox="0 0 16 16">
                                                    <path d="M16 8s-3-5.5-8-5.5S0 8 0 8s3 5.5 8 5.5S16 8 16 8zM1.173 8a13.133 13.133 0 0 1 1.66-2.043C4.12 4.668 5.88 3.5 8 3.5c2.12 0 3.879 1.168 5.168 2.457A13.133 13.133 0 0 1 14.828 8c-.058.087-.122.183-.195.288-.335.48-.83 1.12-1.465 1.755C11.879 11.332 10.119 12.5 8 12.5c-2.12 0-3.879-1.168-5.168-2.457A13.134 13.134 0 0 1 1.172 8z"/>
                                                    <path d="M8 5.5a2.5 2.5 0 1 0 0 5 2.5 2.5 0 0 0 0-5zM4.5 8a3.5 3.5 0 1 1 7 0 3.5 3.5 0 0 1-7 0z"/>
                                                </svg>
                                            </span>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="user_group" class="form-label font-weight-bold">Kullanıcı Grubu</label>
                                        </div>
                                        <div class="col-md-4">
                                            <select name="user_group" id="user_group" class="form-control form-select">
                                                <?php
                                                    $stmt = $db->query("SELECT * FROM {$db->prefix}admin_groups");
                                                    foreach($stmt->rows as $row):
                                                ?>
                                                    <option value="<?php echo $row['id']; ?>" <?php echo $user_details["user_group"] == $row['id'] ? 'selected' : ''; ?>><?php echo $row['name']; ?></option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="user_status" class="form-label font-weight-bold">Durum</label>
                                        </div>
                                        <div class="col-md-4">
                                            <select name="status" id="user_status" class="form-control form-select">
                                                <option value="1" <?php echo $user_details["status"] == "1" ? 'selected' : ''; ?>>Aktif</option>
                                                <option value="0" <?php echo $user_details["status"] == "0" ? 'selected' : ''; ?>>Pasif</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-12 text-end">
                                            <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"; ?>