{% extends "base.html" %}

{% block title %}登入歷史 - WOOO TECH{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <div class="page-header">
        <h1><i class="fas fa-clock-rotate-left me-2"></i>登入歷史</h1>
        <p>系統登入記錄與異常嘗試追蹤</p>
    </div>

    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <span><i class="fas fa-list me-2"></i>最近記錄</span>
            <select class="form-select form-select-sm" id="historyLimit" style="width: 120px;">
                <option value="50">50 筆</option>
                <option value="100" selected>100 筆</option>
                <option value="200">200 筆</option>
            </select>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>時間</th>
                            <th>帳號</th>
                            <th>狀態</th>
                            <th>IP</th>
                            <th>原因</th>
                            <th>User Agent</th>
                        </tr>
                    </thead>
                    <tbody id="loginHistoryBody">
                        <tr>
                            <td colspan="6" class="text-center py-4">
                                <div class="spinner-border text-primary" role="status"></div>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
const statusClass = {
    success: 'text-bg-success',
    failed: 'text-bg-danger',
    locked: 'text-bg-warning'
};

function escapeHtml(value) {
    return String(value ?? '').replace(/[&<>"']/g, char => ({
        '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;'
    }[char]));
}

async function loadLoginHistory() {
    const limit = document.getElementById('historyLimit').value;
    const tbody = document.getElementById('loginHistoryBody');
    const response = await fetch(`/api/login_history?limit=${limit}`);
    const result = await response.json();
    if (!result.success) {
        tbody.innerHTML = `<tr><td colspan="6" class="text-center text-danger py-4">${escapeHtml(result.message)}</td></tr>`;
        return;
    }
    if (!result.data.length) {
        tbody.innerHTML = '<tr><td colspan="6" class="text-center text-muted py-4">尚無登入記錄</td></tr>';
        return;
    }
    tbody.innerHTML = result.data.map(item => `
        <tr>
            <td>${escapeHtml(item.login_time ? new Date(item.login_time).toLocaleString('zh-TW') : '')}</td>
            <td>${escapeHtml(item.username_attempted || item.user_id || '')}</td>
            <td><span class="badge ${statusClass[item.status] || 'text-bg-secondary'}">${escapeHtml(item.status)}</span></td>
            <td>${escapeHtml(item.ip_address)}</td>
            <td>${escapeHtml(item.failure_reason)}</td>
            <td class="text-truncate" style="max-width: 360px;">${escapeHtml(item.user_agent)}</td>
        </tr>
    `).join('');
}

document.getElementById('historyLimit').addEventListener('change', loadLoginHistory);
document.addEventListener('DOMContentLoaded', loadLoginHistory);
</script>
{% endblock %}
