<?php


include 'config.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $phone = $_POST['phone'];
    $password = $_POST['password'];

    $sql_query = "SELECT * FROM tbl_users WHERE mobile='$phone'";
    $result = mysqli_query($db, $sql_query);
    $count = mysqli_num_rows($result);

    if ($count >= 1) {
        $row = mysqli_fetch_array($result);
        $userId = $row['id'];
        $hashed_password = $row['password'];

        if (password_verify($password, $hashed_password)) {
            $_SESSION['role'] = 1;
            $_SESSION['username'] = $phone;
            $_SESSION['userId'] = $userId;
            $_SESSION['success'] = "You are now logged in";

            // Send success response
            echo json_encode(['success' => true]);
        } else {
            // Invalid password
            echo json_encode(['success' => false, 'message' => 'Invalid username or password']);
        }
    } else {
        // User not found
        echo json_encode(['success' => false, 'message' => 'User not found']);
    }
} else {
    // Invalid request method
    echo json_encode(['success' => false, 'message' => 'Invalid request']);
}
?>