<?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']);
        $package    = mysqli_real_escape_string($con, $_POST['i']);
        
            $sql    =   "SELECT * FROM packages WHERE id = $package";
            $result =   mysqli_query($con, $sql);
                        while ($p = mysqli_fetch_assoc($result)) {
                            $directSponsor  = number_format($p['direct_sponsor'], 2);
                            $pakejName      = $p['name'];
                            $commLevel      = $p['commission_level'];
                        }


		$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) {

					// register new member
					require "conn2.php";
					include "_register.php";
					
					// hantar email
					include "_phpmailer.php";

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

					
					// 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];
					$l6 = $r[6];
					$l7 = $r[7];
					$l8 = $r[8];
					$l9 = $r[9];
					$jumlah = $l1+$l2+$l3+$l4+$l5+$l6+$l7+$l8+$l9;


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

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

					// bonus L2
				    $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.")");

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

					// bonus L3
				    $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.")");

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

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

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

					// bonus L5
				    $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.")");

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

					// bonus L6
				    $penaja5 = reKod("penaja", "ahli", "id", $penaja4);
					mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 6 Cash Bonus : ".$nama."', '".$usrnm."', ".$l6.", ".$penaja5.")");
					
					// --------------------------------------------------------------------------------------

					// bonus L7
				    $penaja6 = reKod("penaja", "ahli", "id", $penaja5);
					mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 7 Cash Bonus : ".$nama."', '".$usrnm."', ".$l7.", ".$penaja6.")");					

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

					// bonus L8
				    $penaja7 = reKod("penaja", "ahli", "id", $penaja6);
					mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 8 Cash Bonus : ".$nama."', '".$usrnm."', ".$l8.", ".$penaja7.")");
					
					// --------------------------------------------------------------------------------------

					// bonus L9
				    $penaja8 = reKod("penaja", "ahli", "id", $penaja7);
					mysqli_query($con, "INSERT INTO cwallet (tarikh, keterangan, rujukan, masuk, userid)
					VALUES ('".$tarikh."', 'Level 9 Cash Bonus : ".$nama."', '".$usrnm."', ".$l9.", ".$penaja8.")");
					
					/* registration log for admin
					1 - pin
					2 - cash
					3 - withdraw
					*/
					$senarai = $penaja.",".$penaja1.",".$penaja2.",".$penaja3.",".$penaja4.",".$penaja5.",".$penaja6.",".$penaja7.",".$penaja8; 
					$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.");

				}
			} 					
			
		}		

    }    


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

        $nama       = mysqli_real_escape_string($con, $_POST['a']);
        $telefon    = mysqli_real_escape_string($con, $_POST['b']);
        $email      = mysqli_real_escape_string($con, $_POST['c']);
        $roc      = 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'];
		
		
		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>
<?
         
        
        			// check username
        			if (chkUsrMerchant($usrnm) == 1) {
        				noTis("danger", "Error", "Username already exists.");
        
        			} else {
        
        				// check not duplicate mykad
        				if (chkAnyRoc("roc", $roc) == 0) {
        
        					// register new member
        					require "conn2.php";
        					include "_register_merchant.php";
        					
        					// hantar email
        					include "_phpmailer.php";
         
        				} else {
        
        					noTis("danger", "Error", "Your account already exists.");
        
        				}
        			} 					
        			
        				

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