
<?php 
include '../config.php';

$name = $_POST['name'];
$mobileNum = $_POST['mobileNum'];
$password = $_POST['password'];
$member_psasscode = $_POST['member_passcode'];
$email = $_POST['email'];
$response = array();
$message="";
$query ="SELECT COUNT(*) FROM member WHERE member_mobile = '$mobileNum' OR member_username='$email'";
$result = mysqli_query($conn,$query);
$r = mysqli_fetch_array($result);

$query1 = "SELECT * FROM admin";
$result1 = mysqli_query($conn,$query1);
$rows1 = mysqli_fetch_assoc($result1);
$bonus = $rows1['welcome_bonus'];
$transection_id = 'TRANS'.rand(0000,9999);
$date_transection = $dateTime;
$r1 = $r[0];
if($row=mysqli_fetch_array($result))
{
    #member already exist
       $MemberMobile= $row["member_mobile"];
       $member_username= $row["member_username"];
       
       if($MemberMobile==$mobileNum && $member_username==$email)
       {
           $message="Email Or Mobile number already exists";
       }
       else if($MemberMobile==$mobileNum )
       {
           $message="Mobile number already exists";
       }
       else if($member_username==$email)
       {
           $message="Email already exists";
       }
        
        $response = ["status"=>"Failure","Message"=>$message];
}
else{ 
         $sql = "INSERT INTO member(member_name, member_mobile, member_password,member_username) VALUES ('$name' , '$mobileNum' , '$password','$email')";
        if(mysqli_query($conn, $sql)) {
                $lastid=mysqli_insert_id($conn);
                $sql3="INSERT INTO member_payment_getway(member_id) VALUES ('$lastid')";
                if(mysqli_query($conn,$sql3)){
                        $sql2 = "INSERT INTO member_wallet(member_id, member_wallet_balance) VALUES ('$lastid','$bonus')";
                        if(mysqli_query($conn, $sql2)) 
                        {
                            
                            $sql = "INSERT INTO wallet_transaction(transaction_id, transaction_amount, member_id, transaction_update_date, transaction_type) VALUES ('$transection_id','$bonus','$lastid','$date_transection','AddAmt')";   
                            mysqli_query($conn, $sql) ;
                            $query ="SELECT * FROM member WHERE member_mobile = '$mobileNum'";
                            $result = mysqli_query($conn,$query);
                            $r = mysqli_fetch_array($result);
                            $memberstatus=  $r["status"];
                            $response=["status"=>"success","member_id"=>$lastid,"MemberStatus"=>$memberstatus];
                        }
                }

        }
        else {
                $response=["status"=>"failure"];
        }
}
echo json_encode($response);
?>

