<?php
session_start();
require_once dirname(__DIR__) . '/includes/functions.php';

$message = '';
$email = '';
$currentUser = billing_current_user();

if ($currentUser && $_SERVER['REQUEST_METHOD'] !== 'POST') {
    $organization = billing_current_organization();
    if ($organization && !empty($organization['onboarding_completed_at'])) {
        billing_redirect('dashboard.php?tab=dashboard');
    }
    billing_redirect('onboarding.php?step=1');
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    unset(
        $_SESSION['billing_user_id'],
        $_SESSION['billing_user_name'],
        $_SESSION['billing_user_email']
    );

    $email = billing_clean($_POST['email'] ?? '');
    $password = (string) ($_POST['password'] ?? '');

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $message = 'Please enter a valid email address.';
    } elseif ($password === '') {
        $message = 'Please enter your password.';
    } else {
        $user = billing_fetch_one(
            'SELECT id, business_name, email, password_hash
             FROM users
             WHERE email = ?
             LIMIT 1',
            's',
            [$email]
        );

        if (!$user || !password_verify($password, (string) $user['password_hash'])) {
            $message = 'Invalid email or password.';
        } else {
            session_regenerate_id(true);
            $_SESSION['billing_user_id'] = (int) $user['id'];
            $_SESSION['billing_user_name'] = (string) $user['business_name'];
            $_SESSION['billing_user_email'] = (string) $user['email'];

            $organization = billing_current_organization();
            if (!$organization || empty($organization['onboarding_completed_at'])) {
                billing_redirect('onboarding.php?step=1');
            }

            billing_redirect('dashboard.php?tab=dashboard');
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<base href="<?php echo billing_escape(billing_base_href()); ?>">
<title>b2lmobitech Billing Sign In</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Manrope:wght@400;500;700;800&display=swap">
<link rel="stylesheet" href="Payslip_Project/assets/fontawesome/css/all.min.css">
<link rel="stylesheet" href="assets/billing.css?v=20260507-css-recovery-2">
</head>
<body class="billing-auth-body">
<main class="auth-shell">
    <section class="auth-showcase">
        <div class="billing-logo">
            <span class="billing-logo-mark"><i class="fa-solid fa-bolt"></i></span>
            <span class="billing-logo-copy">b2lmobitech Billing<small>Customer-ready billing</small></span>
        </div>
        <div class="showcase-copy">
            <h1>Sign in to manage customers, invoices, subscriptions, payments, and onboarding progress in one place.</h1>
        </div>
        <div class="showcase-proof">
            <h4>Built For Speed</h4>
            <div class="proof-logos">
                <span>Invoices</span>
                <span>Subscriptions</span>
                <span>Payments</span>
                <span>GST</span>
                <span>Reports</span>
            </div>
        </div>
    </section>

    <section class="auth-panel">
        <div class="auth-form-wrap">
            <h1 class="auth-title">Sign in</h1>
            <p class="auth-subtitle">Access your billing workspace.</p>

            <?php if ($message !== ''): ?>
                <div style="margin:0 0 18px; padding:14px 16px; border-radius:14px; background:#fff3f3; border:1px solid #f2caca; color:#a12626; font-weight:600;">
                    <?php echo billing_escape($message); ?>
                </div>
            <?php endif; ?>

            <form method="post" class="field-list">
                <div class="field-group">
                    <label for="email">Email address</label>
                    <div class="field-shell">
                        <i class="fa-regular fa-envelope"></i>
                        <input id="email" name="email" type="email" value="<?php echo billing_escape($email); ?>" required>
                    </div>
                </div>

                <div class="field-group">
                    <label for="password">Password</label>
                    <div class="field-shell">
                        <i class="fa-solid fa-lock"></i>
                        <input id="password" name="password" type="password" required>
                        <button type="button" id="togglePassword" aria-label="Show password" style="border:0;background:transparent;color:#6b7280;cursor:pointer;padding:0;">
                            <i class="fa-regular fa-eye"></i>
                        </button>
                    </div>
                </div>

                <button class="primary-btn" type="submit">Sign In</button>
            </form>
        </div>
    </section>
</main>
<script>
document.addEventListener('DOMContentLoaded', function () {
    var passwordInput = document.getElementById('password');
    var toggle = document.getElementById('togglePassword');
    if (!passwordInput || !toggle) {
        return;
    }

    toggle.addEventListener('click', function () {
        var isHidden = passwordInput.type === 'password';
        passwordInput.type = isHidden ? 'text' : 'password';
        toggle.setAttribute('aria-label', isHidden ? 'Hide password' : 'Show password');
        toggle.innerHTML = isHidden
            ? '<i class="fa-regular fa-eye-slash"></i>'
            : '<i class="fa-regular fa-eye"></i>';
    });
});
</script>
</body>
</html>

