<?php
include("lib/config.php");

$userID = isset($_GET["uid"])?$_GET["uid"]:'';

if(isset($userID) && $userID!='')
{
	$query_main = "SELECT * FROM ".TBL_USERS." WHERE user_id = '$userID' ";
	$msInfo = $con->getSingleRow($query_main);
}
?>

<div class="box-content">
  <form method="post" name="frmUsers" id="frmUsers">
    <input type="hidden" name="action" value="update">
    <input type="hidden" name="user_id" value="<?php echo(isset($userID))?$userID:'';?>">
    <input type="hidden" name="old_user_pic" value="<?=(isset($msInfo['user_pic']))?$msInfo['user_pic']:''?>">
    <div class="row">
      <div class="col col-lg-6">
        <div class="form-group">
          <label for="title">User Group</label>          
          <select class="form-control" name="group_id" id="group_id" style="width:350px;" required>
            <option value="">---Select Group---</option>
            <?php 
			$selQry_grps = "SELECT * FROM ".TBL_USERS_GROUPS." WHERE status='1' and id !='1' ORDER BY group_name";
			$grpsInfo =  $con->getMultiRow($selQry_grps, __FILE__,__LINE__);
			$totGrps = count($grpsInfo);	
				for($g=0;$g<$totGrps;$g++)
				{
			?>
		  		<option value="<?php echo $grpsInfo[$g]['id'];?>" <?php if(isset($msInfo['group_id']) && $msInfo['group_id']==$grpsInfo[$g]['id']){?> selected="selected"<?php }?>><?php echo $grpsInfo[$g]['group_name'];?></option>
		  <?php }?>
          </select>          
        </div>
      </div>
      <div class="col col-lg-6">
        <div class="form-group" id="uploadFile">
          <label for="title">Profile Pic</label>
          <input type="file" class="" name="user_pic" id="user_pic">
          <img src="uploads/users/<?=(isset($msInfo['user_pic']))?$msInfo['user_pic']:''?>" width="50" height="50" />
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col col-lg-6">
        <div class="form-group">
          <label for="title">First Name</label>
          <input type="text" class="form-control" name="first_name" id="first_name" value="<?=(isset($msInfo['first_name']))?$msInfo['first_name']:''?>" placeholder="" style="width:350px;" required>
        </div>
      </div>
      <div class="col col-lg-6">
        <div class="form-group">
          <label for="title">Last Name</label>
          <input type="text" class="form-control" name="last_name" id="last_name" value="<?=(isset($msInfo['last_name']))?$msInfo['last_name']:''?>" placeholder="" style="width:350px;">
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col col-lg-6">
        <div class="form-group">
          <label for="title">Userame</label>
          <input type="text" class="form-control" name="user_name" id="user_name" value="<?=(isset($msInfo['user_name']))?$msInfo['user_name']:''?>" placeholder="" style="width:350px;" disabled="disabled">
        </div>
      </div>
      <div class="col col-lg-6">
        <div class="form-group">
          <label for="title">Email</label>
          <input type="text" class="form-control" name="user_email" id="user_email" value="<?=(isset($msInfo['user_email']))?$msInfo['user_email']:''?>" placeholder="" style="width:350px;" required>
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col col-lg-6">
        <div class="form-group">
          <label for="title">Password</label>
          <input type="password" class="form-control" name="user_password" id="user_password" value="" placeholder="" style="width:350px;">
        </div>
      </div>
      <div class="col col-lg-6">
        <div class="control-group">
          <label class="control-label" for="selectError">Status</label>
          <div class="controls">
            <select name="status" id="status" data-rel="chosen" required>
              <option value="1" <?php if(isset($msInfo['status']) && $msInfo['status']==1){?> selected="selected" <?php }?>>Active</option>
          	  <option value="0"  <?php if(isset($msInfo['status']) && $msInfo['status']==0){?> selected="selected" <?php }?>>In Active</option>
            </select>
          </div>
        </div>
      </div>
    </div>
    <div class="row">
      <div class="col col-lg-12">
        <div style="margin-top:30px; text-align:right;">
          <button data-loading-text="Saving..." type="submit" class="btn btn-success"> <i class="fa fa-floppy-o"></i>Save</button>
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
  </form>
</div>

<script>
$(document).ready(function(e){
    
	$("#frmUsers").on('submit', function(e){
	  
        e.preventDefault();
		
		for (instance in CKEDITOR.instances) {
			CKEDITOR.instances[instance].updateElement();
		}
		
        $.ajax({
            type: 'POST',
            url: 'users-config.php',
            data: new FormData(this),
            //data: userData,
            contentType: false,
            cache: false,
            processData:false,
			dataType: 'JSON',
            beforeSend: function(){
                //$('.submitBtn').attr("disabled","disabled");
                //$('#fupForm').css("opacity",".5");
            },
            success: function(msg){
			   				
                //$('.statusMsg').html('');
				
                if(msg[0].message == 'ok'){
				
                    $('#frmUsers')[0].reset();
					
					//$('.statusMMsg').html('<span style="font-size:18px;color:#34A853">Product(s) added successfully.</span>');
					$('#myModal').modal('hide');
					
					getUsers();
					
					Lobibox.notify('success', {
						title: 'Success',
						msg: 'Record updated successfully.'
					});
					
                }
				else
				{   
					//$('.statusMMsg').html('<span style="font-size:18px;color:#EA4335">Some problem occurred, please try again.</span>');
					$('#myModal').modal('hide');
					
					Lobibox.notify('error', {
						title: 'Error',
						msg: 'Some problem occurred, please try again.'
					});
                }
            }
        });
    });
	
});
</script>