<?php
session_start();
$key = md5(date("Y-m-d").":".$_SERVER['REMOTE_ADDR']);
if (isset($_SESSION['aW1iZXN0cmEuY29tLm15']) <> $key) {
    header("location:".$_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME']); // cara baru !
} else {
    include "_header.php";
    include "_navtop.php";
    include "_navside.php";
    include "_functions.php";
    require "conn.php";    
    
    $pin = mysqli_real_escape_string($con, $_GET['pin']);
    $cat = mysqli_real_escape_string($con, $_GET['x']);

    switch ($cat) {
        case "unused":
        $jenis = "Available";
        $status = 0;
        break;

        case "used":
        $jenis = "Used";
        $status = 1;
        break;
    }
?>
        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12">
                     <h2>PIN History</h2>                          
                    </div>
                </div>
            <hr />
<?	
	if (empty($pin)) {
?>
                <div class="row" >

                    <div class="col-md-12 col-sm-12 col-xs-12">

                    <div class="panel panel-default">
                        <div class="panel-heading">
                           <?= $jenis ?> PIN
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th>#</th>
                                            <th>No</th>
                                            <th>Generated Date / Time</th>
                                            <? if ($status == 1) { ?><th>Activate Date / Time</th><? } ?>
                                            <th>Status</th>
                                        </tr>
                                    </thead>
                                    <tbody>
<?php



 if ($_SESSION['username'] == "admin") { 
            $sql = "SELECT * FROM bestpin WHERE status = '$status' ORDER BY no, datesold, dateactive ASC";
 }  else {
            $sql = "SELECT * FROM bestpin WHERE userid = '".$_SESSION['aidi']."' AND status='$status' ORDER BY no, datesold, dateactive DESC";
 }



$result = mysqli_query($con, $sql);
$c = 1;
while ($row = mysqli_fetch_array($result)) {
?>
                                        <tr>
                                            <td><?= $c++ ?></td>
                                            <td><?= $row[1] ?></td>
                                            <td><?= $row[4] ?></td>
<?          if ($status == 1) { ?>          <td><?= $row[5] ?></td>     <? } ?>
<?          if ($status == 0) { ?>
                                            <td><a href="registration.php?pin=<?= $row[0] ?>" class="btn btn-success btn-xs">Registration</a></td>
<?          } ?>
<?          if ($status == 1) { ?>
                                            <td><span class="btn btn-warning btn-xs">Used</span></td>
<?          } ?>
                                        </tr>
<? } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>

                    </div>
                </div>
<?
	} else {

		if (chkPin($pin) <> 1) {
			echo '<div class="alert alert-danger">';
			echo 'Error! Wrong PIN.';
			echo '</div>';
			goto kaki;
		}		
?>
<script type="text/javascript">
    function ValidateUsername() {
        var username = document.getElementById("txtUsername").value;
        var lblError = document.getElementById("lblError");
        lblError.innerHTML = "";
        var expr = /^[a-zA-Z0-9_]*$/;
        if (!expr.test(username)) {
            lblError.innerHTML = "Only Alphabets, Numbers, Underscore allowed in Username.";
            event.preventDefault(); // tambahan
        }
    }
</script>
			<div class="row">
                <div class="col-md-12">
                    <!-- Form Elements -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            Register New Member
                        </div>
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <form role="form" method="post" action="register.php" onsubmit="return ValidateUsername();">

                                        <div class="form-group">
                                            <label>Name</label>
                                            <input class="form-control" name="a" placeholder="Full Name" required />
                                        </div>
                                        <div class="form-group">
                                            <label>Phone</label>
                                            <input class="form-control" name="b" placeholder="your mobile phone no." required />
                                        </div>
                                        <div class="form-group">
                                            <label>Email</label>
                                            <input class="form-control" type="email" name="c" placeholder="valid email only" required />
                                        </div>
                                        <div class="form-group">
                                            <label>MyKad</label>
                                            <input class="form-control" name="d" placeholder="your MyKad no." required />
                                        </div>
                                        <div class="form-group">
                                            <label>Username</label>
                                            <input class="form-control" name="e" id="txtUsername" required />
                                            <span id="lblError" style="color: red"></span>
                                        </div>
                                        <div class="form-group">
                                            <label>PIN</label>
                                            <input class="form-control" name="g" value="<?= $pin ?>" readonly />
                                        </div>

                                        <button type="submit" id="btnValidate" name="h" class="btn btn-primary">Register</button>

                                    </form>

                                </div>
                            </div>
                        </div>
                    </div>
                     <!-- End Form Elements -->
                </div>

			</div>
<?
	}	
?>

             <!-- /. PAGE INNER  -->
<? kaki: ?>			 
            </div>
         <!-- /. PAGE WRAPPER  -->
        </div>
<?
    include "_footer.php";
}
?>
