<?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";	

    if (isset($_POST['h'])) {

        $nama = mysqli_real_escape_string($con, $_POST['a']);
        $telefon = mysqli_real_escape_string($con, $_POST['b']);
        $email = mysqli_real_escape_string($con, $_POST['c']);
        $mykad = mysqli_real_escape_string($con, $_POST['d']);
        $usrnm = mysqli_real_escape_string($con, $_POST['e']);
        // $pswd = mysqli_real_escape_string($con, $_POST['f']); // removed
		$pin = mysqli_real_escape_string($con, $_POST['g']);

		$pswd = randomPass();
		$penaja = $_SESSION['aidi'];
		$pakej = 10;
		
		date_default_timezone_set("Asia/Kuala_Lumpur");
		$tarikh = date("Y-m-d H:i:s");
?>
        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12">
                     <h2>Registration</h2>                          
                    </div>
                </div>
<?
		if (chkPin($pin) == 0) {
			noTis ("danger", "Error", "Wrong PIN");
			goto kaki;
		}

		if (reKod("status", "bestpin", "no", $pin) == 1) {
			
			//pin telah digunakan
			noTis ("danger", "Error", "PIN number has expired.");

		} else {

			// check username
			if (chkUsr($usrnm) == 1) {
				noTis("danger", "Error", "Username already exists.");

			} else {

				// check not duplicate mykad
				if (chkAny("kp", $mykad) == 0) {

					// update pin status
					mysqli_query($con, "UPDATE bestpin SET dateactive='".tarikh."', status=1 WHERE no='".$pin."'");

					// register new member
					require "conn2.php";
					include "_register.php";
					
					// hantar email
					include "_phpmailer.php";
					
					// bonus unilevel Shopping
					$r = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM komisen"));
					$l1 = $r[1];
					$l2 = $r[2];
					$l3 = $r[3];
					$l4 = $r[4];
					$l5 = $r[5];
					$jumlah = $l1+$l2+$l3+$l4+$l5;
					
					// bonus unilevel Travel
					$r_t = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM komisen_travel"));
					$l1_t = $r_t[1];
					$l2_t = $r_t[2];
					$l3_t = $r_t[3];
					$l4_t = $r_t[4];
					$l5_t = $t_t[5];
					$jumlah_t = $l1_t+$l2_t+$l3_t+$l4_t+$l5_t;
					
					// bonus unilevel cash
					$r_c = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM komisen_cash"));
					$l1_c = $r_c[1];
					$l2_c = $r_c[2];
					$l3_c = $r_c[3];
					$l4_c = $r_c[4];
					$l5_c = $r_c[5];
					$jumlah_c = $l1_c+$l2_c+$l3_c+$l4_c+$l5_c;
					
					// bayaran bonus
					
					// bonus L1
					mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 1 Bonus : ".$nama."', '".$usrnm."', ".$l1.", ".$penaja.")");
					
					mysqli_query($con, "INSERT INTO cwallet_travel (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 1 Bonus Trip : ".$nama."', '".$usrnm."', ".$l1_t.", ".$penaja.")");
					
					mysqli_query($con, "INSERT INTO cwallet_cash (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 1 Bonus Cash : ".$nama."', '".$usrnm."', ".$l1_c.", ".$penaja.")");

					// --------------------------------------------------------------------------------------

					// bonus L2
					$penaja1 = reKod("penaja", "ahli", "id", $penaja);
					mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 2 Bonus : ".$nama."', '".$usrnm."', ".$l2.", ".$penaja1.")");
					
					mysqli_query($con, "INSERT INTO cwallet_travel (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 2 Bonus Trip : ".$nama."', '".$usrnm."', ".$l2_t.", ".$penaja1.")");
					
					mysqli_query($con, "INSERT INTO cwallet_cash (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 2 Bonus Cash : ".$nama."', '".$usrnm."', ".$l2_c.", ".$penaja1.")");

					// --------------------------------------------------------------------------------------

					// bonus L3
					$penaja2 = reKod("penaja", "ahli", "id", $penaja1);
					mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 3 Bonus : ".$nama."', '".$usrnm."', ".$l3.", ".$penaja2.")");
					
					mysqli_query($con, "INSERT INTO cwallet_travel (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 3 Bonus Trip : ".$nama."', '".$usrnm."', ".$l3_t.", ".$penaja2.")");
					
					mysqli_query($con, "INSERT INTO cwallet_cash (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 3 Bonus Cash : ".$nama."', '".$usrnm."', ".$l3_c.", ".$penaja2.")");
					
					// --------------------------------------------------------------------------------------

					// bonus L4
					$penaja3 = reKod("penaja", "ahli", "id", $penaja2);
					mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 4 Bonus : ".$nama."', '".$usrnm."', ".$l4.", ".$penaja3.")");	
					
					mysqli_query($con, "INSERT INTO cwallet_travel (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 4 Bonus Trip : ".$nama."', '".$usrnm."', ".$l4_t.", ".$penaja3.")");
					
					mysqli_query($con, "INSERT INTO cwallet_cash (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 4 Bonus Cash : ".$nama."', '".$usrnm."', ".$l4_c.", ".$penaja3.")");


					// --------------------------------------------------------------------------------------

					// bonus L5
					$penaja4 = reKod("penaja", "ahli", "id", $penaja3);
					mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 5 Bonus : ".$nama."', '".$usrnm."', ".$l5.", ".$penaja4.")");
					
					mysqli_query($con, "INSERT INTO cwallet_travel (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 5 Bonus Trip : ".$nama."', '".$usrnm."', ".$l5_t.", ".$penaja4.")");
					
					mysqli_query($con, "INSERT INTO cwallet_cash (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 5 Bonus Cash : ".$nama."', '".$usrnm."', ".$l5_c.", ".$penaja4.")");

					/* registration log for admin
					1 - pin
					2 - cash
					3 - withdraw
					*/
					$senarai = $penaja.",".$penaja1.",".$penaja2.",".$penaja3.",".$penaja4; 
					$sql = "SELECT GROUP_CONCAT(username) FROM `ahli` WHERE id IN (".$senarai.")";
					$row = mysqli_fetch_array(mysqli_query($con, $sql));

					$output = logSub($tarikh, 2, $usrnm." account registration. Bonus for ".$row[0], $jumlah);
					if (!$output) {
						echo mysqli_error($con);
					}

				} else {

					noTis("danger", "Error", "Your account already exists.");

				}
			} 					
			
		}		

    }    

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