<?php 
    if(!isset($_GET["id"]) || empty($_GET["id"])){
        header("Location: /admin/user-groups/list");
        exit();
    }

    $data = array();
    $data['title'] = "Kullanıcı Grubu 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' => "Kullanıcı Grupları",
        'href' => "/admin/user-groups/list",
        'index' => 2
    );
    $data['breadcrumbs'][] = array(
        'text' => "Kullanıcı Grubu Güncelle",
        'href' => "#",
        'index' => 3
    );

    if($_SERVER['REQUEST_METHOD'] == 'POST'){
        $id         = $_GET['id'];
        $title      = $_POST['title'];
        $identifier = $_POST['identifier'];
        $status     = $_POST['status'];

        $stmt = $db->query("UPDATE {$db->prefix}user_groups SET `title`=:title, `status`=:status, `identifier`=:identifier WHERE `id`=:id",array(':title'=>$title, ':status'=>$status, ':identifier'=>$identifier, ':id'=>$id));
        if($stmt->error){
            $utils->errorLog("error", "Cannot update user group. 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ı grubu güncellenemedi."; 
            header("Refresh: 0;");
            exit();
        }else{
            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Kullanıcı grubu başarıyla güncellendi."; 
            header("Refresh: 0;");
            exit();
        }
	}
    
    $user_group 	    = $user->getUserGroupDetails($_GET['id']);
    if(empty($user_group)) {
        header("Location: /admin/user-groups/list");
        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ı Grubu Bilgileri</h5>
                            </div>
                            <div class="card-body">
                                <form action="" method="post">
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="title" class="form-label font-weight-bold">Görünen Ad</label>
                                        </div>
                                        <div class="col-md-9">
                                            <input type="text" class="form-control" name="title" id="title" placeholder="adı" value="<?php echo $user_group["title"]; ?>">
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="identifier" class="form-label font-weight-bold">Ek Alan</label>
                                        </div>
                                        <div class="col-md-9">
                                            <select name="identifier" id="identifier" class="form-control form-select">
                                                <option value="" <?php echo $user_group["identifier"] == "" ? 'selected' : ''; ?>>Yok</option>
                                                <option value="institution" <?php echo $user_group["identifier"] == "institution" ? 'selected' : ''; ?>>Kurum Adı</option>
                                                <option value="gln" <?php echo $user_group["identifier"] == "gln" ? 'selected' : ''; ?>>GLN Numarası</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="status" class="form-label font-weight-bold">Durum</label>
                                        </div>
                                        <div class="col-md-9">
                                            <select name="status" id="status" class="form-control form-select">
                                                <option value="1" <?php echo $user_group["status"] == "1" ? 'selected' : ''; ?>>Aktif</option>
                                                <option value="0" <?php echo $user_group["status"] == "0" ? 'selected' : ''; ?>>Pasif</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-12 text-end">
                                            <a href="/admin/user-groups/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"; ?>