<?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";	
echo "haiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii";
    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 cash
					$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;
echo $jumlah;
					// bonus unilevel carwash
					$r_cw       = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM komisen_carwash"));
					$l1_cw      = $r_cw[1];
					$l2_cw      = $r_cw[2];
					$l3_cw      = $r_cw[3];
					$l4_cw      = $r_cw[4];
					$l5_cw      = $r_cw[5];
					$jumlah_cw  = $l1_cw+$l2_cw+$l3_cw+$l4_cw+$l5_cw;					
					
					// bonus unilevel Wax & polish
					$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 repair
					$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;
					
					// bonus unilevel Accessories
					$r_acc = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM komisen_accessories"));
					$l1_acc = $r_acc[1];
					$l2_acc = $r_acc[2];
					$l3_acc = $r_acc[3];
					$l4_acc = $r_acc[4];
					$l5_acc = $r_acc[5];
					$jumlah_acc = $l1_acc+$l2_acc+$l3_acc+$l4_acc+$l5_acc;

					// bonus unilevel insurance
					$r_ins = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM komisen_insurance"));
					$l1_ins = $r_ins[1];
					$l2_ins = $r_ins[2];
					$l3_ins = $r_ins[3];
					$l4_ins = $r_ins[4];
					$l5_ins = $r_ins[5];
					$jumlah_ins = $l1_ins+$l2_ins+$l3_ins+$l4_ins+$l5_ins;

					
					// bayaran bonus
					
					// bonus L1
?>					<script>alert ("Bonus Level 1");</script>
<?php				mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 1 Cash Bonus : ".$nama."', '".$usrnm."', ".$l1.", ".$penaja.")");

					mysqli_query($con, "INSERT INTO cwallet_carwash (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 1 Carwash Bonus : ".$nama."', '".$usrnm."', ".$l1_cw.", ".$penaja.")");
					
					mysqli_query($con, "INSERT INTO cwallet_travel (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 1 Car Wax Polish Bonus : ".$nama."', '".$usrnm."', ".$l1_t.", ".$penaja.")");
					
					mysqli_query($con, "INSERT INTO cwallet_cash (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 1 Car Repair Bonus : ".$nama."', '".$usrnm."', ".$l1_c.", ".$penaja.")");

					mysqli_query($con, "INSERT INTO cwallet_accessories  (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 1 Car Accessories Bonus : ".$nama."', '".$usrnm."', ".$l1_acc.", ".$penaja.")");

					mysqli_query($con, "INSERT INTO cwallet_insurance  (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 1 Car Insurance Bonus : ".$nama."', '".$usrnm."', ".$l1_ins.", ".$penaja.")");

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

					// bonus L2
?>					<script>alert ("Bonus Level 2");</script>
<?php				$penaja1 = reKod("penaja", "ahli", "id", $penaja);
					mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 2 Cash Bonus : ".$nama."', '".$usrnm."', ".$l2.", ".$penaja1.")");

					mysqli_query($con, "INSERT INTO cwallet_carwash (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 2 Carwash Bonus : ".$nama."', '".$usrnm."', ".$l2_cw.", ".$penaja1.")");
					
					mysqli_query($con, "INSERT INTO cwallet_travel (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 2 Car Wax Polish Bonus : ".$nama."', '".$usrnm."', ".$l2_t.", ".$penaja1.")");
					
					mysqli_query($con, "INSERT INTO cwallet_cash (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 2 Car Repair Bonus : ".$nama."', '".$usrnm."', ".$l2_c.", ".$penaja1.")");

					mysqli_query($con, "INSERT INTO cwallet_accessories (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 2 Car Accessories Bonus : ".$nama."', '".$usrnm."', ".$l2_acc.", ".$penaja1.")");
					
					mysqli_query($con, "INSERT INTO cwallet_insurance  (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 2 Car Insurance Bonus : ".$nama."', '".$usrnm."', ".$l2_ins.", ".$penaja1.")");					

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

					// bonus L3
?>					<script>alert ("Bonus Level 3");</script>
<?php				$penaja2 = reKod("penaja", "ahli", "id", $penaja1);
					mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 3 Cash Bonus : ".$nama."', '".$usrnm."', ".$l3.", ".$penaja2.")");

					mysqli_query($con, "INSERT INTO cwallet_carwash (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 3 Carwash Bonus : ".$nama."', '".$usrnm."', ".$l3_cw.", ".$penaja2.")");
					
					mysqli_query($con, "INSERT INTO cwallet_travel (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 3 Car Wax Polish Bonus : ".$nama."', '".$usrnm."', ".$l3_t.", ".$penaja2.")");
					
					mysqli_query($con, "INSERT INTO cwallet_cash (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 3 Car Repair Bonus : ".$nama."', '".$usrnm."', ".$l3_c.", ".$penaja2.")");

					mysqli_query($con, "INSERT INTO cwallet_accessories (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 3 Car Accessories Bonus : ".$nama."', '".$usrnm."', ".$l3_acc.", ".$penaja2.")");

					mysqli_query($con, "INSERT INTO cwallet_insurance  (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 3 Car Insurance Bonus : ".$nama."', '".$usrnm."', ".$l3_ins.", ".$penaja2.")");
					
					// --------------------------------------------------------------------------------------

					// bonus L4
?>					<script>alert ("Bonus Level 4");</script>
<?php				$penaja3 = reKod("penaja", "ahli", "id", $penaja2);
					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.")");

					mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 4 Cash Bonus : ".$nama."', '".$usrnm."', ".$l4.", ".$penaja3.")");

					mysqli_query($con, "INSERT INTO cwallet_carwash (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 4 Carwash Bonus : ".$nama."', '".$usrnm."', ".$l4_cw.", ".$penaja3.")");
					
					mysqli_query($con, "INSERT INTO cwallet_travel (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 4 Car Wax Polish Bonus : ".$nama."', '".$usrnm."', ".$l4_t.", ".$penaja3.")");
					
					mysqli_query($con, "INSERT INTO cwallet_cash (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 4 Car Repair Bonus : ".$nama."', '".$usrnm."', ".$l4_c.", ".$penaja3.")");

					mysqli_query($con, "INSERT INTO cwallet_accessories (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 4 Car Accessories Bonus : ".$nama."', '".$usrnm."', ".$l4_acc.", ".$penaja3.")");

					mysqli_query($con, "INSERT INTO cwallet_insurance  (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 4 Car Insurance Bonus : ".$nama."', '".$usrnm."', ".$l4_ins.", ".$penaja3.")");

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

					// bonus L5
?>					<script>alert ("Bonus Level 5");</script>
<?php				$penaja4 = reKod("penaja", "ahli", "id", $penaja3);
					mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 5 Cash Bonus : ".$nama."', '".$usrnm."', ".$l5.", ".$penaja4.")");

					mysqli_query($con, "INSERT INTO cwallet_carwash (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 5 Carwash Bonus : ".$nama."', '".$usrnm."', ".$l5_cw.", ".$penaja4.")");
					
					mysqli_query($con, "INSERT INTO cwallet_travel (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 5 Car Wax Polish Bonus : ".$nama."', '".$usrnm."', ".$l5_t.", ".$penaja4.")");
					
					mysqli_query($con, "INSERT INTO cwallet_cash (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 5 Car Repair Bonus : ".$nama."', '".$usrnm."', ".$l5_c.", ".$penaja4.")");

					mysqli_query($con, "INSERT INTO cwallet_accessories (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 5 Car Accessories Bonus : ".$nama."', '".$usrnm."', ".$l5_acc.", ".$penaja4.")");

					mysqli_query($con, "INSERT INTO cwallet_insurance  (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 5 Car Insurance Bonus : ".$nama."', '".$usrnm."', ".$l5_ins.", ".$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";
}
?>
