<?php
session_start();

// Database connection
$host = "localhost";
$user = "root";
$password = "";
$dbname = "dubai_treasury_bank";

$conn = mysqli_connect($host, $user, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Login process
if (isset($_POST['loginBtn'])) {

    $login_user = mysqli_real_escape_string($conn, $_POST['login_user']);
    $login_pass = mysqli_real_escape_string($conn, $_POST['login_pass']);

    // Query to check email or phone
    $sql = "SELECT * FROM users WHERE email='$login_user' OR phone='$login_user'";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) == 1) {
        $row = mysqli_fetch_assoc($result);

        // Verify password
        if (password_verify($login_pass, $row['password'])) {

            // Create session
            $_SESSION['user_id'] = $row['id'];
            $_SESSION['user_name'] = $row['full_name'];
            $_SESSION['user_email'] = $row['email'];

            // Redirect to dashboard
            header("Location: dashboard.php");
            exit();

        } else {
            echo "<script>alert('Wrong password!'); window.location='login.html';</script>";
        }
    } else {
        echo "<script>alert('Account not found!'); window.location='login.html';</script>";
    }
}

mysqli_close($conn);
?>
