<!-- load css dan js -->
<link type="text/css" rel="stylesheet" href="<?php echo ASSETS_URL;?>plugins/datatables/dataTables.bootstrap.css" />
<script src="<?php echo ASSETS_URL;?>plugins/datatables/jquery.dataTables.min.js"></script>
<script src="<?php echo ASSETS_URL;?>plugins/datatables/dataTables.bootstrap.min.js"></script>
<script src="<?php echo ASSETS_URL;?>plugins/datatables/fnPagingInfo.js"></script>
<script src="<?php echo ASSETS_URL;?>plugins/datatables/fnReloadAjax.js"></script>
<script src="<?php echo ASSETS_URL;?>plugins/form/jquery.validate.js"></script>
<link rel="stylesheet" href="<?php echo ASSETS_URL;?>plugins/select2/select2.min.css" rel="stylesheet" type="text/css"/>
<script src="<?php echo ASSETS_URL;?>plugins/select2/select2.min.js"></script>

<!-- end load css dan js -->

<section class="content-header">
	<h1>
		<?php echo $app_title;?>
		<small><?php echo $app_desc;?></small>
	</h1>
</section>

<section class="content">
	<div class="box box-info"> <!-- start .box -->
		<div class="box-header"> <!-- start .box-header -->
			<div class="row">
				<div class="col-md-6">
					<?php if($this->rolemodule->check_button('add')) { ?>			
						<a class="btn btn-sm btn-primary" id="addUser"><i class="fa fa-plus"></i> Tambah</a>&nbsp;&nbsp;
					<?php } ?>					
				</div>
				<div class="col-md-6" align="right">
					<?php
						$choose = array(1=> 'Active',0=>'Non Active');
						echo form_dropdown('f_user_active',$choose,1,'id="f_user_active" class="select" style="width:25%"');
					?>
				</div>
			</div>
		</div> <!-- end .box-header -->
		
		<div class="box-body"> <!-- start .box-body -->
			<table id="tableUser" class="table datares">
				<thead>
					<tr>				
						<th>No</th>				
						<th>Email</th>				
						<th>Name</th>				
						<th>Level</th>				
						<th>Role</th>				
						<th>Action</th>
					</tr>
				</thead>
			</table>
		</div> <!-- end .box-body -->
		<div class="box-footer" align="right"> <!-- start box-footer -->
		  <p>Page rendered in <strong>{elapsed_time}</strong> seconds</p>
		</div> <!-- end .box-footer-->
	</div> <!-- end .box -->
</section><!-- /.content -->

<script>
var oTable;
var $f_user_active = $('#f_user_active'); 
$(document).ready(function(){
	oTable = $('#tableUser').dataTable({
		"processing": true,
		"serverSide": true,
		"sAjaxSource": "<?php echo site_url('user/fnUserDataJson');?>?active="+$('#f_user_active').val(),
		"sServerMethod": "POST",
		"aoColumns": [ 
			null,null,null,{'mRender': fnLevel},null,{ 'bSortable': false,'sClass':'center' }
		],
		"fnRowCallback": function (nRow, aData, iDisplayIndex, DisplayIndexFull) {
		  var page		= this.fnPagingInfo().iPage;
		  var lengt		= this.fnPagingInfo().iLength;
		  var index 	= (page * lengt + (iDisplayIndex +1));
		  $('td:eq(0)', nRow).html(index);
		}
	});
	$(".dataTables_filter input").attr("placeholder","Cari Kata disini ...");

	$('#addUser').click(function()	{
		window.location.href = "<?php echo site_url('user/fnUserAdd');?>";

	});

	var $f_user_active = $('#f_user_active'); 
    $f_user_active.select2()
    .change(function(){
		oTable.fnReloadAjax('<?php echo site_url('user/fnUserDataJson');?>?active='+$(this).val());
	});;

<?php if($this->session->userdata('SUCMSG'))
{	
	echo "infoDialog.realize();";
	echo "infoDialog.setTitle('Info').setMessage('<center>".$this->session->userdata('SUCMSG')."</center>').open();";
	$this->session->unset_userdata('SUCMSG');
}
?>
});

function fnLevel(x)
{
	if(x == 1)
		return 'Super Admin';
	else if(x == 2)
		return 'Admin';
	else
		return 'User';
}

function fnEdit(idRow)
{
	window.location.href = "<?php echo site_url('user/fnUserEdit');?>/"+idRow;
}

function fnDelete(idRow)
{
	BootstrapDialog.confirm('Yakin ingin dihapus ?', function(result){
		var urlUpdate='<?php echo site_url('user/fnUserDelete');?>'
		if(result)
		{
			$.post(urlUpdate,{id : idRow,<?php echo $this->security->get_csrf_token_name();?>:'<?php echo $this->security->get_csrf_hash(); ?>'},function(html){
				if(html== 'TRUE') { oTable.fnReloadAjax();} 
				else
				{
					infoDialog.realize();
					infoDialog.setTitle('Info').setMessage(html).setType(BootstrapDialog.TYPE_DANGER).open();
				} 
			});
		}	
	}).setType(BootstrapDialog.TYPE_DANGER);
}

</script>



