<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<style>
th { white-space: nowrap; }
</style>
 <link href="<?= $assets ?>styles/jquery.dataTables.css" rel="stylesheet"/>
 <script type="text/javascript" src="<?= $assets ?>js/jquery.js"></script>
   <script type="text/javascript" src="<?= $assets ?>js/jquery.dataTables.js"></script>
<script>
$(document).ready(function() {
    $('#example').dataTable({
    	"footerCallback": function ( row, data, start, end, display ) {
            var api = this.api(), data;
 
            // converting to interger to find total
            var intVal = function ( i ) {
                return typeof i === 'string' ?
                    i.replace(/[\,]/g, '')*1 :
                    typeof i === 'number' ?
                        i : 0;
            };
 
            // computing column Total of the complete result 
            var monTotal = api
                .column( 5 )
                .data()
                .reduce( function (a, b) {
                    return  (intVal(a) + intVal(b)).toFixed(2);
                }, 0 );
				
	    var tueTotal = api
                .column( 6 )
                .data()
                .reduce( function (a, b) {
                    return  (intVal(a) + intVal(b)).toFixed(2);
                }, 0 );
				
            var pageTotals = api
                .column( 4, { page: 'current'} )
                .data()
                .reduce( function (a, b) {
                    return  (intVal(a) + intVal(b)).toFixed(2);
                } );
                 var pageTotal = api
                .column( 5, { page: 'current'} )
                .data()
                .reduce( function (a, b) {
                    return  (intVal(a) + intVal(b)).toFixed(2);
                } );
				
            // Update footer by showing the total with the reference of the column index 
	    $( api.column( 3 ).footer() ).html('Total');
            $( api.column( 4 ).footer() ).html(pageTotals);
            $( api.column( 5 ).footer() ).html(pageTotal);
           
        },
        
    } );
} );
</script>
<div class="box">
    <div class="box-header">
        <div class="box-header">
        <h2 class="blue"><i class="fa-fw fa fa-plus"></i><?= lang('in_list') ?></h2>
    </div>
        
    </div>
    <div class="box-content">
        <div class="row">
            <div class="col-lg-12">
                <!--<p class="introtext"><?= lang('list_results'); ?></p>-->
                 <?php if(!empty($transaction)){?>
 <div class="box"><?php $total=$inco=$expen=$amount=$amountt=$amountts=$amounts=0;?>
  
			   <?php foreach($transaction as $rows):?>
			   <?php  
			   $total+= $rows->amount; 
			   if(($rows->type) == 'income'){
			   $inco+= $rows->amount;
			   //$value =  $total - $inco;print_r($value);
			   }
			   if(($rows->type) == 'expense'){
			   $expen+= $rows->amount;  
			   }
			  $value =  $inco - $expen;
			  if((($rows->account) == '5')&&(($rows->type) == 'income')){
			   $amount+= $rows->amount;  
			   }
			 if((($rows->account) == '4')&&(($rows->type) == 'income')){
			   $amounts+= $rows->amount;  
			   }
			   if((($rows->account) == '5')&&(($rows->type) == 'expense')){
			   $amountt+= $rows->amount;  
			   }
			 if((($rows->account) == '4')&&(($rows->type) == 'expense')){
			   $amountts+= $rows->amount;  
			   }
			    $values =  $amount - $amountt;
				$valuess =  $amounts - $amountts;
			  //print_r( $value);
			  ?>
			   <?php endforeach;?>
			    <h1 class="blue col-md-6"><?= lang('All accounts') ?> : <?=$value;?></h1>
				<h3 class="blue text-right" style="margin-top: 20px;margin-right: 10px;"><?= lang('Cash On Hand') ?> : <?=$values;?></h3>
				<h3 class="blue text-right" style="margin-right: 10px;"><?= lang('Cash On Bank') ?> : <?= $valuess;?></h3><br>
				</div>
				<?php }?>
                <div class="table-responsive">
                        <?php if(!empty($transaction)){?>
                    <table id="example" class="table table-bordered table-hover table-striped dataTable" cellspacing="0" width="100%">
                        <thead>
                        <tr class="primary">
                            <!--<th style="min-width:30px; width: 30px; text-align: center;">
                                <input class="checkbox checkth" type="checkbox" name="check"/>
                            </th>-->
                            <th>#</th>
                            <!--<th><?= lang("type") ?></th>-->
							<th><?= lang("date") ?></th>
							 <th><?= lang("category") ?></th>
                            <th><?= lang("account") ?></th>
                            <!--<th><?= lang("date") ?></th>-->
                            <th><?= lang("Debit") ?></th>
                            <th><?= lang("Credit") ?></th>
                            <th><?= lang("deposit/withdrawal") ?></th>
                            <!--<th><?= lang("reference_no") ?></th>-->
							<th><?= lang("actions") ?></th>
                           
                        </tr>
                        </thead>
                        
                         <tfoot align="right">
		<tr>  <th colspan="4" style="text-align:right">Total:</th><th></th><th></th> <th colspan="2" style="text-align:right"></th></tr>
	</tfoot>   
                        <tbody>
						<?php  $count = 1; $inc = 0;
						if($transaction != '' ){?>
						<?php foreach($transaction as $row):?>
                       <?php if(($row->type) == 'income'){?>
                        <tr>
							<!--<th style="min-width:30px; width: 30px; text-align: center;">
                                <input class="checkbox checkft" type="checkbox" name="check"/>
                            </th>-->
							<td><?php echo $count++; ?></td>
							<!--<td><?=$row->type; ?></td>-->
							<td><?=$row->date; ?></td>
							<?php $cates = $this->db->get_where('accounts' , array('id' => $row->category))->row()->Name;?>
							<td><?php echo $cates; ?></td>
							<?php $cate = $this->db->get_where('accounts' , array('id' => $row->account))->row()->Name;?>
							<td><?php echo $cate; ?></td>
							 
							<td>0.00</td> 
							<td><?=$this->sma->formatQuantity($row->amount); 
							$inc+= $row->amount; ?></td>
							<td><?=$row->deposit_withdrawal; ?></td> 
							<!--<td><?=$row->reference_no; ?></td>--> 
							<td>
							 <div class="btn-group">
                                <button type="button" class="btn btn-default btn-xs btn-primary dropdown-toggle" data-toggle="dropdown">
                                    Action <span class="caret"></span>
                                </button>
                                <ul class="dropdown-menu dropdown-default pull-right" role="menu">
                                    
                                     <!-- VIEW LINK -->
                                    <li><?php $id = $row->id;?>
                                        <a href="<?= admin_url('transaction/transaction_viewi/'.$id); ?>">
                                            <i class="fa fa-eye"></i>
                                                <?= lang("View") ?>
                                            </a>
                                                    </li>
                                    <li class="divider"></li>
                                    
                                    
                                    <!-- DELETION LINK -->
                                    <li>
                                        <a href="<?= admin_url('transaction/deleteSi/'.$id); ?>">
                                            <i class="fa fa-trash-o "></i>
                                              <?= lang("Delete") ?>
                                            </a>
                                                    </li>
                                </ul>
                            </div>
							</td>
							 
						   
						   
                        </tr>
						 <?php  } ?>
						<?php endforeach;
						}
						?>
                        </tbody>

                        
                    </table>
                     <?php 
    }
    else{
						 
						 ?>
						 <table class="table table-bordered table-hover table-striped dataTable" cellspacing="0" width="100%">
                        <thead>
                        <tr class="primary">
                            <!--<th style="min-width:30px; width: 30px; text-align: center;">
                                <input class="checkbox checkth" type="checkbox" name="check"/>
                            </th>-->
                            <th>#</th>
                            <!--<th><?= lang("type") ?></th>-->
							<th><?= lang("date") ?></th>
                            <th><?= lang("category") ?></th>
							 <th><?= lang("account") ?></th>
                            <!--<th><?= lang("date") ?></th>-->
                            <th><?= lang("Debit") ?></th>
                            <th><?= lang("Credit") ?></th>
                            <th><?= lang("deposit/withdrawal") ?></th>
                            <!--<th><?= lang("reference_no") ?></th>-->
							<th><?= lang("actions") ?></th>
                           
                        </tr>
                        </thead>
                        <tbody>
						     <tr>
						         <td colspan="8">
						             No Transactions
						         </td>
						     </tr>
						     </tbody>
						 </table>
						 <?php 
						}
						?>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">

	jQuery(document).ready(function($)
	{
		

		var datatable = $("#table_export").dataTable();
		
		$(".dataTables_wrapper select").select2({
			minimumResultsForSearch: -1
		});
	});
	
</script>
