{% extends "base.html" %}

{% block content %}
<div class="bg-white dark:bg-slate-800 shadow rounded-lg p-6">
    <div class="flex justify-between items-center mb-6">
        <h2 class="text-2xl font-bold text-gray-900 dark:text-white">管理后台 (Admin Dashboard)</h2>
        <a href="{{ url_for('admin.logout') }}" class="text-red-600 hover:text-red-800">Logout</a>
    </div>
    
    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
        <!-- ETL Controls -->
        <div class="border border-gray-200 dark:border-gray-700 rounded-lg p-4">
            <h3 class="text-lg font-bold text-gray-900 dark:text-white mb-4">数据管线 (ETL)</h3>
            <div class="space-y-2">
                <button onclick="triggerEtl('L1A.py')" class="w-full bg-blue-600 text-white py-2 px-4 rounded hover:bg-blue-700">Trigger L1A (Ingest)</button>
                <button onclick="triggerEtl('L2_Builder.py')" class="w-full bg-blue-600 text-white py-2 px-4 rounded hover:bg-blue-700">Trigger L2 Builder</button>
                <button onclick="triggerEtl('L3_Builder.py')" class="w-full bg-blue-600 text-white py-2 px-4 rounded hover:bg-blue-700">Trigger L3 Builder</button>
            </div>
            <div id="etlResult" class="mt-4 text-sm text-gray-600 dark:text-gray-400"></div>
        </div>
        
        <!-- Tools -->
        <div class="border border-gray-200 dark:border-gray-700 rounded-lg p-4">
            <h3 class="text-lg font-bold text-gray-900 dark:text-white mb-4">工具箱</h3>
            <div class="space-y-2">
                <a href="{{ url_for('admin.sql_runner') }}" class="block w-full text-center bg-gray-600 text-white py-2 px-4 rounded hover:bg-gray-700">SQL Runner</a>
                <a href="{{ url_for('wiki.index') }}" class="block w-full text-center bg-gray-600 text-white py-2 px-4 rounded hover:bg-gray-700">Manage Wiki</a>
            </div>
        </div>
    </div>
</div>

<script>
function triggerEtl(scriptName) {
    const resultDiv = document.getElementById('etlResult');
    resultDiv.innerText = "Triggering " + scriptName + "...";
    
    fetch("{{ url_for('admin.trigger_etl') }}", {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: 'script=' + scriptName
    })
    .then(response => response.text())
    .then(text => {
        resultDiv.innerText = text;
    })
    .catch(err => {
        resultDiv.innerText = "Error: " + err;
    });
}
</script>
{% endblock %}
