<?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";    
?>
        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12">
                     <h2>Registration</h2>                          
                    </div>
                </div>
            <hr />
<?
    // $pin = mysqli_real_escape_string($con, $_GET['pin']);
    $pin = mysqli_real_escape_string($con, $_GET['pin'] ?? '');

	
	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">
                           Unused 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>Date / Time</th>
                                            <th>Status</th>
                                        </tr>
                                    </thead>
                                    <tbody>
<?php
$sql = "SELECT no, datesold FROM bestpin WHERE userid = ".$_SESSION['aidi']." AND status = 0";
$result = mysqli_query($con, $sql);
$c = 1;
while ($row = mysqli_fetch_array($result)) {
?>
                                        <tr>
                                            <td><?= $c++ ?></td>
                                            <td><?= $row[0] ?></td>
                                            <td><?= $row[1] ?></td>
                                            <td><a href="registration.php?pin=<?= $row[0] ?>" class="btn btn-success btn-xs">Registration</a></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>

<?php
            // Check Number Sponsor - Limit Sponsor (Limitkan Sponsor) 
            
            $s_limit_sql = mysqli_fetch_array(mysqli_query($con, "SELECT sponsor_limit FROM setting"));
            $s_limit = $s_limit_sql['sponsor_limit'];
            
            $s_num_sql = mysqli_num_rows(mysqli_query($con, "SELECT id FROM ahli WHERE penaja = '".$_SESSION['aidi']."'"));
            $s_num = $s_num_sql;

            if ($s_limit == 0) {
                $dis = "";
            }
            else    {
                if ($s_num >= $s_limit) {
                    $dis = "disabled";
                    noTis("danger", "Notice ", " Your account reach a maximum sponsor limit."); 
                }   
                else {
                    $dis = "";
                } 
            }
            
            
            
            
            
           

?>
			<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 class="form-label"><strong>Select Package</strong></label>
                                            <div style="display:flex; gap:15px; flex-wrap:wrap;">
                            
                                            <?php
                                            $sql = "SELECT * FROM packages ORDER BY price ASC";
                                            $result = mysqli_query($con, $sql);
                            
                                            while ($p = mysqli_fetch_assoc($result)) {
                                                echo '
                                                <label style="flex:1; cursor:pointer; border:2px solid #eee; border-radius:10px; padding:15px; text-align:center;">
                                                    <input type="radio" name="i" value="'.$p['id'].'" required>
                                                    
                                                    <!-- Package Name -->
                                                    <div style="font-weight:bold; font-size:16px; margin-bottom:8px; color:#333;">'.$p['name'].'</div>
                                                    
                                                    <!-- Logo -->
                                                    <div style="color:#555;">
                                                        <img src="assets2/ico/tls2u.png" height="55px" width="auto" alt="TLS2U.COM">
                                                    </div>
                                                    
                                                    <!-- Quantity -->
                                                    <div style="font-weight:bold;">x '.$p['qty_set'].' pcs</div>
                                                    
                                                    <!-- Price -->
                                                    <div style="color:#555;">RM '.number_format($p['price'], 2).'</div>
                                                </label>';
                                            }
                                            ?>
                            
                                            </div>
                                        </div>
                                        
                                        <div class="form-group">
                                            <label>Name</label>
                                            <input class="form-control" name="a" placeholder="Full Name" required <?= $dis; ?>/>
                                        </div>
                                        <div class="form-group">
                                            <label>Phone</label>
                                            <input class="form-control" name="b" placeholder="your mobile phone no." required <?= $dis; ?>/>
                                        </div>
                                        <div class="form-group">
                                            <label>Email</label>
                                            <input class="form-control" type="email" name="c" placeholder="valid email only" required <?= $dis; ?>/>
                                        </div>
                                        <div class="form-group">
                                            <label>MyKad</label>
                                            <input class="form-control" name="d" placeholder="your MyKad no." required <?= $dis; ?>/>
                                        </div>
                                        <div class="form-group">
                                            <label>Username</label>
                                            <input class="form-control" name="e" id="txtUsername" required <?= $dis; ?>/>
                                            <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" <?= $dis; ?>>Register</button>

                                    </form>

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

			</div>
			
			
<?
	}	
?>

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