<script>
$(document).ready(function(){
	$('select').select2();
	jQuery('.datepicker2').datepicker({
			dateFormat : 'dd-mm-yy',
			changeMonth : true,
			changeYear : true
			});
	
    $("table.editable").on("click",".edit",function(){
        var eRow   = $(this).parents('tr');
        var eState = $(this).attr('data-state');
        
        if(eState == null){
            //$(this).html('Save');
            eState = 1;
        }
        if(eState == 1) 
            $(this).attr('data-state','2');
        else{
            $(this).removeAttr('data-state');
            //$(this).html('Edit');
        }
    });
        
});
</script>

<script type="text/javascript">
$(document).ready(function() {
    /* Build the DataTable with third column using our custom sort functions */
    var oTable = $("#test").dataTable( {
        "iDisplayLength": 10,
		 "aLengthMenu": [5,10,25,50,100],
		 "asSorting": [ "asc"], "aTargets": [ 1 ],
		 "sPaginationType": "full_numbers", "aoColumns": [ { 
		 	"bSortable": false }, 
			null, 
			null,
			null, 
                        null,
                         null,
			null, 
			null, { 
			"bSortable": false } ]});
     oTable.fnSort( [ [1,'asc'] ] );
	 $("table .checkall1").live("click", "checkall1",function(){           
		$(oTable.fnGetNodes()).find(':checkbox').prop('checked', this.checked);
	});
    $("table.editable").on("click",".edit",function(){
        rRow = $(this).parents("tr");
        ID = $('td:first', $(this).parents('tr')).text(); 
        $("#row_edit").dialog("open");
    });
    $("#row_edit").dialog({
        autoOpen: false,
        resizable: false,        
        modal: true,
        buttons: {
            "Continue...": function() {
				window.location.href = "<?php echo base_url() ?>aptransaction/editPayment/" + ID;		
                $(this).dialog("close");
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        }
    });    
     function remove_row(row){
       
 		$.ajax({
  			type: "POST",
  			url: "<?php echo base_url() ?>aptransaction/supplierpaymentDelete",
			data:"ID="+ID,
  			success: function(data){
				if($.trim(data) == "success"){
                                    alertbox("Row successfully deleted !!! ",1);
                                     row.remove();
                                }else{
                                     alertbox("Fail to delete row !!! ",2);
                                }
			},
		});       // Here your ajax action after delete confirmed
    }
    $("#row_delete").dialog({
        autoOpen: false,
        resizable: false,        
        modal: true,

        buttons: {
            "Delete": function() {
//				window.location.href = "<?php echo base_url() ?>po_transaction/purchaseOrderCancel/" + ID;
				//window.location.href = "purchaseOrderCancel?ID=" + ID;		
                remove_row(rRow);
                $(this).dialog("close");
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        }
    });
});



 function delete_row(me){
    
     rRow = $(me).parents("tr");
        ID = $('td:first', $(me).parents('tr')).text(); 
        $("#row_delete").dialog("open");
    }

function changeList(){
	var addyear = $('#add_year option:selected').val();
	var addper = $('#add_period option:selected').val();
	var urls = "<?php echo base_url();?>aptransaction/supplierpaymentlist/"+addyear+"/"+addper;
		window.location = urls;
}

function printSupplierPaymentList(){
	var selDateFrom = $('#selDateFrom').val();
	var selDateTo = $('#selDateTo').val();
	window.open("<?php echo base_url();?>aptransaction/supplierpaymentlist_print/"+selDateFrom+"/"+selDateTo);
		
}

function updatePosting(){
   var oTable = $("#test").dataTable( );
//   alert("hai");
	$(oTable.fnGetNodes()).find(':checkbox').each(function () {
        if($(this).is(':checked')){
			var ID = $('td:first', $(this).parents('tr')).text(); 
			var updateAble = $('td:eq(2)', $(this).parents('tr')).text(); 
			var postData = {
				'fldid' : ID,
				'updateAble' : updateAble,
			};
 			$.ajax({
  				type: "POST",
	  			dataType: "json",
				url: "<?php echo base_url()?>aptransaction/paymentPosting/",
				data: postData ,
	  			async: false,
  				success: function(content){
					//window.location.reload(true);
				}
			}); 
		}
	});
        window.location.reload(true);
} 

function selectYearPeriod(obj){
	var year = obj.value;
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "<?php echo base_url()?>glreports/get_TrialBalance_year/"+year,
		data: year,
		async: false,
		success: function(content){
			$("#add_period").empty().select2('val',null);
			if(content.status == "success") {
				var items = [];
				//items.push('<option>-Please Select-</option>');
				for ( var i = 0; i < content.message.length; i++) {
					items.push('<option value="'+content.message[i].period+'">'
					+ content.message[i].financialPeriod + '</option>');
				}  //end for
				jQuery("#add_period").empty();
				jQuery("#add_period").append(items.join('</br>'));
				//jQuery("#add_period").val(null);
				$("#add_period").select2();
                changeList();
			}else {
				jQuery("#add_period").empty();
				$("#error").html('<p>'+content.message+'</p>');
				$("#add_period").select2('val',null);
			} //end if
		} //end success		
	}); //end ajax
}


function printSupplierPaymentConfirmList(){
	var df = $('#add_year').val();
	var dt = $('#add_period').val();
	window.open("<?php echo base_url();?>aptransaction/supplierpaymentlistPrint/"+df+"/"+dt);
	
}

</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 $this->lang->line('dashboard') ?></a> <span class="divider">-</span></li>
                <li><a href="#"><?php echo $this->lang->line('module') ?></a> <span class="divider">-</span></li>
                <li class="active"><?php echo $this->lang->line('titleAdvicePayment') ?></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('titleAdvicePayment') ?></h2>
                    </div><!-- head -->
				<div class="content np">    
                	<div class="controls-row"><!-- Row 4-->
						<div class="span5 tac">
                    	<div class="span2 tar"><?php echo form_label($this->lang->line('year'),'add_year');?></div>
						<?php
							$preselyear = $selyear;
							$options = '';
							$options[] = $this->lang->line('selectyear');
							if(isset($yrs) && !empty($yrs)){	
								foreach($yrs as $row){
									$options[$row] = $row;
								}
							}
							$js='onchange="selectYearPeriod(this)" class="input-medium validate[required]" id="add_year"';

							echo form_dropdown('add_year', $options, $preselyear, $js);
						?>       
                        </div>
						<div class="span5 tac">
                    	<div class="span2 tar"><?php echo form_label($this->lang->line('period'),'add_period');?></div>
						<?php
							$preselperiod = $selper;
							$options = '';
							$options[] = $this->lang->line('period');
							if($period){
								foreach($period as $row){
									$options[$row->period] = $row->financialPeriod;
								}
							}
							$js='class="input-medium validate[required]" id="add_period" style="width:75%" OnChange="changeList()"';

							echo form_dropdown('add_period', $options, $preselperiod, $js);
						?>&nbsp;&nbsp;  
                        </div>
						<!--<div class="span2 tac">
                        <?php
							$data = array(
									'name'=>'mysubmit',
									'class'=>'btn-form btn-success-form');
									$js="onClick='changeList()'";
							echo form_button($data,$this->lang->line('submit'),$js);
							?>
                        </div>-->
                 	</div><!-- End Row 4-->
				</div><!-- content -->
            	<div class="head">
                	<h2><?php echo $this->lang->line('titleAdvicePayment') ?></h2>
                    <div class="side fr">
						<a href="" name="updatePosting" class="btn-form btn-success-form" onClick="printSupplierPaymentConfirmList()"><?php echo $this->lang->line('print');?></a>
					</div>
                </div>
            	<div class="content np table-sorting">
            	<table cellpadding="0" cellspacing="0" width="100%" id="test" class="editable">
                	<thead>
                    <tr>
            			<th style="display:none"></th>
            			<th class="tac">#</th>
            			<th><?php echo $this->lang->line('formNo');?></th>
                        <th class="tac"><?php echo $this->lang->line('paymentDate');?></th>
						<th><?php echo $this->lang->line('supplierName');?></th>
                                                <th><?php echo $this->lang->line('referenceNo');?></th>
                        <th><?php echo $this->lang->line('amountPaid');?></th>
                		<th width='55' class='tac'><input type='checkbox' id='checkall1' class='checkall1'/></th>
                		<th class="tac"><?php echo $this->lang->line('delete');?></th>
            		</tr>
        			</thead>
       				 <tbody>
			<?php $bil = 0;
			//echo var_dump($datatbls); 
				if($datatbls){
				foreach($datatbls as $datatbl):
				$bil++?>
            	<tr>
                	<td style="display:none" ><?php echo $datatbl->ID; ?></td>	
                	<td><?php echo $bil; ?></td>
                	
                	<td><a title="<?php echo $this->lang->line('details');?>" class="btn btn-mini btn-link edit"><?php echo $datatbl->formNo; ?></a></td>
                    <td class="tac"><?php echo date("d-m-Y", strtotime($datatbl->paymentDate)); ?></td>
                	<td><?php echo $datatbl->supplierName ?></td>
                        <td><?php echo $datatbl->referenceNo; ?></td>
                    <td class="tar"><?php echo number_format($datatbl->amountPaid,2,'.',','); ?></td>
						<td class="tac"><?php
							echo"<input type='checkbox' class='checkbox' name='updateAble' id='updateAble' value='0'/>";
							?></td>
							 <td class="tac">
                        <button onclick="delete_row(this)" class="btn-form btn-danger-form"><i class="i-trashcan"></i></button>
                    </td>
                </tr>
            <?php endforeach; }?>
					</tbody>
			</table>
            <div class='footer'>
            	<div class='side fl'>
                	<!--div class='btn-group'>
                    <?php
							$data = array(
									'name'=>'updatePosting', 
									'class'=>'btn-form btn-info-form');
							$js='onclick="printSupplierPaymentList()"';
							echo form_submit($data,$this->lang->line('printList'),$js);
							?>
                    </div-->
                </div>
            	<div class='side fr'>
                	<div class='btn-group'>
                    <?php
							$data = array(
									'name'=>'updatePosting', 
									'class'=>'btn-form btn-warning-form');
							$js='onclick="updatePosting()"';
							echo form_submit($data,$this->lang->line('updatePosting'),$js);
							?>
                    </div>
                </div>
            </div>
			</div>
<div class="dialog" id="row_edit" style="display: none;" title="Edit ?">
    <p><?php echo $this->lang->line('message2');?></p>
</div>  
</div>
</div>
</div>
</div>
</div>
</div>
</div>

 <div class="dialog" id="row_delete" style="display: none;" title="Delete?">
    <p>Row will be deleted</p>
</div>

