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

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        if(isset($_POST['action']) && !empty($_POST['action'])) {
            switch($_POST['action']) {
                case 'password-change':
                    $id         = $_GET['user_id'];
                    $password   = $_POST['password'];
                    $hashed_password = password_hash($password, PASSWORD_DEFAULT);

                    $stmt = $db->query("UPDATE {$db->prefix}users SET `password`=:password WHERE `id`=:id",array(':password'=>$hashed_password, ':id'=>$id));
                    if($stmt->error){
                        $utils->errorLog("error", "Cannot update user password. 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 parola güncellenemedi."; 
                        header("Refresh: 1;");
                        exit();
                    }else{
                        $session->data['messageType'] = "success"; 
                        $session->data['messageTitle'] = "Başarılı!"; 
                        $session->data['message'] = "Kullanıcı parolası başarıyla güncellendi. Yeni parola: <b>".$password."</b>"; 
                        header("Refresh: 1;");
                        exit();
                    }
                break;
                case 'user-details':
                    $id             = $_GET['user_id'];
                    $name           = $_POST['firstname'];
                    $surname        = $_POST['lastname'];
                    $email_         = $_POST['email'];
                    $phone          = $_POST['phone'];
                    $status         = $_POST['status'];
                    $is_admin       = isset($_POST['is_admin']) ? 1 : 0;

                    $stmt = $db->query("UPDATE {$db->prefix}users SET `firstname`=:name, `lastname`=:surname, `email`=:email, `phone`=:phone, `status`=:status, is_admin=:is_admin WHERE `id`=:id", array(':name'=>$name, ':surname'=>$surname, ':email'=>$email_, ':phone'=>$phone, ':status'=>$status, ':is_admin'=>$is_admin, ':id'=>$id));
                    if($stmt->error){
                        $utils->errorLog("error", "Cannot update user 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: 1;");
                        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: 1;");
                        exit();
                    }
                break;
            }
            
        }
	}
    
    $user_details       = $user->getUser($_GET['user_id']);

    $user_addresses     = $user->getAdresses(array('user_id' => $_GET['user_id'], 'default' => 0));
    $user_orders 	    = $order->getOrders(array('user_id'=>$_GET['user_id']));
?>
<?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="#accountTab" aria-selected="true">Kullanıcı Bilgileri</a>
                                <a class="list-group-item list-group-item-action" data-bs-toggle="tab" href="#otherTab" aria-selected="false">Diğer Bilgiler</a>
                                <a class="list-group-item list-group-item-action" data-bs-toggle="tab" href="#passwordTab" aria-selected="false">Parola Değiştir</a>
                                <a class="list-group-item list-group-item-action" data-bs-toggle="tab" href="#addressTab" aria-selected="false">Kayıtlı Adresler</a>
                                <a class="list-group-item list-group-item-action" data-bs-toggle="tab" href="#ordersTab" aria-selected="false">Siparişler</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ı Bilgileri</h5>
                                    </div>
                                    <div class="card-body">
                                        <form action="" method="post">
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="firstname" class="form-label font-weight-bold">Ad</label>
                                                </div>
                                                <div class="col-md-9">
                                                    <input type="text" class="form-control" name="firstname" id="firstname" placeholder="adı" value="<?php echo $user_details["firstname"]; ?>">
                                                </div>
                                            </div>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="lastname" class="form-label font-weight-bold">Soyad</label>
                                                </div>
                                                <div class="col-md-9">
                                                    <input type="text" class="form-control" name="lastname" id="lastname" placeholder="soyadı" value="<?php echo $user_details["lastname"]; ?>">
                                                </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-9">
                                                    <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_phone" class="form-label font-weight-bold">Telefon</label>
                                                </div>
                                                <div class="col-md-9">
                                                    <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-md-3">
                                                    <label for="user_status" class="form-label font-weight-bold">Üyelik Durumu</label>
                                                </div>
                                                <div class="col-md-9">
                                                    <select name="status" id="user_status" class="form-control form-select">
                                                        <option value="" <?php echo $user_details["status"] == '' ? 'selected' : ''; ?>>Seçiniz</option>
                                                        <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>
                                            <hr>
                                            <div class="row mb-3">
                                                <div class="col-md-3">
                                                    <label for="is_admin" class="form-label font-weight-bold">Admin kullanıcı?</label>
                                                </div>
                                                <div class="col-md-9 position-relative">
                                                    <input type="checkbox" class="form-label" name="is_admin" id="is_admin" <?php echo $user_details["is_admin"]==1 ? 'checked' : ''; ?>>
                                                </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>
                                            <input type="hidden" name="action" value="user-details">
                                        </form>
                                    </div>
                                </div>
                            </div>

                            <div class="tab-pane fade" id="otherTab" role="tabpanel">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title">Diğer Bilgiler</h5>
                                    </div>
                                    <div class="card-body">
                                        <div class="row mb-3">
                                            <div class="col-md-3">
                                                <label for="date_added" class="form-label font-weight-bold">Kayıt Tarihi</label>
                                            </div>
                                            <div class="col-md-9 position-relative">
                                                <input type="datetime-local" class="form-control" id="date_added" value="<?php echo date('Y-m-d\TG:i', strtotime($user_details["date_added"])); ?>" disabled>
                                            </div>
                                        </div>
                                        <div class="row mb-3">
                                            <div class="col-md-3">
                                                <label for="last_login" class="form-label font-weight-bold">Son Giriş</label>
                                            </div>
                                            <div class="col-md-9 position-relative">
                                                <input type="datetime-local" class="form-control" id="last_login" value="<?php echo date('Y-m-d\TG:i', strtotime($user_details["last_login"])); ?>" disabled>
                                            </div>
                                        </div>
                                        <div class="row mb-3">
                                            <div class="col-md-3">
                                                <label for="user_agreement" class="form-label font-weight-bold">Kullanıcı Sözleşmesi</label>
                                            </div>
                                            <div class="col-md-9 position-relative">
                                                <input type="checkbox" class="form-label" id="user_agreement" <?php echo $user_details["user_agreement"] == 1 ? 'checked' : ''; ?> disabled> <?php echo $user_details["user_agreement"] == 1 ? 'Onaylı' : 'Onaysız'; ?>
                                            </div>
                                        </div>
                                        <div class="row mb-3">
                                            <div class="col-md-3">
                                                <label for="kvkk_approval" class="form-label font-weight-bold">KVKK Onayı</label>
                                            </div>
                                            <div class="col-md-9 position-relative">
                                                <input type="checkbox" class="form-label" id="kvkk_approval" <?php echo $user_details["kvkk_approval"] == 1 ? 'checked' : ''; ?> disabled> <?php echo $user_details["kvkk_approval"] == 1 ? 'Onaylı' : 'Onaysız'; ?>
                                            </div>
                                        </div>
                                        <div class="row mb-3">
                                            <div class="col-md-3">
                                                <label for="promotion_approval" class="form-label font-weight-bold">Bülten Onayı</label>
                                            </div>
                                            <div class="col-md-9 position-relative">
                                                <input type="checkbox" class="form-label" id="promotion_approval" <?php echo $user_details["promotion_approval"] == 1 ? 'checked' : ''; ?> disabled> <?php echo $user_details["promotion_approval"] == 1 ? 'Onaylı' : 'Onaysız'; ?>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="tab-pane fade" id="passwordTab" role="tabpanel">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title"><i class="bi bi-person-check"></i> Parola Değiştir</h5>
                                    </div>
                                    <div class="card-body">
                                        <form action="" method="post">
                                            <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-9 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-12 text-end">
                                                    <input type="submit" name="update" class="btn btn-primary" value="Güncelle">
                                                </div>
                                            </div>
                                            <input type="hidden" name="action" value="password-change">
                                        </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">Kayıtlı Adresler</h5>
                                    </div>
                                    <div class="card-body p-0">
                                        <div class="table-responsive">
                                            <table class="table table-striped-table-hover table-condensed mb-0">
                                                <thead>
                                                    <tr>
                                                        <th>Başlık</th>
                                                        <th>Alıcı Adı</th>
                                                        <th>Telefon</th>
                                                        <th>Adres</th>
                                                        <th></th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php if(!empty($user_addresses)): ?>
                                                        <?php foreach($user_addresses as $row): ?>
                                                            <tr>
                                                                <td><?php echo $row['title']; ?></td>
                                                                <td><?php echo $row['firstname']." ".$row['lastname']; ?></td>
                                                                <td><?php echo $row['phone']; ?></td>
                                                                <td><?php echo $row['address']." ".$row['town_name']."/".$row['city_name']; ?></td>
                                                            </tr>
                                                        <?php endforeach; ?>
                                                    <?php else: ?>
                                                        <tr>
                                                            <td colspan="5" class="text-center">Görüntülenecek kayıt bulunamadı!</td>
                                                        </tr>
                                                    <?php endif; ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="tab-pane fade" id="ordersTab" role="tabpanel">
                                <div class="card">
                                    <div class="card-header">
                                        <h5 class="card-title">Siparişler</h5>
                                    </div>
                                    <div class="card-body p-0">
                                        <div class="row">
                                            <div class="col-12">
                                                <div class="table-responsive">
                                                    <table class="table table-striped table-hover table-bordered">
                                                        <thead>
                                                            <tr>
                                                                <th>Durum</th>
                                                                <th>Alıcı Adı</th>
                                                                <th>Tutar</th>
                                                                <th>Sipariş Tarihi</th>
                                                                <th></th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                            <?php if(!empty($user_orders)): ?>
                                                                <?php foreach($user_orders as $row): ?>
                                                                    <tr>
                                                                        <td><?php echo $row['status_text']; ?></td>
                                                                        <td><?php echo $row['name']." ".$row['surname']; ?></td>
                                                                        <td><?php echo $utils->moneyFormatter($row['order_total']); ?> ₺</td>
                                                                        <td><?php echo date('d-m-Y G:i', strtotime($row['date_added'])); ?></td>
                                                                        <td>
                                                                            <a class="btn btn-sm btn-primary" href="/admin/orders/edit?id=<?php echo $row['id']; ?>" target="_blank"><i class="bi bi-pencil-square"></i></a>
                                                                        </td>
                                                                    </tr>
                                                                <?php endforeach; ?>
                                                            <?php else: ?>
                                                                <tr>
                                                                    <td colspan="4" class="text-center">Görüntülenecek kayıt bulunamadı!</td>
                                                                </tr>
                                                            <?php endif; ?>
                                                        </tbody>
                                                    </table>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
<?php include DIR_VIEWS."parts/footer.php"; ?>