<?php
	$now = @$this->uri->segment(3);
	$cat = substr($now,-1);
	$stlunas = array('Belum Lunas', 'Lunas');
	
?>

<h3 class="page-header">
	<?php echo $judul;?>
</h3>
<div class="panel panel-default">
<div class="panel-heading">
	<div class="row">
		<div class='col-sm-6'><?php echo $judul;?> List</div>
		<div class='col-sm-6' align='right'>
			<a href="<?=$url?>invoice-new" class="btn btn-success btn-sm"><i class=" fa fa-plus"></i> Add</a>
		</div>
	</div>
</div>
<div class="panel-body">
	<div class="table-responsive">
		<table class="table table-striped table-bordered table-hover dataTables" >
			<thead>
				<tr>
					<th width='35'>No</th>
					<th width=''>Nama</th>
					<th width='100'>Tanggal</th> 
					<th width='100'>Total</th>
					<th width='100'>Sudah Bayar</th>
					<th width='100'>Sisa</th>
					<th width='100'>Status</th>
					<th width='150'></th>
				</tr>
			</thead>
			<tbody>
				<?php
					$no=1;
					if(isset($data) && count($data) > 0): foreach($data as $row):  
					$now = @$this->uri->segment(3);

					$q = "SELECT sum(total) totala FROM `additional` where id_invoice='".$row['id_invoice']."' and status='1'";
					$ccp = $this->db->query($q)->result_array();
					$total = "0";
					if(count($ccp)>0){
						$total= $ccp[0]['totala'];
					}
					
					$bayar = "0";
					$q2 = "SELECT sum(total) bayar FROM `bayar` where id_invoice='".$row['id_invoice']."' and status='1'";
					$ccp2 = $this->db->query($q2)->result_array();
					if(count($ccp2)>0){
						$bayar= $ccp2[0]['bayar'];
					}
					$s = $stlunas[$row['status_lunas']];
					if($s=="Lunas"){
						$statuslunas = "<label class='label label-success' style='font-size:12px;'>$s</label>";
					}else{
						$statuslunas = "<label class='label label-danger' style='font-size:12px;'>$s</label>";
					}
				?>
				<tr>
					<td align='center'><?=$no;?></td>
					<td align='left'><?=$row['nama'];?></td>
					<td align='left'><?=$row['tanggal'];?></td> 
					<td align='left'>Rp. <?=number_format($total);?></td> 
					<td align='left'>Rp. <?=number_format($bayar);?></td> 
					<td align='left'>Rp. <?=number_format($total-$bayar);?></td> 
					<td align='left'><?=$statuslunas;?></td> 
					<td align='center'>
						<a title="Edit" href="<?=$url?>invoice-new?_id=<?=$row['id_invoice']?>" class='btn btn-xs btn-warning '><i class="fa fa-edit "></i></a>
						<a title="Tiket" href="<?=$url?>invoice-tiket?_id=<?=$row['id_invoice']?>" class='btn btn-xs btn-primary '><i class="fa fa-ticket "></i></a>
						<a title="Bayar" href="<?=$url?>invoice-bayar?_id=<?=$row['id_invoice']?>" class='btn btn-xs btn-success '><i class="fa fa-usd "></i></a>
						<a title="Print Invoice" href="<?=base_url()?>cetakinvoice/<?=$row['id_invoice']?>" class='btn btn-xs btn-info '><i class="fa fa-print "></i></a>
						<a title="Delete" class='btn btn-xs btn-danger' onclick="return confirm('Are you sure you want to delete this item?');" href="<?=base_url()?>cms/logic/delete/invoice/<?=$row['id_invoice']?>?part=<?=$now;?>" class="text-danger"><i class="fa fa-trash-o"></i></a>					
					</td>
				</tr>
				<?php $no++; endforeach; endif; ?>
			</tbody>
		</table>
	</div>
</div>
</div>