<?php
	$now = @$this->uri->segment(3);
	$cat = substr($now,-1);
	$stlunas = array('Belum Lunas', 'Lunas');
	$q = "SELECT sum(total) totala FROM `additional` where id_invoice='".$_GET['_id']."' 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='".$_GET['_id']."' and status='1'";
	$ccp2 = $this->db->query($q2)->result_array();
	if(count($ccp2)>0){
		$bayar= $ccp2[0]['bayar'];
	}
	$sisa = $total - $bayar;
?>
<style>
	p{padding-top:0px;
	font-size: 14px;
    line-height: 12px;}
	#mce_15, #mce_20,#mce_29-body{display:none;}
</style>
<div class="panel panel-default">
	<div class="panel-heading">
		<div class="row">
			<div class='col-sm-6'>Data Invoice</div>
			<div class='col-sm-6' align='right'>

			</div>
		</div>
	</div>  
	<div class="panel-body">  
		<div class="form-group">
			<div class="row">
				<div class="col-sm-2">
					<label class="control-label">Nama</label>
				</div>
				<div class="col-sm-4"> 
					<?=(isset($invoice['nama']) ? $invoice['nama'] : '')?>
				</div>
				<div class="col-sm-2">
					<label class="control-label">Email</label>
				</div>
				<div class="col-sm-4"> 
					<?=(isset($invoice['email']) ? $invoice['email'] : '')?>
				</div>
			</div>
		</div>			
		<div class="form-group">
			<div class="row">
				<div class="col-sm-2">
					<label class="control-label">Hp</label>
				</div>
				<div class="col-sm-4"> 
					<?=(isset($invoice['hp']) ? $invoice['hp'] : '')?>
				</div> 
				<div class="col-sm-2">
					<label class="control-label">Tanggal</label>
				</div>
				<div class="col-sm-4"> 
					<?=(isset($invoice['tanggal']) ? $invoice['tanggal'] : '')?>
				</div>
			</div>
		</div>			
		<div class="form-group">
			<div class="row">
				<div class="col-sm-2">
					<label class="control-label">Alamat </label>
				</div>
				<div class="col-sm-4"> 
					<?=(isset($invoice['alamat']) ? $invoice['alamat'] : '')?>
				</div>
				<div class="col-sm-2">
					<label class="control-label">Total </label>
				</div>
				<div class="col-sm-4"> 
					RP. <?=(isset($total) ? number_format($total) : '')?>
				</div>
			</div>
			<div class="row">
				<div class="col-sm-6">
					<label class="control-label"> </label>
				</div>
				<div class="col-sm-2">
					<label class="control-label">Sudah Dibayarkan </label>
				</div>
				<div class="col-sm-4"> 
					RP. <?=(isset($bayar) ? number_format($bayar) : '')?>
				</div>
			</div>
			<div class="row">
				<div class="col-sm-6">
					<label class="control-label"> </label>
				</div>
				<div class="col-sm-2">
					<label class="control-label">Sisa Pelunasan </label>
				</div>
				<div class="col-sm-4"> 
					RP. <?=(isset($sisa) ? number_format($sisa) : '')?>
				</div>
			</div>
		</div>	 
		 
	</div>   
</div> 
<h3 class="page-header">
	Form Pembayaran
</h3>
<div class="panel panel-default">
	<div class="panel-heading">
		<div class="row">
			<div class='col-sm-6'>Form Pembayaran</div>
			<div class='col-sm-6' align='right'>

			</div>
		</div>
	</div>
	<?php
	if(isset($_GET['s'])){
		switch($_GET['s']){
			case "true" : $c="success"; break;
			case "false": $c="danger"; break;
		}
			echo"
			<br>
			<div class='container' style='width:100%;'><div class='row'><div class='col-md-12'>
			<div class='alert alert-$c' role='alert'>
				<span class='glyphicon glyphicon-exclamation-sign' aria-hidden='true'></span>
				<span class='sr-only'>Error:</span>
				$_GET[m]
			</div></div></div>
			</div>
			";
	}
	?>	
	<form action="<?=base_url()?>cms/logic/bayarProses" method="post" data-parsley-validate enctype="multipart/form-data">
		<div class="panel-body"> 
			<input type="hidden" name="id_invoice" value="<?=$_GET['_id']?>" />
			<?php if(isset($_GET['bayar']) && $_GET['bayar'] != ''):
				$databayar = $this->model_global->get_data(array('data' => 'row','table' => 'bayar', 'where' => array('status !=' => -1, 'id_bayar' => $_GET['bayar'])));
			?>
				<input type="hidden" name="_id" value="<?=$_GET['bayar']?>" />
				<input type="hidden" name="action" value="edit" /> 
			<?php endif; ?>  
				<div class="form-group">
					<div class="row">
						<div class="col-sm-2">
							<label class="control-label">Terima Dari<span class="text-danger">*</span></label>
						</div>
						<div class="col-sm-3"> 
							<input type='text' class='form-control'  name='dari' value='<?=(isset($databayar['dari']) ? $databayar['dari'] : '')?>' required>
						</div>
					</div>
				</div>
				<div class="form-group">
					<div class="row">
						<div class="col-sm-2">
							<label class="control-label">Tanggal<span class="text-danger">*</span></label>
						</div>
						<div class="col-sm-3"> 
							<input type='text' class='form-control datepicker' data-date-format='yyyy-mm-dd'  name='tanggal' value='<?=(isset($databayar['tanggal']) ? $databayar['tanggal'] : date("Y-m-d"))?>'  required>
						</div>
					</div>
				</div>
				<div class="form-group">
					<div class="row">
						<div class="col-sm-2">
							<label class="control-label">Untuk Pembayaran </label>
						</div>
						<div class="col-sm-7"> 
							<textarea class='form-control tinymce-editoradv' rows='3' name='deskripsi'  ><?=(isset($databayar['deskripsi']) ? $databayar['deskripsi'] : '')?></textarea>
							
						</div>
					</div>
				</div>			
				<div class="form-group">
					<div class="row">
						<div class="col-sm-2">
							<label class="control-label">Jumlah </label>
						</div>
						<div class="col-sm-4"> 
							<input type='text' class='form-control angka' id='total' name='total' value='<?=(isset($databayar['total']) ? $databayar['total'] : $sisa)?>' required>
						</div>
					</div>
				</div> 
			 
		</div> 
		<div class="panel-footer">
			<button type="submit" onclick=" " class="btn btn-primary"><?php if(isset($_GET['bayar']) && $_GET['bayar'] != ''){ echo "Update";}else{echo "Tambah";} ?></button>
			<?php if(isset($_GET['bayar']) && $_GET['bayar'] != ''):?>
				<a href='<?=$url?>invoice-bayar?_id=<?=$_GET['_id']?>' class='btn btn-warning'>Batal</a> 
			<?php endif?>
			<a href='<?=$url?>invoice' class='btn btn-default'>Back</a> 
		</div>
	</form>
</div> 
<div class="panel panel-default">
	<div class="panel-heading">
		<div class="row">
			<div class='col-sm-6'><?php echo $judul;?> List</div> 
		</div>
	</div>
	<div class="panel-body">
		<div class="table-responsive">
			<table class="table table-striped table-bordered table-hover dataTables invoiceTables" >
				<thead>
					<tr>
						<th width='35'>No</th>
						<th width=''>Terima Dari</th>
						<th width=''>Untuk Pembayaran</th>
						<th width='100'>Tanggal</th>  
						<th width='110'>Total</th>
						<th width='80'></th>
					</tr>
				</thead>
				<tbody>
					<?php
						$no=1;
						if(isset($data) && count($data) > 0): foreach($data as $row):  
						$now = @$this->uri->segment(3); 

					?>
					<tr>
						<td align='center'><?=$no;?></td>
						<td align='left'><?=$row['dari'];?></td>
						<td align='left'><?=$row['deskripsi'];?></td>
						<td align='left'><?=$row['tanggal'];?></td>  
						<td align='left'>Rp. <?=number_format($row['total']);?></td> 
						<td align='center'>
							<a title="Edit" href="<?=$url?>invoice-bayar?_id=<?=$_GET['_id']?>&bayar=<?=$row['id_bayar']?>" class='btn btn-xs btn-warning '><i class="fa fa-edit "></i></a>
							<a title="Print Kwitansi" href="<?=base_url()?>cetakkwitansi/<?=$row['id_bayar']?>" 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/bayar/<?=$row['id_bayar']?>?part=<?=$now."?_id=".$_GET['_id'];?>" class="text-danger"><i class="fa fa-trash-o"></i></a>					
						</td>
					</tr>
					<?php $no++; endforeach; endif; ?>
				</tbody>
			</table>
		</div>
	</div>
</div>