<?php
require_once 'connection.php';
session_start();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $password = $_POST['password'];

    $query = "SELECT * FROM tb_akun WHERE email = '$email' LIMIT 1";
    $sql = mysqli_query($conn, $query);
    $result = mysqli_fetch_assoc($sql);

    if ($result) {
        if (password_verify($password, $result['password'])) {
            if ($result['status'] == 1) {
                // Akun aktif dan login berhasil
                $_SESSION['email'] = $email;
                header('Location: dashboard.php');
                exit;
            } else {
                // Akun belum diverifikasi (status 0)
                $_SESSION['email'] = $email;
                $_SESSION['status'] = "warning, Akun belum diverifikasi. Silakan masukkan kode OTP.";
                header('Location: otp.php');
                exit;
            }
        } else {
            $_SESSION['status'] = "danger, Password salah!";
            header('Location: index.php');
            exit;
        }
    } else {
        $_SESSION['status'] = "danger, Email tidak terdaftar!";
        header('Location: index.php');
        exit;
    }
}
?>
