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

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

if ($currentUser) {
    $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') {
    $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['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>Zoho 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">
</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">Zoho Billing<small>Customer-ready billing</small></span>
        </div>
        <div class="showcase-testimonial">
            <blockquote>Sign in to manage customers, invoices, subscriptions, payments, and onboarding progress in one place.</blockquote>
        </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-header">
            <h1>Sign in</h1>
            <p>Access your billing workspace.</p>
        </div>

        <?php if ($message !== ''): ?>
            <div class="auth-alert"><?php echo billing_escape($message); ?></div>
        <?php endif; ?>

        <form method="post" class="auth-form">
            <label class="field-label" for="email">Email address</label>
            <input class="field-input" id="email" name="email" type="email" value="<?php echo billing_escape($email); ?>" required>

            <label class="field-label" for="password">Password</label>
            <input class="field-input" id="password" name="password" type="password" required>

            <button class="auth-submit" type="submit">Sign In</button>
        </form>
    </section>
</main>
</body>
</html>

