<!-- 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">
					<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="module_id" value="<?php echo isset($modules->id) && !empty($modules->id) ? $modules->id : ''; ?>">


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

                                <datalist id="group_lists">
                                    <?php
                                        if(isset($groups) && !empty($groups)){
                                            foreach ($groups as $key => $value) {
                                    ?>
                                                <option value="<?php echo $value->module_group_name; ?>"></option>
                                    <?php
                                            }
                                        }
                                    ?>
                                </datalist>
                                
                                <div class="form-group  row"><label class="col-sm-2 col-form-label">Module name <span class="required">*</span></label>
                                    <div class="col-sm-10"><input type="text" name="module_name" class="form-control" required="" value="<?php echo isset($modules->module_name) && !empty($modules->module_name) ? $modules->module_name : ''; ?>" /></div>
                                </div>


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

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

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

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