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

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


        $stmt = $db->query("INSERT INTO {$db->prefix}admin_groups (`status`, `name`, `description`) VALUES (:status,:name, :description)", 
        array(':status'=>$status, ':name'=>$name, ':description'=>$description));
        
        if($stmt->error) {
            $session->data['messageType'] = "error"; 
            $session->data['messageTitle'] = "Hata!"; 
            $session->data['message'] = 'Bir hata oluştu ve isteğiniz yerine getirilemedi. Lütfen daha sonra tekrar deneyiniz.'; 
            header("Refresh: 0;");
        }else{
            $user_id = $db->getLastId();
            $session->data['messageType'] = "success"; 
            $session->data['messageTitle'] = "Başarılı!"; 
            $session->data['message'] = "Kullanıcı grubu başarıyla oluşturuldu."; 
            header("Location: /admin/admin-groups/edit?id={$user_id}");
            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 Ekle</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="" required>
                                        </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">Aktif</option>
                                                <option value="0">Pasif</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-md-3">
                                            <label for="description" class="form-label font-weight-bold">Açıklama</label>
                                        </div>
                                        <div class="col-md-4">
                                            <textarea name="description" id="description" rows="3" class="form-control"></textarea>
                                        </div>
                                    </div>
                                    <div class="row mb-3">
                                        <div class="col-12 text-end">
                                            <input type="submit" name="update" class="btn btn-primary" value="Kaydet">
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
<?php include DIR_VIEWS."parts/footer.php"; ?>