<div id="content">                        
<div class="wrap">
<script type="text/javascript">
$(document).ready(function() {
	$('select').select2();
	$.each($(".select2-container"), function (i, n) {
            $(n).next().show().fadeTo(0, 0).height("0px").css("left", "auto"); // make the original select visible for validation engine and hidden for us
            $(n).prepend($(n).next());
            $(n).delay(500).queue(function () {
                $(this).removeClass("validate[required]"); //remove the class name from select2 container(div), so that validation engine dose not validate it
                $(this).dequeue();
            });
        });	
    var oTable = $("#test").dataTable( {
		"iDisplayLength": 10,
		 "aLengthMenu": [5,10,25,50,100],
		 "bPaginate": false, 
		 "bFilter": false, 
		 "aoColumns": [ { 
		 	"bSortable": false }, 
			null, 
			null, 
			null, 
			null, 
			null, 
			null ]});
	$("#Submit").attr("disabled", "disabled");
    $("#test input.alls[type=checkbox]").each(function () {
        $(this).change(function () {
             var $tr =  $(this).closest('tr');
             $tr.find('.Submit').removeAttr("disabled");
            if ($(this).is(":checked")) {
                $tr.find('.transaction').attr('checked', true);
                $tr.find('.reports').attr('checked', true);
                $tr.find('.set_up').attr('checked', true);
            } else {
                $tr.find('.transaction').attr('checked', false);
                $tr.find('.reports').attr('checked', false);
                $tr.find('.set_up').attr('checked', false);
            }
        });
    });
    $("#test input.transaction[type=checkbox]").each(function () {
        $(this).change(function () {
             var $tr =  $(this).closest('tr');
             $tr.find('.Submit').removeAttr("disabled");
            if ($(this).is(":checked")) {
            } else {
                $tr.find('.alls').attr('checked', false);
            }
        });
    });
    $("#test input.reports[type=checkbox]").each(function () {
        $(this).change(function () {
             var $tr =  $(this).closest('tr');
             $tr.find('.Submit').removeAttr("disabled");
            if ($(this).is(":checked")) {
            } else {
                $tr.find('.alls').attr('checked', false);
            }
        });
    });
    $("#test input.set_up[type=checkbox]").each(function () {
        $(this).change(function () {
             var $tr =  $(this).closest('tr');
             $tr.find('.Submit').removeAttr("disabled");
            if ($(this).is(":checked")) {
            } else {
                $tr.find('.alls').attr('checked', false);
            }
        });
    });
    $("table.editable").on("click",".Submit",function(){
        rRow = $(this).parents("tr");
        fldid = $('td:first', $(this).parents('tr')).text(); 
		if($(rRow.find('input[id="transaction"]')).is(':checked')){ transaction = 1 ;}else{ transaction = 0;};
		if($(rRow.find('input[id="reports"]')).is(':checked')){ reports = 1 ;}else{ reports = 0;};
		if($(rRow.find('input[id="set_up"]')).is(':checked')){ set_up = 1 ;}else{ set_up = 0;};
        $("#row_submit").dialog("open");
    });
    function row_submit(row){
		var postData = {
			'fldid' : fldid,
			'transaction' : transaction,
			'reports' : reports,
			'setup' : set_up,
		};
 		$.ajax({
  			type: "POST",
  			dataType: "json",
			url: "../useraccess_update",
			data: postData ,
  			success: function(content){
				alert(content.message);
setTimeout(
                  function() 
                  {
                     location.reload();
                  }, 0001);			},
		});        // Here your ajax action after delete confirmed
//		location.reload();
    }
    $("#row_submit").dialog({
        autoOpen: false,
        resizable: false,        
        modal: true,
        buttons: {
            "Continue...": function() {
                row_submit(rRow);
                $(this).dialog("close");
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        }
    });    
}); 

function accessRight(obj){
	var companyID = obj.value;
	var tablerows1 = $("#test > tbody >tr").length;
	var rows1 = $("#test").dataTable().fnGetNodes();
	var fldid = $("#userID").val();
	var postData = {
		companyID : companyID,
		fldid : fldid
		};
		$.ajax({
			type: "POST",
        	url: '<?php echo base_url();?>admin/userAccessSearch',
			dataType:"json",
			data: postData,
			success: function(content){
				if(content.status == "success") {
					for (var i = 0; i < tablerows1 ; i++){
						var module = $(rows1[i]).find("td:first").text();
						for ( var ii = 0; ii < content.message.length; ii++) {
							if(content.message[i].moduleID = module){
								if(content.message[i].transaction == 1){
									$(rows1[i]).find("td:eq(4) input:checkbox").attr("checked","checked");
								}else{
									$(rows1[i]).find("td:eq(4) input:checkbox").removeAttr("checked");
								}
								if(content.message[i].reports == 1){
									$(rows1[i]).find("td:eq(5) input:checkbox").attr("checked","checked");
								}else{
									$(rows1[i]).find("td:eq(5) input:checkbox").removeAttr("checked");
								}
								if(content.message[i].setup == 1){
									$(rows1[i]).find("td:eq(6) input:checkbox").attr("checked","checked");
								}else{
									$(rows1[i]).find("td:eq(6) input:checkbox").removeAttr("checked");
								}
								if(content.message[i].transaction == 1 && content.message[i].reports == 1 && content.message[i].setup == 1){
									$(rows1[i]).find("td:eq(3) input:checkbox").attr("checked","checked");
								}
							}
						}
					}
				}else{
					for (var i = 0; i < tablerows1 ; i++){
									$(rows1[i]).find("td:eq(3) input:checkbox").removeAttr("checked");
									$(rows1[i]).find("td:eq(4) input:checkbox").removeAttr("checked");
									$(rows1[i]).find("td:eq(5) input:checkbox").removeAttr("checked");
									$(rows1[i]).find("td:eq(6) input:checkbox").removeAttr("checked");
					}
				}
			}
		});
//    var rRow = $(this).parents("tr");
//	if(tablerows1 > 0){
//		var rows1 = $("#test").dataTable().fnGetNodes();
//		for (var i = 0; i < tablerows1 ; i++){
//			var module = $(rows1[i]).find("td:first").text();
//			var transaction = $(rows1[i]).find("td:eq(3)").is(':checked')?1:0;
//			alert(fldid);
//		}
//	}
}
function updateAccess(){
	var fldid = $("#userID").val();
	var companyID = $("#companyID option:selected").val();
	var tablerows1 = $("#test > tbody >tr").length;
	var rows1 = $("#test").dataTable().fnGetNodes();
	for (var i = 0; i < tablerows1 ; i++){
		var module = $(rows1[i]).find("td:first").text();
		if($(rows1[i]).find("td:eq(4) input:checkbox").is(":checked")){
			var transaction = 1;
		}else{
			var transaction = 0;
		}
		if($(rows1[i]).find("td:eq(5) input:checkbox").is(":checked")){
			var reports = 1;
		}else{
			var reports = 0;
		}
		if($(rows1[i]).find("td:eq(6) input:checkbox").is(":checked")){
			var setup = 1;
		}else{
			var setup = 0;
		}
		var postData = {
			companyID : companyID,
			fldid : fldid,
			module : module,
			transaction : transaction,
			reports : reports,
			setup : setup,
			};
 			$.ajax({
  				type: "POST",
	  			dataType: "json",
				url: "<?php echo base_url()?>admin/updateAccess",
				data: postData ,
	  			async: false,
  				success: function(content){
				}
			}); 
	}
	alert("<?php echo $this->lang->line('message1')?>");
}
function updateAccess1(){
   var oTable = $("#test").dataTable( );
	$(oTable.fnGetNodes()).each(function () {
        if($(this).is(':checked')){
			var fldid = $('td:first', $(this).parents('tr')).text(); 
			var effdate = $('td:eq(6)', $(this).parents('tr')).text(); 
			var postData = {
				'fldid' : fldid,
				'effdate' : effdate,
			};
			
//			alert(effdate);
 			$.ajax({
  				type: "POST",
	  			dataType: "json",
				url: "<?php echo base_url()?>Aptransaction/debitNotePosting",
				data: postData ,
	  			async: false,
  				success: function(content){
				}
			}); 
		}
	});
	window.location.reload(true);
}
</script>

<div class="head">
                        <div class="info">
							<h1><?php echo element('compName', $this->session->userdata('logged_in')) ?> [ 
								<?php echo element('compNo', $this->session->userdata('logged_in')) ?> ]</h1>
			<ul class="breadcrumb">
            	<li><a href='<?php echo base_url()."home" ?>'><?php echo $this->lang->line('dash') ?></a> <span class="divider">-</span></li>
                <li><a href="home"><?php echo $this->lang->line('module') ?></a> <span class="divider">-</span></li>
                <li class="active"><?php echo $this->lang->line('title') ?></li>
            </ul>
	</div>
	<div class="search">
		<form action="<?php echo base_url() ?>admin/search" method="post">
			<input name="search_text" type="text" placeholder="search..."/>                                
            <button type="submit"><span class="i-magnifier"></span></button>
		</form>
	</div>                        
</div><!-- head --> 
<div class="content">
<div class="wrap">                    
	<div class="row-fluid">
		<div class="span12">
        	<div class="block">
            	<div class="head">
                	<h2><?php echo $this->lang->line('title1'); echo $umaster->username ; ?> [ <?php echo $umaster->userid ; ?> ]</h2>
                </div>
            	<div class="content">
                  <div class="controls-row">
                  	<div class="span2"><?php echo form_label($this->lang->line('company'),'companyID');?></div>
                   	<div class="span6">
						<?php
							$preseldept = '0';
							$options[NULL] = $this->lang->line('select');
							foreach($company as $row){
								$options[$row->ID] = $row->companyName.' ['.$row->companyNo.']';
							}
							$js = "id='companyID' class='span12' onChange='accessRight(this)'";
							echo form_dropdown('companyID', $options, $preseldept, $js);
						?>       
                            	<?php 								
								$data = array(
									'name'=>'userID', 
									'id'=>'userID', 
									'type'=>'hidden',); 
								echo form_input($data,$umaster->fldid); ?>
                     </div>                
                  </div>
                </div>
            </div>
        </div>
    </div>
	<div class="row-fluid">
		<div class="span12">
        	<div class="block">
            	<div class="head">
                </div>
            	<div class="content np">
                    <table cellpadding="0" cellspacing="0" width="100%" id="test" class="editable oc_disable">
                	<thead>
                    <tr>
            			<th style="display:none"></th>
            			<th class="tac">#</th>
                		<th><?php echo $this->lang->line('moduleID')?></th>
                		<th class="tac"><?php echo $this->lang->line('All')?></th>
                		<th class="tac"><?php echo $this->lang->line('transaction')?></th>
                		<th class="tac"><?php echo $this->lang->line('reports')?></th>                
                		<th class="tac"><?php echo $this->lang->line('setup')?></th>                
            		</tr>
        			</thead>
       				 <tbody>
			<?php $bil = 0; 
				if($datatbls){
				foreach($datatbls as $datatbl):
				$bil++?>
            	<tr>
                	<td style="display:none" ><?php echo $datatbl->ID; ?></td>
                	<td class="tac"><?php echo $bil; ?></td>
                	<td id="moduleID"><?php echo $datatbl->description; ?></td>
                	<td id="alls" class="tac">
					<?php 
						$data = array(
    						'name'        => 'alls',
    						'id'          => 'alls',
    						'style'       => 'margin:10px',
							'class'	   => 'alls',
 							'title'	   => $this->lang->line('All')
    					);
//						if($datatbl->setup == 1 && $datatbl->transaction == 1 && $datatbl->reports == 1 ){ $data['checked'] = TRUE; }else{ $data['checked'] = FALSE; };
						echo form_checkbox($data); ?>
                        </td>
                	<td id="transaction" class="tac"><?php 
						$data = array(
    						'name'        => 'transaction',
    						'id'          => 'transaction',
    						'style'       => 'margin:10px',
							'class'	   => 'transaction',
 							'title'	   => $this->lang->line('transaction')
   					);
//						if($datatbl->transaction == 1){ $data['checked'] = TRUE; }else{ $data['checked'] = FALSE; };
						echo form_checkbox($data); ?></td>
                	<td id="reports" class="tac"><?php 
						$data = array(
    						'name'        => 'reports',
    						'id'          => 'reports',
    						'style'       => 'margin:10px',
							'class'	   => 'reports',
 							'title'	   => $this->lang->line('reports')
    					);
//						if($datatbl->reports == 1){ $data['checked'] = TRUE; }else{ $data['checked'] = FALSE; };
						echo form_checkbox($data); ?></td>
                	<td id="set_up" class="tac"><?php 
						$data = array(
    						'name'        => 'set_up',
    						'id'          => 'set_up',
    						'style'       => 'margin:10px',
							'class'	   => 'set_up',
 							'title'	   => $this->lang->line('setup')
    					);
//						if($datatbl->setup == 1){ $data['checked'] = TRUE; }else{ $data['checked'] = FALSE; };
						echo form_checkbox($data); ?></td>
                </tr>
            <?php endforeach; }?>
					</tbody>
			</table>                                         
			</div>
            <div class="footer">
            	<div class="side fr">
                	<div class="btn-group">
                        <?php 
							$data = array(
									'name'=>'Submit', 
									'class'=>'btn btn-primary');
							$js='onclick="updateAccess()"';
							echo form_submit($data,$this->lang->line('Submit'),$js);
							?>
                    </div>
                </div>
            </div>                                    
			</div>                                
    	</div>
	</div>                                
</div>
</div>                                
</div>
</div>
<div class="dialog" id="row_submit" style="display: none;" title="Update User Access?">
    <p>Row will be updated</p>
</div>   
