<?php
    include('loginsession.php');
    error_reporting(E_ALL);
    $filename = basename(__FILE__);
    $save = mysqli_query($conn, "INSERT INTO novia_abc_user_logs (userid,filename) VALUES ('$userini_id','$filename')");
    
//-------------
    // ambil halaman dari URL, default = 1
    $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
    $limit = 100;
    $offset = ($page - 1) * $limit;
    
//-------------
    $cari ='';
    if(isset($_GET['cari'])){
    	$cari = $_GET['cari'];
    }
    $pointacc = 0;
    $point = "SELECT * FROM novia_abc_user_logs WHERE userid = '$userini_id' ORDER BY tanggal DESC";
    $result = $conn->query($point);
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
    		$pointacc ++;
    	}
    }
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//ID" "http://www.w3.org/TR/html4/frameset.dtd">
<html lang="id">
<head>
	<title>LACAK PASIEN</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="css/style.css">
	<link href="images/balance.ico" rel="icon" type="image/icon" />
	<style>
		input[type=text],input[type=password]
		{
		width:60%;
		padding:8px;
		margin-top:8px;
		margin-bottom:8px;
		border:1px solid #ccc;
		padding-left:5px;
		font-size:18px;
		}
		input[type=submit]
		{
		width:35%;
		background-color:#8baca4;
		color:#FFFFFF;
		border:2px solid #8baca4;
		padding:6px;
		font-size:18px;
		cursor:pointer;
		border-radius:5px;
		}
		img {
            width: 100%;
            max-width: 500px;
            height: auto;
        }
    </style>	
</head>

<body>
    
    <div><img src="images/logo_blue.png" alt="Deskripsi Gambar"></div>

    <table width="100%" cellpadding="5">
		<tr>
		    <td Height=39px align="center"; ><a style = "font-size:20px; font-family: Times New Roman; text-transform:uppercase; color:#bbb;"><?php echo $userini_name; ?><a>
		        <a style = "font-size:18px; font-family: Arial; color:#aaa;">( Point - <?php echo $pointacc ?> )<a>
		</tr>
	</table>
	
    <table width="100%" cellpadding="5">
		<tr class="dua">
    		<td><a href="biodata-new.php" title="Masukkan data pasien baru ke database"><button style="padding : 10px; width:100%; background-color:#1baca4;"> DAFTAR BARU </button></a></td>
    		<td width="34%"><a href="cash/cash-view-daily.php?tanggal=<?php echo date("Y-m-d");?>" title="Lihat Pengeluaran Hari ini"><button style="padding : 10px; width:100%; background-color:#8baca4;">KEUANGAN</button></a></td>
		    <td width="33%"><a href="administrasi.php" title="Pusat Navigasi Data"><button style="padding : 10px; width:100%; background-color:#A6B7C1;"> NAVIGASI </button></a></td>
		</tr>
	</table>
	
	<table width="100%" cellpadding="5">
		<tr class="dua">
	    <td><a href="visit-last.php" title="Terakhir datang"><button style="padding : 10px; width:100%; background-color:#A6B7C1;">KUNJUNGAN TERBARU</button></a></td>
		<td><a href="visit-daily.php?tanggal=<?php echo date("Y-m-d");?>" title="Lihat Pasien Hari ini"><button style="padding : 10px; width:100%; background-color:#8baca4;"> PASIEN HARI INI </button></a></td>
		</tr>
	</table>

    <table width="100%" cellpadding="5">
		<tr class="dua">
		</tr>
	</table>

	<table width="100%" cellpadding="5">
		<tr class="dua">
			<td colspan="4" style="padding-top : 10px;">
				<form action="index.php" method="get" align="center">
					<input type="text" name="cari" size="15" maxlength="33" placeholder=<?php echo $cari ?>>
					<input type="submit" value="Lacak">
				</form>
			</td>
		</tr>
	</table>

    <table width="900" cellpadding="5">
    	<tr class="head">
    		<th width="30">MR</th>
    		<th width="160">Nama</th>
    		<th width="10">F</th>
    		<th width="60">Telepon</th>
    		<th>Alamat</th>
    		<th width="80">Kelurahan</th>
    		<th width="80">Kecamatan</th>
    		<th width="80">Kota</th>
    	</tr>
    
        <?php 
        $no = 1;
        $sty = 1;
        $sql = "SELECT * FROM novia_abc_biodata 
                WHERE name LIKE '%$cari%' 
                   OR mr LIKE '%$cari%' 
                   OR phone_wa LIKE '%$cari%' 
                   OR phone_home LIKE '%$cari%' 
                   OR street_address LIKE '%$cari%' 
                ORDER BY mr DESC 
                LIMIT $offset, $limit";
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
        		$no++;
        		$y = 2;
        		$result1 = fmod($no,$y);
        		if ( $result1 == 0 ) $sty = "dua";
        		if ( $result1 == 1 ) $sty = "satu";
        		
    			//Script Umur
        		    $yer = new DateTime();
        		    $yar = $row["birthday"];
        		    $yur = new DateTime($yar);
        		    $yor = $yer->diff($yur);
        		    $mer = $yor->y;
        		    $mur = " (".$mer.")";
        			if ($mer > 150) $mur = "";
        		//--------
        		
        		?>
        		<tr class=<?php echo $sty; ?>>
        			<td width="30" align="center"><a href="biodata.php?id=<?php echo $row["id"];?>" title="Lihat detail pasien"><?php echo $row["mr"]; ?></a></td>
        			<td width="160"><a href="biodata.php?id=<?php echo $row["id"];?>" title="Lihat detail pasien"><?php echo $row["name"].$mur; ?></a></td>
        			<td width="10" align="center"><a  href="biodata.php?id=<?php echo $row["id"];?>" title="Lihat detail pasien"><?php echo $row["freq"]; ?></a></td>
        			<td width="60"><a href="https://wa.me/<?php echo $row["phone_wa"]; ?>"><?php echo $row["phone_wa"]; ?></a></td>
        			<td><a href="biodata.php?id=<?php echo $row["id"];?>" title="Lihat detail pasien"><?php echo $row["street_address"]; ?></a></td>
        			<td width="80"><a href="biodata.php?id=<?php echo $row["id"];?>" title="Lihat detail pasien"><?php echo $row["kelurahan"]; ?></a></td>
        			<td width="80"><a href="biodata.php?id=<?php echo $row["id"];?>" title="Lihat detail pasien"><?php echo $row["kecamatan"]; ?></a></td>
        			<td width="80"><a href="biodata.php?id=<?php echo $row["id"];?>" title="Lihat detail pasien"><?php echo $row["city"]; ?></a></td>
        		</tr>
        		<?php
            }
        }
        ?>
    </table>
    <table>
    <tr class="dua">
    <td> 
    <a style = "font-size:20px; font-family: Times New Roman; text-transform:uppercase; color:#bbb;">        

        <?php

// hitung total data
$sql_count = "SELECT COUNT(*) as total FROM novia_abc_biodata 
              WHERE name LIKE '%$cari%' 
                 OR mr LIKE '%$cari%' 
                 OR phone_wa LIKE '%$cari%' 
                 OR phone_home LIKE '%$cari%' 
                 OR street_address LIKE '%$cari%'";
$count_result = $conn->query($sql_count);
$total_rows = $count_result->fetch_assoc()['total'];

$total_pages = ceil($total_rows / $limit);

// navigasi halaman
echo "<div style='margin-top:20px;'>";

// tombol previous
if ($page > 1) {
    echo "<a href='?page=" . ($page - 1) . "&cari=$cari' style='font-size:22px;'><strong>Previous</strong></a> ";
}

// nomor halaman
for ($i = 1; $i <= $total_pages; $i++) {
    if ($i == $page) {
        //echo "<strong>$i</strong> "; // halaman aktif ditebalkan
        echo "<a style='font-size:22px;'><strong>$i</strong></a> ";
    } else {
        echo "<a href='?page=$i&cari=$cari' style='font-size:18px;'>$i</a> ";
    }
} 

// tombol next
if ($page < $total_pages) {
    echo "<a href='?page=" . ($page + 1) . "&cari=$cari'style='font-size:22px;'><strong>Next</strong></a>";
}

echo "</div>";
?>
        </a>
    </td>
</tr>        
    </table>
</body>
</html>