<?php
session_start();

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 database configuration
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'];

    try {
        // Prepare statement to fetch user from the database
        $stmt = $pdo->prepare("SELECT * FROM users WHERE mobile = ?");
        $stmt->execute([$username]);
        $user = $stmt->fetch();

        if ($user && (md5($password.'12061998')==$user['password'])) {
            $_SESSION['username'] = $user['username'];
            echo json_encode([
                "res"=>true,
                "user"=>$user['username'],
                "msg"=> "success"
            ]);
            
           // Response to indicate successful login
            exit();
        } else {
            echo json_encode([
                "res"=>false,
                "msg"=> "Wrong Password / Mobile Number"
            ]);
        }
    } catch (PDOException $e) {
        echo json_encode([
            "res"=>true,
            "msg"=> $e->getMessage()
        ]);
       
    }
} else {
    echo json_encode([
        "res"=>false,
        "msg"=> "Connection Error / Post Not working"
    ]);
    exit();
}
?>
