<?php
	$now = @$this->uri->segment(3);
	$cat = substr($now,-1);
?>
<style>
	label{
		cursor:pointer;
	}
	.daterangepicker .input-mini{
		width:100px;
		padding:0px;	s
	}
	.daterangepicker .calendar{
		max-width:320px;
	}
	.ranges{
		clear:both;
	}
 
</style> 
<?php if(isset($_POST['search'])){ }else{?>
<div class="panel panel-default">
<div class="panel-heading">
	<div class="row">
		<div class='col-sm-6'><?php echo $judul;?></div>
		<div class='col-sm-6' align='right'>
		</div>
	</div>
</div>
<div class="panel-body"> 
		<form  method="post" data-parsley-validate enctype="multipart/form-data">
			<?php $response = $this->session->flashdata('response'); if(isset($response["status"]) && $response["status"] == "error"): ?>
				<div class="alert alert-warning">
					<?=$response["message"]?>
				</div>
			<?php endif; ?>		
			<div class="form-group">
				<div class="row">
					<div class="col-sm-2">
						<label class="control-label"><label><input type='radio' name='kapan' value='1' <?=(isset($_POST['kapan']) ? (($_POST['kapan']=="1") ? "checked" : "" ) : "checked")?>> Daily</label><span class="text-danger">*</span></label>
					</div>
					<div class="col-sm-3"> 
					<?php
						$v = date("Y-m-d")." - ".date("Y-m-d");
						if(isset($_POST['daily'])){
							$v = $_POST['daily'];
						}
					?>
						<input type='text' class='form-control tgl' name='daily' value='<?=$v?>'>
					</div>
				</div>
			</div>     
			<div class="form-group">
				<div class="row">
					<div class="col-sm-2">
						<label class="control-label"><label><input type='radio' name='kapan' value='2'<?=(isset($_POST['kapan']) ? (($_POST['kapan']=="2") ? "checked" : "" ) : "")?>> Monthly</label><span class="text-danger">*</span></label>
					</div>
					<div class="col-sm-1"> 
						<select class='form-control' name='mm1'>
							<option value=''>BLN</option>
							<?php
								foreach($bulanradio as $b=>$v){
									$b = $b+1;
									if(strlen($b)==1){
										$b = "0".$b;
									}
									$s="";
									if(isset($_POST['mm1'])){
										if($b==$_POST['mm1']){
											$s = "selected";
										}
									}
									echo "<option value='$b' $s>$v</option>";
								}
							?>
						</select>
					</div>
					<div class="col-sm-1"> 
						<select class='form-control' name='mt1'>
							<option value=''>THN</option>
							<?php
								for($y=date("Y")-1;$y<=date("Y");$y++){
									$s="";
									if(isset($_POST['mt1'])){
										if($y==$_POST['mt1']){
											$s = "selected";
										}
									}
									echo "<option value='$y' $s>$y</option>";
								}
							?>
						</select>
					</div>
					<div class="col-sm-1" style='padding-top:8px;' align='center'> 
						S/D
					</div>
					<div class="col-sm-1"> 
						<select class='form-control' name='mm2'>
							<option value=''>BLN</option>
							<?php
								foreach($bulanradio as $b=>$v){
									$b = $b+1;
									if(strlen($b)==1){
										$b = "0".$b;
									}
									$s="";
									if(isset($_POST['mm2'])){
										if($b==$_POST['mm2']){
											$s = "selected";
										}
									}
									echo "<option value='$b' $s>$v</option>";
								}
							?>
						</select>
					</div>
					<div class="col-sm-1"> 
						<select class='form-control' name='mt2'>
							<option value=''>THN</option>
							<?php
								for($y=date("Y")-1;$y<=date("Y");$y++){
									$s="";
									if(isset($_POST['mt2'])){
										if($y==$_POST['mt2']){
											$s = "selected";
										}
									}
									echo "<option value='$y' $s>$y</option>";
								}
							?>
						</select>
					</div>
				</div>
			</div>    
			<div class="form-group">
				<div class="row">
					<div class="col-sm-2">
						<label class="control-label">Distributor <span class="text-danger">*</span></label>
					</div>
					<div class="col-sm-3"> 
						<select name="distributor" class="form-control"  >
							<option value="">ALL</option>
							<?php  foreach ($distributor as $value) { ?>
								<option value="<?=$value['id_supplier']?>" <?=(isset($_POST['distributor']) ? (($_POST['distributor']==$value['id_supplier']) ? "selected" : "") : "")?> ><?=$value['nama']?></option>  
							<?php } ?>
						</select>
					</div>
				</div>
			</div>	
			<div class="form-group">
				<div class="row">
					<div class="col-sm-2">
						 
					</div>
					<div class="col-sm-3"> 
						<button type='submit' name='search' class='btn btn-md btn-success'>Submit</button>
					</div>
				</div>
			</div>	
		</form>	
</div>
</div>
<?php }?>
<?php if(isset($_POST['search'])){ ?>
<h3 class="page-header">
	Hasil Laporan Pembelian
</h3>
<div class="panel panel-default">
	<div class="panel-heading">
		<div class="row"> 
			<div class='col-sm-12' align='left'>
				<!--<a href='javascript:void(0);' class='btn btn-sm btn-primary'><i class='fa fa-file-excel-o'></i> Export</a>-->
				<a href='javascript:void(0);' class='btn btn-sm btn-primary btn-detil' id='btn-detil'><i class='fa fa-th'></i> Detail</a>
				<a href='javascript:void(0);' class='btn btn-sm btn-success btn-graph' id='btn-graph'><i class='fa fa-bar-chart'></i> Grapik</a>
				<?php if(isset($result) && count($result) > 0){;?>
				<a href='<?=$linklaporanexcel;?>' target='_blank' class='btn btn-sm btn-default ' ><i class='fa fa-file-excel-o'></i> Excel</a>
				<a href='<?=$linklaporan;?>' target='_blank' class='btn btn-sm btn-danger ' ><i class='fa fa-print'></i> Print</a>
				<?php }?>
			</div>
		</div>
	</div>
	<div class="panel-body">
		
		<div class="table-responsive" id='detil'>
		
			<table class="table table-striped table-bordered table-hover  " >
				<thead>
					<tr>
						<th width='35'>No</th>
						<th width='130'>Tgl. Faktur</th> 
						<th width='130'>Tgl. Jatuh Tempo</th> 
						<th width='80'>Code Obat</th> 
						<th width=''>Nama Obat</th> 
						<th width='200'>Kategori</th> 
						<th width='100'>Qty</th> 
						<th width='100'>Harga Awal</th> 
						<th width='100'>Harga Beli</th> 
						<th width='100'>Harga Rata²</th> 
						<th width='100'>Jum. Beli</th> 
						<th width='50'>Stok</th> 
					</tr>
				</thead>
				<tbody>
				<?php
					$no=1; 
					
					$tqty=0;
					$tstok=0;
					$thb=0;
					$thj=0; 
					$tb=0; 
					$tob=0; 
					if(isset($result) && count($result) > 0){ foreach($result as $row){
 
						$totbeli = $row['harga'] * $row['qty'] ;
						$tob+=$totbeli;
						$totjual+=$row['total'];
						 
						$t = explode(" ",$row['tanggal']); 
						$t2 = explode("-",$t[0]);
						$tgl = $t2[2]."/".$t2[1]."/".$t2[0] ;
						$t = explode(" ",$row['jatuh_tempo']); 
						$t2 = explode("-",$t[0]);
						$tglt = $t2[2]."/".$t2[1]."/".$t2[0]  ;
						$tqty = $tqty + (int)$row['qty'];
						$thb = $thb + (int)$row['harga_awal'];
						$tb = $tb + (int)$row['avg'];
						$thj = $thj + (int)$row['harga'];
						$tstok = $tstok + (int)$row['stok'];
				?>
				<tr>
					<td align='center'><?=$no;?></td>
					<td><?=$tgl;?></td>
					<td><?=$tglt;?></td>
					<td><?=$row['kode'];?></td>
					<td><?=$row['nama'];?></td>
					<td><?=$row['kategori'];?></td>
					<td><?=(int)$row['qty'];?> <?=$row['satuan'];?></td>
					<td>Rp. <?=number_format($row['harga_awal']);?></td>
					<td>Rp. <?=number_format($row['harga']);?></td>
					<td>Rp. <?=number_format($row['avg']);?></td>
					<td>Rp. <?=number_format($totbeli);?></td>
					<td><?=$row['stok'];?></td>
				</tr>
				<?php
					$no++;}}
				?>
				<tr>
					<td align='right' colspan='6'>TOTAL</td> 
					<td><?=(int)$tqty;?></td>
					<td>Rp. <?=number_format($thb);?></td>
					<td>Rp. <?=number_format($thj);?></td> 
					<td>Rp. <?=number_format($tb);?></td>
					<td>Rp. <?=number_format($tob);?></td>
					<td><?=$tstok;?></td>
				</tr>
			</tbody>
			</table>
			<?php if(isset($result) && count($result) > 0){?>
			<hr>	
			<table class='table'>
				<tr> 
					<th width='150'>Distributor</th>
					<th><?=$distributorvalue;?></th>
				</tr>
				<tr> 
					<th>Periode</th>
					<th><?=$periodevalue;?></th>
				</tr>
				<tr> 
					<th >Jumlah Pembelian</th>
					<th>Rp. <?=number_format($totjual);?></th>
				</tr> 
			</table>
			<?php }?>
		</div> 
		<div  id='graph'>
		<?php if(isset($resultgraph) && count($resultgraph) > 0){?>
			<canvas id="myChart" width="400" height="100"></canvas>
			<hr>	
			<table class='table'>
				<tr> 
					<th width='150'>Distributor</th>
					<th><?=$distributorvalue;?></th>
				</tr>
				<tr> 
					<th>Periode</th>
					<th><?=$periodevalue;?></th>
				</tr>
				<tr> 
					<th >Jumlah Pembelian</th>
					<th>Rp. <?=number_format($totjual);?></th>
				</tr> 
			</table>			
		<?php }?>
		</div>
		
	</div>
</div>

<?php }?>