<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Customer Login - GO-ACS</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary: #6366f1;
            --secondary: #0ea5e9;
            --success: #10b981;
            --warning: #f59e0b;
            --danger: #ef4444;
            --dark: #1e1b4b;
            --darker: #0f0d24;
            --light: #f8fafc;
            --gray: #64748b;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', sans-serif;
            background: var(--darker);
            color: var(--light);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .login-container {
            width: 100%;
            max-width: 420px;
            padding: 2rem;
        }

        .login-header {
            text-align: center;
            margin-bottom: 2rem;
        }

        .login-logo {
            font-size: 3rem;
            color: var(--primary);
            margin-bottom: 1rem;
        }

        .login-title {
            font-size: 1.75rem;
            font-weight: 700;
            margin-bottom: 0.5rem;
        }

        .login-subtitle {
            color: var(--gray);
        }

        .login-card {
            background: rgba(255, 255, 255, 0.05);
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 20px;
            padding: 2rem;
            backdrop-filter: blur(10px);
        }

        .form-group {
            margin-bottom: 1.5rem;
        }

        .form-group label {
            display: block;
            margin-bottom: 0.5rem;
            font-size: 0.875rem;
            color: var(--gray);
        }

        .input-wrapper {
            position: relative;
        }

        .input-wrapper i {
            position: absolute;
            left: 14px;
            top: 50%;
            transform: translateY(-50%);
            color: var(--gray);
        }

        .form-group input {
            width: 100%;
            padding: 14px 14px 14px 44px;
            background: rgba(0, 0, 0, 0.3);
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 12px;
            color: var(--light);
            font-size: 1rem;
            transition: all 0.2s;
        }

        .form-group input:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
        }

        .form-group input::placeholder {
            color: var(--gray);
        }

        .remember-row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 1.5rem;
            font-size: 0.875rem;
        }

        .remember-row label {
            display: flex;
            align-items: center;
            gap: 8px;
            cursor: pointer;
            color: var(--gray);
        }

        .remember-row a {
            color: var(--primary);
            text-decoration: none;
        }

        .btn-login {
            width: 100%;
            padding: 14px;
            background: linear-gradient(135deg, #6366f1, #4f46e5);
            border: none;
            border-radius: 12px;
            color: white;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
            transition: all 0.3s;
        }

        .btn-login:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(99, 102, 241, 0.3);
        }

        .btn-login:disabled {
            opacity: 0.7;
            cursor: not-allowed;
            transform: none;
        }

        .error-message {
            background: rgba(239, 68, 68, 0.1);
            border: 1px solid rgba(239, 68, 68, 0.3);
            border-radius: 10px;
            padding: 12px;
            margin-bottom: 1rem;
            display: none;
            align-items: center;
            gap: 10px;
            color: #f87171;
            font-size: 0.875rem;
        }

        .error-message.show {
            display: flex;
        }

        .login-footer {
            text-align: center;
            margin-top: 2rem;
            color: var(--gray);
            font-size: 0.875rem;
        }

        .login-footer a {
            color: var(--primary);
            text-decoration: none;
            font-weight: 500;
        }

        .admin-link {
            margin-top: 1rem;
            text-align: center;
            font-size: 0.8rem;
        }

        .admin-link a {
            color: var(--gray);
            text-decoration: none;
        }

        .admin-link a:hover {
            color: var(--primary);
        }

        /* Loading spinner */
        .spinner {
            width: 20px;
            height: 20px;
            border: 2px solid transparent;
            border-top-color: white;
            border-radius: 50%;
            animation: spin 0.8s linear infinite;
        }

        @keyframes spin {
            to {
                transform: rotate(360deg);
            }
        }

        /* Additional decorations */
        .bg-decoration {
            position: fixed;
            width: 300px;
            height: 300px;
            border-radius: 50%;
            background: linear-gradient(135deg, rgba(99, 102, 241, 0.3), rgba(14, 165, 233, 0.2));
            filter: blur(80px);
            z-index: -1;
        }

        .bg-decoration.top {
            top: -100px;
            right: -100px;
        }

        .bg-decoration.bottom {
            bottom: -100px;
            left: -100px;
        }
    </style>
</head>

<body>
    <div class="bg-decoration top"></div>
    <div class="bg-decoration bottom"></div>

    <div class="login-container">
        <div class="login-header">
            <div class="login-logo">
                <i class="fas fa-user-circle"></i>
            </div>
            <h1 class="login-title">Customer Portal</h1>
            <p class="login-subtitle">Login to manage your internet service</p>
        </div>

        <div class="login-card">
            <div class="error-message" id="errorMessage">
                <i class="fas fa-exclamation-circle"></i>
                <span id="errorText">Invalid username or password</span>
            </div>

            <form id="loginForm" onsubmit="handleLogin(event)">
                <div class="form-group">
                    <label>Username or Customer ID</label>
                    <div class="input-wrapper">
                        <i class="fas fa-user"></i>
                        <input type="text" id="username" placeholder="Enter your username" required autofocus>
                    </div>
                </div>

                <div class="form-group">
                    <label>Password</label>
                    <div class="input-wrapper">
                        <i class="fas fa-lock"></i>
                        <input type="password" id="password" placeholder="Enter your password" required>
                    </div>
                </div>

                <div class="remember-row">
                    <label>
                        <input type="checkbox" id="remember"> Remember me
                    </label>
                    <a href="#">Forgot password?</a>
                </div>

                <button type="submit" class="btn-login" id="loginBtn">
                    <i class="fas fa-sign-in-alt"></i>
                    <span>Login</span>
                </button>
            </form>
        </div>

        <div class="login-footer">
            Need help? Contact support at <a href="tel:+6281234567890">0812-3456-7890</a>
        </div>

        <div class="admin-link">
            <a href="/"><i class="fas fa-arrow-left"></i> Back to main site</a>
        </div>
    </div>

    <script>
        async function handleLogin(e) {
            e.preventDefault();

            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const loginBtn = document.getElementById('loginBtn');
            const errorMessage = document.getElementById('errorMessage');

            // Hide previous error
            errorMessage.classList.remove('show');

            // Show loading state
            loginBtn.disabled = true;
            loginBtn.innerHTML = '<div class="spinner"></div> <span>Logging in...</span>';

            try {
                const response = await fetch('/api/portal/auth/login', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ username, password })
                });

                const data = await response.json();

                if (data.success) {
                    // Store customer token
                    localStorage.setItem('customerToken', data.token);
                    localStorage.setItem('customerId', data.customer.id);
                    localStorage.setItem('customerName', data.customer.name);

                    // Redirect to portal
                    window.location.href = '/portal';
                } else {
                    showError(data.error || 'Invalid credentials');
                }
            } catch (error) {
                showError('Connection error. Please try again.');
            } finally {
                // Reset button state
                loginBtn.disabled = false;
                loginBtn.innerHTML = '<i class="fas fa-sign-in-alt"></i> <span>Login</span>';
            }
        }

        function showError(message) {
            const errorMessage = document.getElementById('errorMessage');
            document.getElementById('errorText').textContent = message;
            errorMessage.classList.add('show');
        }

        // Check if already logged in
        if (localStorage.getItem('customerToken')) {
            window.location.href = '/portal';
        }
    </script>
</body>

</html>