<?php

$id = HelperAdmin::getPostGetVar("id");

$name = "";
$username = "";
$email = "";
$role = "";

$isNew = true;
$editRole = true;

if(!empty($id)){
	$isNew = false;
	$user = $this->getUser($id);
	
	$name = $user["name"];
	$username = $user["username"];
	$email = $user["email"];
	$role = $user["role"];
	
	$cuser = $this->isLogedin();
	
	if($this->isUserAdmin() == false)
		$editRole = false;
	
	if($cuser["id"] == $id)
		$editRole = false;
	
}else{		//add new user failed
	
	if(!empty($_POST)){
		
		$name = HelperAdmin::getPostVar("name");
		$username = HelperAdmin::getPostVar("username");
		$email = HelperAdmin::getPostVar("email");
		$role = HelperAdmin::getPostVar("role");
	}
	
}

	
	
$message = "";
	
if ($message = @$_SESSION["message"]) {
    
	if ($message["type"] == "error") {
        $mclass = "alert-danger";
        $mtitle = "Error!!";
    } else {
        $mclass = "alert-success";
        $mtitle = "Success!!";
    }
    ?>
    <div class="alert alert-block <?php echo $mclass ?> fade in">
        <button type="button" class="close close-sm" data-dismiss="alert">
            <i class="fa fa-times"></i>
        </button>
        <h4><?php echo $mtitle ?></h4>
        <?php
        echo $message["message"];
        unset($_SESSION["message"])
        ?>
    </div>
<?php }?>

<div class="col-sm-12 users">
    <section class="panel">
        <header class="panel-heading">
            <?php if($isNew == false) echo "Edit";
            else echo "Add New" ?> User
        </header>
        <div class="panel-body" style="display: block;">
            <div class="form">
                <form class="cmxform form-horizontal " id="signupForm" method="post" action="" autocomplete="off" <?php if($isNew): ?> data-isnew="true" <?php endif?>>
                    <div class="form-group ">
                        <label for="name" class="control-label col-lg-3">Name</label>
                        <div class="col-lg-6">
                            <input name="id" type="hidden" value="<?php echo $id ?>" >
                            <input class=" form-control" id="firstname" name="name" type="text" value="<?php echo $name ?>">
                        </div>
                    </div>                    
                    <div class="form-group ">
                        <label for="username" class="control-label col-lg-3">Username</label>
                        <div class="col-lg-6">
                            <input class="form-control <?php if($isNew):?>transparent-color<?php endif?>" id="username" name="username" type="text" value="<?php echo $username ?>">
                            <span class="setting_required">*</span>
                        </div>
                    </div>
                    <div class="form-group ">
                        <label for="password" class="control-label col-lg-3">Password</label>
                        <div class="col-lg-6">
                            <input class="form-control transparent-color" autocomplete="off" id="password" name="password"  type="password">
                            <?php if($isNew):?>
                            	<span class="setting_required">*</span>
                            <?php endif?>
                         </div>
                    </div>
                    <div class="form-group ">
                        <label for="confirm_password" class="control-label col-lg-3">Confirm Password</label>
                        <div class="col-lg-6">
                            <input class="form-control " id="confirm_password" name="confirm_password" id="cpwd" type="password">
                            <?php if($isNew):?>
                            	<span class="setting_required">*</span>
                            <?php endif?>
                       </div>
                    </div>
                    <div class="form-group ">
                        <label for="email" class="control-label col-lg-3">Email</label>
                        <div class="col-lg-6">
                            <input class="form-control " id="email" name="email" type="email"  value="<?php echo $email ?>">
                            <span class="setting_required">*</span>
                        </div>
                    </div>
                    <div class="form-group ">
                        <label for="group" class="control-label col-lg-3 col-sm-3">Role</label>
                        <div class="col-lg-6">
                        
                            <?php if ($editRole == true): ?>
                                
                                <select name="role" class="form-control m-bot15">
                                        <option <?php if($role != "editor") echo 'selected'?> value="admin">Admin</option>
                                        <option <?php if($role == "editor") echo 'selected'?> value="editor">Editor</option>
                                </select>
                            
                            <?php else:?>   
                             
                            <?php echo $role?>
                        	
                        	<?php endif?>
                        	
                        </div>
                    </div>


                    <div class="form-group">
                        <div class="col-lg-offset-3 col-lg-6">
                            <?php $back = @$_SESSION["page"] ? '&page=' . ($_SESSION["page"] + 1) : ''; ?>
                            <button class="btn btn-success" type="submit">Save</button>
                            <a class="btn btn-default" href="<?php echo HelperAdmin::getUrlView("users") ?>">Cancel</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </section>
</div>


<script type="text/javascript" src="./template/js/jquery.validate.min.js"></script>

<script type="text/javascript">

jQuery(document).ready(function() {
	initEditUser();
});
            
            </script>