<?php

include 'db_connect.php';

$name      = $_POST['name'];
$email     = $_POST['email'];
$phone     = $_POST['phone'];
$password  = $_POST['password'];
$gender    = $_POST['gender'];
$birthdate = $_POST['birthdate'];
$country   = $_POST['country'];
$time      = $_POST['time'];
$key       = $_POST['key'];

$security_key = decrypData($key);

header('Content-Type: application/json; charset=utf-8');

if ($security_key == 'Jobayer121415' && strlen($email) > 0) {

    $con = dbConnect();
    $sql1 = "SELECT * FROM singup_table WHERE email = '$email'";
    $result = mysqli_query($con, $sql1);
    $rows = mysqli_num_rows($result);

    if ($rows <= 0) {

        $sql = "INSERT INTO singup_table(name, email, phone, password, image, time, cover, gender, birthdate, country, token) 
                VALUES ('$name', '$email', '$phone', '$password', '0', '$time', '0', '$gender', '$birthdate', '$country', '0')";

        $result = mysqli_query($con, $sql);

        if ($result) {
            $userid = mysqli_insert_id($con);
            echo json_encode([
                "status" => "success",
                "userid" => $userid
            ]);
        } else {
            echo json_encode([
                "status" => "error",
                "message" => "Query Error"
            ]);
        }

    } else {
        echo json_encode([
            "status" => "error",
            "message" => "User already available! Change email and try again"
        ]);
    }

    mysqli_close($con);
}

function decrypData($text) {
    $decoded = base64_decode($text);
    $decrypted = openssl_decrypt($decoded, 'AES-128-ECB', '0175526800812345', OPENSSL_RAW_DATA);
    return $decrypted;
}
?>
