<?php
include "control/activity.php";
$dd = new activity();

$mobile = $_REQUEST['mobile'];
$pass = $_REQUEST['pass'];
$code = $_REQUEST['code'];

if($mobile=="" || $pass == "" || $code=="")
{
    print_r(json_encode(array("status" => "failed","remark" => "Field Required")));
}
else{
    $date = date("h:i d/m/Y");
    $bytes = openssl_random_pseudo_bytes(8);
    $refral = bin2hex($bytes);
    $bytes = openssl_random_pseudo_bytes(6);
    $memId = "MEMBER".bin2hex($bytes).mt_rand(11,99);

    $table = "userdetails";
    if($check["status"] === "success"){
        print_r(json_encode(array("status" => "success","remark" => "Already Register")));
        die();
    }
    $value = "(`memId`, `mobile`,`password`,`join_date`,`my_refral_id`, `refral_from`,`level_1`, `level_2`, `level_3`, `level_4`, `level_5`, `level_6`, `level_7`, `level_8`, `level_9`, `level_10`)
     SELECT '$memId','$mobile','$pass','$date','$refral','$code','$code',level_1,level_2,level_3,level_4,level_5,level_6,level_7,level_8,level_9 FROM userdetails WHERE my_refral_id='$code'";
    $result = $dd->insertData($table,$value);
    if($result){
        $table = "userdetails";
        $value = "wallet=wallet+10,withdraw_balance=withdraw_balance+0,refAmount=refAmount+10";
        $condition = " my_refral_id='$code'";
        $result = $dd->updateData($table,$value,$condition);
        if($result){
              $date = date("h:i d/m/Y");
              $ref = mt_rand(1111111111,9999999999); 
              $table = "funds_request";
              $value = "(`userKey_id`, `request_date`, `request_type`, `amount`, `refrence_number`, `funds_status`, `approved_date`, `avl_balance`, `remark`) 
              SELECT userKey,'$date','INR','10','$ref','success','$date',wallet,'Refral Incentive' FROM userdetails WHERE my_refral_id='$code'";
              $result = $dd->insertData($table,$value);
              if($result){
                  print_r(json_encode(array("status" => "success","remark" => "Registration successfull")));
              }
        }
    }
}


?>