<?php
// Include database 

// Allow requests from any origin
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Content-Type");
header('Content-Type: application/json');
header('Access-Control-Allow-Methods','GET, POST, PUT, DELETE, OPTIONS');

include 'db_config.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $json_data = file_get_contents('php://input');
    
    // Decode the JSON data into an associative array
    $data = json_decode($json_data, true);

    $username = $data['username'];
    $password = $data['password'];
    $mobile = $data['mobile'];

    // Generate salt
    $salt = "12061998";

    // Hash password
    $hashedPassword = md5($password . $salt);

    try {
        // Prepare statement to insert user into the database
        $stmt = $pdo->prepare("INSERT INTO users (username, password, mobile) VALUES (?, ?, ?)");
        $stmt->execute([$username, $hashedPassword, $mobile]);
        
        echo json_encode(["res"=>true,"msg"=>"User registered successfully!"]);
    } catch (PDOException $e) {
        echo json_encode(["res"=>false,"msg"=>"Error: " . $e->getMessage()]);
    }
} else {
    echo json_encode(["res"=>false,msg=>"Error: api error"]);
    //header("Location: http://localhost/login");
    exit();
}
?>
