<?php
session_start();
include "config/conn.php";
if($_POST['btncari']=="Search") {
	$tglf=$_POST['tglf'];
	$tgls=$_POST['tgls'];
} else { 
	$tglf= date('Y-m-d');
	$tgls = date('Y-m-d',strtotime('-1 day',strtotime($tglf))) ;
}
$sql="SELECT
			b.id_pulsa,
			c.nama_bbws,
			a.nama_lokasi,
			a.ReceivedDate,
			b.awal_isi_pulsa,
			b.nominal,
			(150*24) AS biaya_perhari,
			format((b.nominal/(150*24)),0) AS perkiraan_hari,
			ADDDATE(b.awal_isi_pulsa,(b.nominal/(150*24))) AS batas_akhir_pulsa,
			DATEDIFF(ADDDATE(b.awal_isi_pulsa,(b.nominal/(150*24))),now()) AS limithari
		FROM
			m_lokasi AS a
			INNER JOIN m_pulsa AS b ON a.id_lokasi = b.id_lokasi
			INNER JOIN m_bbws AS c ON c.id_bbws = b.id_bbws
		WHERE 1=1 AND a.id_bbws=".$_SESSION['id_bbws']."
		ORDER BY DATEDIFF(ADDDATE(b.awal_isi_pulsa,(b.nominal/(150*24))),now()) ASC ";
$query=mysql_query($sql);

if($_POST['simpan']=="Simpan"){
		$idpulsa=strip_tags($_POST['idpulsa']);
		$ibbws=trim($_POST['ibbws']);
		$id_lokasi=strip_tags($_POST['id_lokasi']);
		$awal_isi_pulsa=$_POST['tgls'];
		$nominal=strip_tags($_POST['nominal']);
		if ($idpulsa=="") {
		   	// INSERT m_lokasi
			$sqlinsertmpulsa="INSERT INTO m_pulsa values('','$ibbws','$id_lokasi','$awal_isi_pulsa','$nominal')";
		    $query=mysql_query($sqlinsertmpulsa);
			if($query){ 
				echo"<script>alert('Pengisian pulsa berhasil !');history.go(-1);</script>";
			}
		} else {
		$sql="UPDATE m_pulsa SET nominal='$nominal'	 WHERE id_pulsa='$idpulsa'";			
		$query=mysql_query($sql);
			if($query){
				echo"<script>alert('Update nominal pulsa berhasil !');history.go(-1);</script>";
			}
		}
}

?>

		<article class="module width_4_quarter">
		<header>
		<h3 class="tabs_involved" style="background-image:url(images/icons/stats_lines.png); background-repeat:no-repeat; padding-left:20px; margin-left:5px;">STATUS PULSA PERANGKAT</h3>
		<form action="" method="post">
        <ul class="tabs">
        	<li><!--Periode : <input type="text" id="tgls" name="tgls" value="<?php echo date('Y-m-d',strtotime($tgls));?>" size="10" /> s/d <input type="text" id="tglf" name="tglf" value="<?php echo date('Y-m-d',strtotime($tglf));?>" size="10" /-->
            	<input type="button" id="btntambah" name="btntambah" value="Tambah" onclick="tambah()" />	
            </li>
        </ul>
        </form>
		</header>
		<div class="tab_container">
        <div id="forminputpulsa">
        		<table class="tablesorter" cellspacing="0" border="1">
                                   <thead>
                                        <tr>
                                            <th>#</th>
                                            <th>Nama Lokasi</th>
                                            <th>Update Terakhir Data</th>
                                            <th>Awal Pengisian Pulsa</th>
                                            <th>Nominal</th>
                                            <th>Asumsi Biaya/Hari</th>
                                            <th>Perkiraan Waktu (Hari)</th>
                                            <th>Batas Akhir Pulsa</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
									<?php
                                    $i=0;
                                    while($row=mysql_fetch_array($query)){
                                    $i++;
                                    ?>
                                    <tr>
                                    	<td><?php 
											if($row['limithari']>0) { echo "<img src='images/icon_online2.png' />"; } 
											else { echo "<img src='images/icon_offline2.png' />"; }?></td>
                                    	<td><?php echo $row['nama_lokasi'];?></td>
                                    	<td><?php echo date('d M Y',strtotime($row['ReceivedDate']));?></td>
                                    	<td><?php echo date('d M Y',strtotime($row['awal_isi_pulsa']));?></td>
                                    	<td>Rp. <?php echo number_format($row['nominal'],0);?></td>
                                    	<td>Rp. <?php echo number_format($row['biaya_perhari'],0);?> (Rp.150 x 24 Jam)</td>
                                    	<td> 
                                        <?php if($row['limithari']>0) { echo $row['perkiraan_hari']." Hari ( <strong>".$row['limithari']." Lagi</strong> )"; } 
											else { echo "<font color='red' style='text-decoration:line-through;'>".$row['perkiraan_hari']." Hari</font>"; }
										?>
                                        </td>
                                    	<td><?php echo date('d M Y',strtotime($row['batas_akhir_pulsa']));?></td>
                                    	<td>
                                        <a href="#" onclick="tambah(<?php echo $row['id_pulsa'];?>)"><img src="images/icons/page_white_edit.png"/></a>
                                        </td>
                                    </tr>
                                    <?php }?>
                                </table>
			</div>
		</div><!-- end of .tab_container -->
		</article><!-- end of content manager article -->        
<script src="js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="js/manpulsa.js"></script>