<!-- 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_modules/index'; ?>">Modules</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">
					<form name="form_submit" action="javascript:;" method="POST" id="form_submit" data-parsley-validate="">
                        <input type="hidden" name="id" id="role_id" value="<?php echo isset($roles->id) && !empty($roles->id) ? $roles->id : ''; ?>">

                        <div class="row">
                            <div class="col-md-12">
                                <div class="card">
                                    <div class="card-header">
                                        <h3 class="card-title">Basic details</h3>
                                    </div>
                                    <div class="card-body">
                                        <div class="form-group row">
                                            <label class="col-sm-2 col-form-label">Role Name <span class="required">*</span></label>
                                            <div class="col-sm-10">
                                                <input type="text" name="role_name" class="form-control" required="" value="<?php echo isset($roles->role_name) && !empty($roles->role_name) ? $roles->role_name : ''; ?>">
                                            </div>
                                        </div>

                                        <div class="form-group row">
                                            <label class="col-sm-2 col-form-label">Status<span class="required">*</span></label>
                                            <div class="col-sm-10">
                                                <select class="form-control" name="is_active" required>
                                                    <option value="" <?php echo $roles->is_active == '' ? "selected" : ""; ?>>-----Select-----</option>
                                                    <option value="1" <?php echo $roles->is_active == "1" ? "selected" : ""; ?>>Active</option>
                                                    <option value="0" <?php echo $roles->is_active == "0" ? "selected" : ""; ?>>In-active</option>
                                                </select>
                                            </div>
                                        </div>

                                    </div>
                                </div>
                            </div>
                            <?php
                                foreach ($modules as $key => $value) {
                                    $disabled = "";
                                    if(isset($roles->id) && $roles->id == 1){
                                        $disabled = "";
                                    }
                            ?>
                                    <div class="col-md-4">
                                        <div class="card">
                                            <div class="card-header">
                                                <h3 class="card-title"><?php echo $value->module_name; ?></h3>
                                            </div>
                                            <div class="card-body">
                                                <div class="row">
                                                    <div class="col-md-6">
                                                        <div class="checkbox-fade fade-in-warning">
                                                            <label>
                                                                <input type="checkbox" <?php echo $disabled; ?> name="list<?php echo $value->id; ?>" <?php echo isset($value->role_rights->view_right) && $value->role_rights->view_right == 1 ? 'checked' : ''; ?> />
                                                                <span class="cr"><i class="cr-icon icofont icofont-ui-check txt-warning"></i></span>
                                                                <span class="text-inverse">List</span>
                                                            </label>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-6">
                                                        <div class="checkbox-fade fade-in-success">
                                                            <label>
                                                                <input type="checkbox" <?php echo $disabled; ?> name="create<?php echo $value->id; ?>" <?php echo isset($value->role_rights->create_right) && $value->role_rights->create_right == 1 ? 'checked' : ''; ?> />
                                                                <span class="cr"><i class="cr-icon icofont icofont-ui-check txt-success"></i></span>
                                                                <span class="text-inverse">Create</span>
                                                            </label>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-6">
                                                        <div class="checkbox-fade fade-in-primary">
                                                            <label>
                                                                <input type="checkbox" <?php echo $disabled; ?> name="edit<?php echo $value->id; ?>" <?php echo isset($value->role_rights->edit_right) && $value->role_rights->edit_right == 1 ? 'checked' : ''; ?> />
                                                                <span class="cr"><i class="cr-icon icofont icofont-ui-check txt-primary"></i></span>
                                                                <span class="text-inverse">Edit</span>
                                                            </label>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-6">
                                                        <div class="checkbox-fade fade-in-danger">
                                                            <label>
                                                                <input type="checkbox" <?php echo $disabled; ?> name="delete<?php echo $value->id; ?>" <?php echo isset($value->role_rights->delete_right) && $value->role_rights->delete_right == 1 ? 'checked' : ''; ?> />
                                                                <span class="cr"><i class="cr-icon icofont icofont-ui-check txt-danger"></i></span>
                                                                <span class="text-inverse">Delete</span>
                                                            </label>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                            <?php
                                }
                            ?>
                        </div>

                        <div class="form-group row margin-top text-center">
                            <div class="col-12">
                                <button class="btn btn-primary" type="submit">Save</button>
                            </div>
                        </div>
                    </form>
					<!-- /.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 = $("#role_id").val();
        var url = base_url+"/roles/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>