<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">

	<!-- Content Header (Page header) -->
	<section class="content-header">
		<div class="container-fluid">
			<div class="row mb-2">
				<div class="col-sm-6">
					<h1><?php echo $meta_title; ?></h1>
				</div>
				<div class="col-sm-6">
					<ol class="breadcrumb float-sm-right">
						<li class="breadcrumb-item"><a href="<?php echo base_url().$this->config->item('admin_folder').'/'.'dashboard'; ?>">Home</a></li>
						<li class="breadcrumb-item"><a href="<?php echo base_url().$this->config->item('admin_folder').'/'.'admin_data/index'; ?>">Admin Users</a></li>
						<li class="breadcrumb-item active"><?php echo $meta_title; ?></li>
					</ol>
				</div>
			</div>
		</div><!-- /.container-fluid -->
	</section>

	<section class="content">
		<div class="container-fluid">
			<div class="row">
				<div class="col-12">
					<div class="card">
						<div class="card-header">
							<h3 class="card-title"><?php echo $meta_title; ?></h3>
						</div>
						<!-- /.card-header -->

						<div class="card-body">
							<form name="form_submit" action="javascript:;" method="POST" id="form_submit" data-parsley-validate="">
								<input type="hidden" name="id" id="admin_id" value="<?php echo isset($admin_user->id) && !empty($admin_user->id) ? $admin_user->id : ''; ?>">


								<div class="form-group row">
									<label class="col-sm-2 col-form-label">Name <span class="required">*</span></label>
						            <div class="col-sm-10">
						            	<input type="text" name="name" class="form-control" required="" value="<?php echo isset($admin_user->name) && !empty($admin_user->name) ? $admin_user->name : ''; ?>">
						            </div>
						        </div>

						        <div class="form-group  row">
						        	<label class="col-sm-2 col-form-label">Email <span class="required">*</span></label>
						            <div class="col-sm-10">
						            	<input type="email" name="email" class="form-control" required="" value="<?php echo isset($admin_user->email) && !empty($admin_user->email) ? $admin_user->email : ''; ?>">
						            </div>
						        </div>

						        <div class="form-group row">
						        	<label class="col-sm-2 col-form-label">Password <?php if(!isset($admin_user->id)){ ?><span class="required">*</span><?php } ?></label>
						            <div class="col-sm-10">
						            	<input type="password" name="password" id="password" class="form-control" <?php if(!isset($admin_user->id)){ echo "required"; } ?> />
						            </div>
						        </div>


						        <div class="form-group row">
						        	<label class="col-sm-2 col-form-label">Role<span class="required">*</span></label>
						            <div class="col-sm-10">
						            	<select class="form-control" name="role_id" required>
						            		<option value="">-----Select-----</option>
						            		<?php

						            			foreach ($roles as $key => $value) {
						            				$selected = "";
						            				if($value->id == (int)$admin_user->role_id){
						            					$selected = "selected";
						            				}
						            		?>
						            				<option value="<?php echo $value->id; ?>" <?php echo $selected; ?>><?php echo $value->role_name; ?></option>
						            		<?php
						            			}
						            		?>
						            	</select>
						            </div>
						        </div>
							    

							    <div class="form-group row margin-top text-center">
						            <div class="col-12">
						            	<button class="btn btn-primary btn-square px-4" id="submit_btn" type="submit">Save</button>
						            </div>
						        </div>
							</form>
						</div>
						<!-- /.card-body -->
					</div>
					<!-- /.card -->
				</div>
			</div>
		</div>
	</section>

</div>
<!-- /.content-wrapper -->


<script type="text/javascript">
	$('#form_submit').parsley().on('field:validated', function() {
        var ok = $('.parsley-error').length === 0;
        $('.bs-callout-info').toggleClass('hidden', !ok);
        $('.bs-callout-warning').toggleClass('hidden', ok);
    })
    .on('form:submit', function() {
        var formData = new FormData($("#form_submit")[0]);
        var base_url = '<?php echo base_url().$this->config->item('admin_folder'); ?>';
        var id = $("#admin_id").val();
        var url = base_url+"/admin_data/edit/"+id;

        $.ajax({
            type: "POST",
            dataType: "json",
            url: url,
            data: formData,
            //async: false,
            global : false,
            cache: false,
            contentType: false,
            processData: false,
            beforeSend:function(){
            	$("#submit_btn").addClass("disabled");
            	$("#submit_btn").attr("disabled");
            	$("#submit_btn").html("Saving...");
            },
            success: function(result) {

            	$("#submit_btn").removeClass("disabled");
            	$("#submit_btn").removeAttr("disabled");
            	$("#submit_btn").html("Save");

            	if(result["status"] === false){
           			bootbox.alert("<p style='color:red'>"+result['errors']+"</p>", function() {});
            	}else{

                   	bootbox.alert(result['message'], function() {
                        location.href = result['redirect_url'];
                   	});

                }
            },
            error: function() {
            	$("#submit_btn").removeClass("disabled");
            	$("#submit_btn").removeAttr("disabled");
            	$("#submit_btn").html("Save");

		        bootbox.alert("<p style='color:red'>Failed to save the details</p>", function() {});
            }
        });
    });
</script>