<script type="text/javascript">
$(document).ready(function() {   
	$('select').select2();
	//Datepicker
   jQuery('.datepicker2').datepicker({
			dateFormat : 'dd-mm-yy',
			changeMonth : true,
			changeYear : true
			});
   
   //Data table configurations
   var oTable = $("#test").dataTable({
	   	"iDisplayLength": 10,
		"aLengthMenu": [5,10,25,50,100],
		"sPaginationType": "full_numbers",
		"aoColumns": [	{"bSortable": false },
						{"bSortable": false },
					<?php foreach($tbHdr as $v): ?>
						null,
					<?php endforeach; ?>
						{"bSortable": false }
					] 
	});
	oTable.fnSort( [ [0,'asc'] ] );

	//
	var unReconcileBal = 0;
	$('.bankID,#selDateFrom').change(function(){
		var date = $('#selDateFrom').val();
		var bankID = $('.bankID option:selected').val();
		$.ajax({
			url: "<?php echo base_url(); ?>bankreconciliation/gatherinfo",
			async: false,
			type: "POST",
			data: 'bankID='+bankID+'&date='+date,
			dataType: "html",
			success: function(data) {
				var obj = jQuery.parseJSON(data);
				if(obj != ''){
					oTable.fnDeleteRow(0,null,true);
					var serial = 1;
					oTable.fnClearTable();
					var unReConDiff = 0;
					jQuery.each(obj.statements,function(i,v){
						var input = "";
						if(v.reconcile != 1){
							var input = "<input type='checkbox' value='"+v.ID+"' class='tac' />";
							unReConDiff = parseFloat(unReConDiff)+parseFloat(v.amountPaid);
						}	
						var rowIndex = oTable.fnAddData( [
											v.ID,
											serial,
											v.paymentDate,
											v.transNo,
											v.referenceNo,
											v.accountDescription,
											parseFloat(v.amountPaid).toFixed(2),
											input
											] );


						
					    var row = oTable.fnGetNodes(rowIndex);
					    $(row).find('td:nth-child(1)').css('display','none').addClass('ID');
					    $(row).find('td:nth-child(7)').addClass('amountPaid');
					    $(row).find('td:last-child').addClass('reconsil');
					    $(row).find('td').addClass('tac');
						serial++;
					});
					//$('.unreconcil').val(unReConDiff.toFixed(2));
					unReconcileBal = unReConDiff.toFixed(2)
					jQuery.each(obj.reconcile,function(i,v){
						var a = new Date(Date.parse(v.createdTS));
						var date = a.getDate();
						var month = a.getMonth()+1;
						var year = a.getFullYear();	
							
						//console.log(v.lastReconDate);
						$('.lasReconDate').val(date+'-'+month+'-'+year);
						$('.lasReconBal').val(parseFloat(v.reconcileBalance).toFixed(2));
						$('.statClosBal').val(parseFloat(v.bankStatementBalance).toFixed(2));
						$('.unreconcil').val(parseFloat(v.bankStatementBalance).toFixed(2));
					});
					
				}else{
					oTable.fnClearTable();
					$('.lasReconDate').val('');
					$('.lasReconBal').val('');
					$('.statClosBal').val('');
					$('.unreconcil').val('');
				}
			}
		});
	});	  

	
	//reconsiliation
	$('.reconsil input').live('change',function(){
		/*var total = 0;
		$('.reconsil input:checked').each(function(){
			total += parseFloat($(this).parents('tr').find('.amountPaid').text());
		});*/
		if($(this).attr('checked') == 'checked'){
			var total = (parseFloat($('.unreconcil').val())-parseFloat($(this).parents('tr').find('.amountPaid').text())).toFixed(2);
			$('.unreconcil').val(total);
		}else{
			var total = (parseFloat($('.unreconcil').val())+parseFloat($(this).parents('tr').find('.amountPaid').text())).toFixed(2);
			$('.unreconcil').val(total);
		}
		if(total == '0.00'){
			var conID = '';
			$('.reconsil input:checked').each(function(){
				if(conID == '')
					conID += $(this).val();
				else
					conID += ','+$(this).val();
			});
			if(conID != ''){
				$.ajax({
					url: "<?php echo base_url(); ?>bankreconciliation/setconcile",
					async: false,
					type: "POST",
					data: 'conID='+conID
							+'&modReconBal='+unReconcileBal
							+'&bankID='+$('.bankID option:selected').val()
							+'&bankStatementDate='+$('#selDateFrom').val()
							+'&bankStatementBalance='+$('#statClosBal').val()
							+'&totalCredit='+unReconcileBal,
					dataType: "html",
					success: function(data) {
						if(data == 'Success'){
							location.reload();
						}
					}
				});
			}
		}
	});
}); 

</script>

<div id="content">                        
<div class="wrap">
<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(); ?>'><?php echo $this->lang->line('dashboard') ?></a> <span class="divider">-</span></li>
            <li><a href="<?php echo base_url(); ?>/locationtransfer"><?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">
	<!--Company information start-->
   	<?php //$this->load->view('layouts/rptheader'); ?>
    <!--Company information end-->
    
	<div class="wrap">        
        <div class="content">
            <div class="wrap">                 
                <div class="row-fluid">
                    <div class="span12">
                        <!--Location transfer top form-->  
			            <div class="row-fluid scRow">
			                <div class="span6 scCol">        	
			                    <div class="block" id="grid_block_1">
			                        <div class="head">
				                    	<h2><?php echo $this->lang->line('title') ?></h2>
				                    </div><!-- head -->
			                        <div class="content">                 	
			                            <div class="controls-row">
			                                <div class="span4"><?php echo $this->lang->line('bank') ?>*</div>
			                                <div class="span8">
			                                    <?php
			                                    	$preselcat = '';
													$options[''] = 'Please select';
			                                        foreach($bank as $row){
			                                            $options[$row->ID] = $row->bankName." (".$row->linkAccountID.")";
			                                        }
			                                        $js=' class="validate[required] input-medium bankID"';
			                                        echo form_dropdown('bankID', $options, $preselcat, $js);
			                                    ?>
			                                </div>                       
			                            </div> 
			                            <div class="controls-row">
			                                <div class="span4"><?php echo $this->lang->line('lastReconciliationDate') ?></div>
			                                <div class="span8">
			                                    <?php
			                                    $data = array(
			                                        'value'=>'',//$formNo,
			                                        'name'=>'lasReconDate',
			                                        'id'=>'lasReconDate',
			                                        'class'=>'input-medium lasReconDate',); 
			                                    echo form_input($data, '');//set_value($formNo));
			                                    ?>
			                                </div>                       
			                            </div>
			                            <div class="controls-row">
			                                <div class="span4"><?php echo $this->lang->line('lastReconciliationBalance') ?></div>
			                                <div class="span8">
			                                    <?php
			                                    $data = array(
			                                        'value'=>'',//$formNo,
			                                        'name'=>'lasReconBal',
			                                        'id'=>'lasReconBal',
			                                        'class'=>'input-medium lasReconBal',); 
			                                    echo form_input($data, '');//set_value($formNo));
			                                    ?>
			                                </div>                       
			                            </div>
			                            <div class="controls-row" style="padding:10px 10px 8px;">
					                    	<div class="span4"><?php echo form_label($this->lang->line('statDate')); ?></div>
					                        <div class="input-prepend">
						                        <span class="add-on"><i class="i-calendar"></i></span>
												<?php
																$data = array(
																'name' => 'selDateFrom',
																'id' => 'selDateFrom',
																'type' => 'text',
																'class' => 'input-small datepicker2',);
																$js='';
																echo form_input($data, date('d-m-Y', time($statDate)), $js);
																
															?>       
					                        </div>
					                    </div>                         
			                        </div> 
			                    </div> <!-- Block -->
			                </div>
			                <div class="span6 scCol">
			                    <div class="block" id="grid_block_1">	
			                        <div class="content"> 
			                            <div class="controls-row">
			                                <div class="span4"><?php echo $this->lang->line('statClosBal') ?></div>
			                                <div class="span8">
			                                    <?php
			                                    $data = array(
			                                        'value'=>'',//$formNo,
			                                        'name'=>'statClosBal',
			                                        'id'=>'statClosBal',
			                                        'class'=>'input-medium statClosBal',); 
			                                    echo form_input($data, '');//set_value($formNo));
			                                    ?>
			                                </div>                       
			                            </div>
			                            <?php /*
			                            <div class="controls-row">
			                                <div class="span4"><?php echo $this->lang->line('deposit') ?></div>
			                                <div class="span8">
			                                    <?php
			                                    $data = array(
			                                        'value'=>'',//$formNo,
			                                        'name'=>'formNo',
			                                        'id'=>'formNo',
			                                        'class'=>'input-medium',); 
			                                    echo form_input($data, '');//set_value($formNo));
			                                    ?>
			                                </div>                       
			                            </div>
			                            <div class="controls-row">
			                                <div class="span4"><?php echo $this->lang->line('unpresentedCheque') ?></div>
			                                <div class="span8">
			                                    <?php
			                                    $data = array(
			                                        'value'=>'',//$formNo,
			                                        'name'=>'formNo',
			                                        'id'=>'formNo',
			                                        'class'=>'input-medium',); 
			                                    echo form_input($data, '');//set_value($formNo));
			                                    ?>
			                                </div>                       
			                            </div>
			                            <div class="controls-row">
			                                <div class="span4"><?php echo $this->lang->line('glBalance') ?></div>
			                                <div class="span8">
			                                    <?php
			                                    $data = array(
			                                        'value'=>'',//$formNo,
			                                        'name'=>'formNo',
			                                        'id'=>'formNo',
			                                        'class'=>'input-medium',); 
			                                    echo form_input($data, '');//set_value($formNo));
			                                    ?>
			                                </div>                       
			                            </div>
			                             */ ?>
			                            <div class="controls-row">
			                                <div class="span4"><?php echo $this->lang->line('UnReconciledDifference') ?></div>
			                                <div class="span8">
			                                    <?php
			                                    $data = array(
			                                        'value'=>'',//$formNo,
			                                        'name'=>'UnReconciledDifference',
			                                        'id'=>'UnReconciledDifference',
			                                        'class'=>'input-medium unreconcil',); 
			                                    echo form_input($data, '');//set_value($formNo));
			                                    ?>
			                                </div>                       
			                            </div>
			                        	    			                                                      
			                        </div>
			                    </div>
			                </div>
			            </div>
			            <!--Location transfer top form-->
                        <div class="block">                            
                            <div class="head">
                                <h2><?php echo $this->lang->line('title') ?></h2>
                            	<!--<ul class="buttons">
                                     <li><a href="<?php echo base_url()?>locationtransfer/printReport/<?php echo $dateFrom.'/'.$dateTo; ?>" title="updatePosting" onclick=""><?php echo $this->lang->line('print') ?></a></li>
                                     <li><a href="<?php echo base_url(); ?>locationtransfer/add" title="Add New"><span class="i-plus-2"></span></a></li>  
                                </ul>-->
                            </div><!-- head -->
                            <div class="content np table-sorting">
                                <table cellpadding="0" cellspacing="0" width="100%" id="test" class="editable itemTbl">
                                    <thead>
                                    <tr>
                                    	<th style="display:none;">&nbsp;</th>
                                    	<th class="tac">#</th>
                                    <?php foreach($tbHdr as $row): ?>
                                        <th class="tac"><?php echo $this->lang->line($row); ?></th>
                                    <?php endforeach; ?>                       	
				                		<th width="55" class="tac"><?php echo $this->lang->line('action');?></th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php $bil = 0; 
                                    if(isset($datatbls) && !empty($datatbls)):
                                        foreach($datatbls as $item):
                                        $bil++?>
                                        <tr class="itemList">
                                        	<td class="ID" style="display:none"><?php echo $item->ID; ?></td>
                                            <td class="tac"><?php echo $bil; ?></td>
										<?php foreach($tbHdr as $row): ?>
                                            <td class="tac span2">
												<?php
													echo $item->$row; 
												?>                                            	        
                                            </td>
                                        <?php endforeach; ?>    
                                        	<td class="tac"><?php
												$js = 'class="btn btn-mini btn-primary detail" title="'.$this->lang->line("Show Detail").'"';
												echo form_button('edit','<span class="i-pen">',$js); ?>
											</td>
                                        </tr>
                                        <?php 
                                        endforeach;
                                    endif;
                                    ?>
                                    </tbody>         
                                </table>
                            </div><!-- content -->                              
                        </div><!-- block -->
                    </div><!-- span12 -->
                </div><!-- row-fluid -->	
            </div><!-- wrap -->
        </div><!-- content -->
	</div>
</div>
