<div id="content">                        
<div class="wrap">
<script type="text/javascript">
$(document).ready(function() {
	$('select').select2();
	$(".accessDetails").hide()
	$(".accessDetails2").hide()
	$.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, 
		 "bInfo": false,
		 "aoColumns": [ { 
		 	"bSortable": false }, 
			null, 
			null, 
			null, 
			null, 
			null, 
			null ]});
    var oTable2 = $("#oTable2").dataTable( {
		 "asSorting": [ "asc"], "aTargets": [ 1 ],
		 "bPaginate": false, 
		 "bFilter": false, 
		 "bInfo": false,
		 "aoColumns": [ { 
		 	"bSortable": false }, 
			null, 
			null, { 
			"bSortable": false } ]});
	$("#Submit").attr("disabled", "disabled");
  	$("#allsDetails").click(function () {
        $('#oTable2 tbody input[type="checkbox"]').prop('checked', this.checked);
    });
	$("#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(){
	var companyID = $("#companyID option:selected").val();
	$(".accessDetails").show();
	$(".accessDetails2").hide();
//	var companyID = obj.value;
	if(!companyID){$(".accessDetails").hide()}else{
	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();
						$(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");
						for ( var ii = 0; ii < content.message.length; ii++) {
							if(content.message[ii].moduleID == module){
								if(content.message[ii].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[ii].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[ii].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[ii].transaction == 1 && content.message[ii].reports == 1 && content.message[ii].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);
}
function accessDetailsFunc(obj){
 	$("#oTable2 tbody tr").remove(); 	
    var rRow = $(obj).parents("tr");
	var fldid = $("#userID").val();
	var module = $('td:first', rRow).text();
	var companyID = $("#companyID option:selected").val();
	var detailsAcccess = obj.value;
//	alert(obj.value);
	if(companyID != null){
		$(".accessDetails").hide();
		$(".accessDetails2").show();
		var postData = {
			'fldid' : fldid,
			'module' : module,
			'companyID' : companyID,
			'detailsAcccess' : detailsAcccess,
		};
 			$.ajax({
  				type: "POST",
	  			dataType: "json",
				url: "<?php echo base_url()?>admin/accessDetails",
				data: postData ,
	  			async: false,
  				success: function(content){
					if(content.status == "success") {
						var counter = 0;
						$("#moduleID2").val(content.module);
						for ( var i = 0; i < content.message.length; i++) {
							counter++;
							var displayLine = content.message[i].name;
							var lineAdd = "<tr><td style='display:none'>"+content.message[i].code + "</td>" +
        					"<td class='tac'>" + counter + "</td>" +
        					"<td>" + content.message[i].name + "</td>" +
        					'<td class="tac">' + 
							'<input id="allsDet" class="allsDet" type="checkbox" title="All" style="margin:10px" value="" name="allsDet"';
							if(content.message[i].accessRight == 1){
								lineAdd = lineAdd + ' checked="checked"';
							}
							lineAdd = lineAdd + '>' +
							'</td>' +
					        "</tr>";
							$("#oTable2 tbody").append(lineAdd);
							$("#access").val(content.message[i].type);
						}
					}
				}
			}); 
	} 
}
function updateAccess2(){
	var fldid = $("#userID").val();
	var access = $("#access").val();
	var moduleID = $("#moduleID2").val();
	var companyID = $("#companyID option:selected").val();
   	var oTable2 = $("#oTable2").dataTable( );
	var tablerows1 = $("#oTable2 > tbody > tr").length;
	var accessRight = 0;
		if(access == 1){
			var col = 'transaction';
		}else if(access == 2){
			var col = 'reports';
		}else{
			var col = 'setup';
		}
	for (var i = 0; i < tablerows1 ; i++){
		if($("#oTable2 tbody tr:eq("+ i +")").find("td:eq(3) input:checkbox").is(":checked")){
			accessRight = 1;
		}else{
			accessRight = 0;
		}
			var menuCode = $("#oTable2 tbody tr:eq("+ i +")").find("td").eq(0).text();
			var postData = {
				'fldid' : fldid,
				'module' : moduleID,
				'companyID' : companyID,
				'menuCode' : menuCode,
				'accessRight' : accessRight,
				'col' : col,
			};
 			$.ajax({
  				type: "POST",
	  			dataType: "json",
				url: "<?php echo base_url()?>admin/accessDetailsSave",
				data: postData ,
	  			async: false,
  				success: function(content){
					if(content.status == "success") {
						if(i == 0){
							alert(content.message1);
						}
					}
				}
			}); 
	}
}

</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); ?>
                            	<?php 								
								$data = array(
									'name'=>'moduleID2', 
									'id'=>'moduleID2', 
									'type'=>'hidden',); 
								echo form_input($data); ?>
                            	<?php 								
								$data = array(
									'name'=>'access', 
									'id'=>'access', 
									'type'=>'hidden',); 
								echo form_input($data); ?>
                     </div>                
                  </div>
                </div>
            </div>
        </div>
	<div class="row-fluid accessDetails">
		<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"><a onclick="accessDetails(this)"><?php echo $datatbl->description; ?></a></td>
                	<td id="alls" class="tac">
					<?php 
						$data = array(
    						'name'        => 'alls',
    						'id'          => 'alls',
    						'style'       => 'margin:10px',
							'class'	   => 'alls',
 							'title'	   => $this->lang->line('All')
    					);
						echo form_checkbox($data); ?>
                        </td>
                	<td id="transaction" class="tac">
                    <div class="10">
                    <span class="add-on"><button value="1" class="btn-mini" onclick="accessDetailsFunc(this)" title="<?php echo $this->lang->line('details')?>"><i class="i-arrow-down-2"></i></button></span>
					<?php 
						$data = array(
    						'name'        => 'transaction',
    						'id'          => 'transaction',
    						'style'       => 'margin:10px',
							'class'	   => 'transaction',
 							'title'	   => $this->lang->line('transaction')
   					);
						echo form_checkbox($data); ?>
                    </div>
                        </td>
                	<td id="reports" class="tac">
                    <div class="12">
                    <span class="add-on"><button value="2" class="btn-mini" onclick="accessDetailsFunc(this)" title="<?php echo $this->lang->line('details')?>"><i class="i-arrow-down-2"></i></button></span>
					<?php 
						$data = array(
    						'name'        => 'reports',
    						'id'          => 'reports',
    						'style'       => 'margin:10px',
							'class'	   => 'reports',
 							'title'	   => $this->lang->line('reports')
    					);
						echo form_checkbox($data); ?>
                    </div>
                        </td>
                	<td id="set_up" class="tac">
                    <div class="12">
                    <span class="add-on"><button value="3" class="btn-mini" onclick="accessDetailsFunc(this)" title="<?php echo $this->lang->line('details')?>"><i class="i-arrow-down-2"></i></button></span>
					<?php 
						$data = array(
    						'name'        => 'set_up',
    						'id'          => 'set_up',
    						'style'       => 'margin:10px',
							'class'	   => 'set_up',
 							'title'	   => $this->lang->line('setup')
    					);
						echo form_checkbox($data); ?>
                    </div>
                        </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>      <!-- footer -->                              
			</div>  <!-- block -->                              
    	</div><!-- span12 -->
	</div>   <!-- accessDetails -->                             
    </div>
	<div class="row-fluid accessDetails2">
		<div class="span12">
        	<div class="block">
            	<div class="head">
                </div>
            	<div class="content np">
                    <table cellpadding="0" cellspacing="0" width="100%" id="oTable2" class="editable oTable2">
                	<thead>
                    <tr>
            			<th style="display:none"></th>
            			<th class="tac" width="5">#</th>
                		<th><?php echo $this->lang->line('Menu')?></th>
                		<th class="tac">					<?php 
						$data = array(
    						'name'        => 'allsDetails',
    						'id'          => 'allsDetails',
    						'style'       => 'margin:10px',
							'class'	   => 'allsDetails',
 							'title'	   => $this->lang->line('All')
    					);
						$js='onChange="checkAll()"';
						echo form_checkbox($data, '', TRUE); ?>
						</th>                
            		</tr>
        			</thead>
       				 <tbody>
			<?php $bil = 0; 
				if($datatbls){
				foreach($datatbls as $datatbl):
				$bil++?>
            <?php endforeach; }?>
                     </tbody>
                     </table>
                </div>
            <div class="footer">
            	<div class="side fr">
                	<div class="btn-group">
                        <?php 
							$data = array(
									'name'=>'myCancel', 
									'class'=>'btn btn-danger');
							$js='onclick="accessRight()"';
							echo form_button($data,$this->lang->line('Cancel'),$js);
							?>
                        <?php 
							$data = array(
									'name'=>'mySubmit', 
									'class'=>'btn btn-primary');
							$js='onclick="updateAccess2()"';
							echo form_button($data,$this->lang->line('Submit'),$js);
							?>
                    </div>
                </div>
            </div>      <!-- footer -->                              
			</div>  <!-- block -->                              
    	</div><!-- span12 -->
	</div>   <!-- accessDetails2 -->                             
</div>
</div>                                
</div>
</div>
<div class="dialog" id="row_submit" style="display: none;" title="Update User Access?">
    <p>Row will be updated</p>
</div>   
