<?php
require_once __DIR__ . '/../config/config.php';
requireLogin();

// 獲取一些統計數據
$total_employees = $pdo->query("SELECT COUNT(*) FROM employees")->fetchColumn();
$total_subcontractors = $pdo->query("SELECT COUNT(*) FROM subcontractors")->fetchColumn();
$active_sites = $pdo->query("SELECT COUNT(*) FROM sites WHERE status = 'active'")->fetchColumn();
$today_entries = $pdo->query("SELECT COUNT(*) FROM work_entries WHERE work_date = CURDATE()")->fetchColumn();

include __DIR__ . '/../includes/header.php';
?>

<div class="mb-8">
    <h1 class="text-2xl font-bold text-slate-800">首頁概覽</h1>
    <p class="text-slate-500">歡迎回來，<?php echo htmlspecialchars($_SESSION['username'] ?? '管理員'); ?>。以下是今天的系統摘要。</p>
</div>

<!-- Stats Grid -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-10">
    <div class="bg-white p-6 rounded-2xl shadow-sm border border-slate-100">
        <div class="flex items-center">
            <div class="p-3 rounded-xl bg-blue-50 text-blue-600 mr-4">
                <i class="fas fa-users text-xl"></i>
            </div>
            <div>
                <p class="text-sm text-slate-500 font-medium">總員工數</p>
                <p class="text-2xl font-bold text-slate-800"><?php echo $total_employees; ?></p>
            </div>
        </div>
    </div>
    <div class="bg-white p-6 rounded-2xl shadow-sm border border-slate-100">
        <div class="flex items-center">
            <div class="p-3 rounded-xl bg-indigo-50 text-indigo-600 mr-4">
                <i class="fas fa-briefcase text-xl"></i>
            </div>
            <div>
                <p class="text-sm text-slate-500 font-medium">總判工數</p>
                <p class="text-2xl font-bold text-slate-800"><?php echo $total_subcontractors; ?></p>
            </div>
        </div>
    </div>
    <div class="bg-white p-6 rounded-2xl shadow-sm border border-slate-100">
        <div class="flex items-center">
            <div class="p-3 rounded-xl bg-green-50 text-green-600 mr-4">
                <i class="fas fa-map-marker-alt text-xl"></i>
            </div>
            <div>
                <p class="text-sm text-slate-500 font-medium">活躍地盤</p>
                <p class="text-2xl font-bold text-slate-800"><?php echo $active_sites; ?></p>
            </div>
        </div>
    </div>
    <div class="bg-white p-6 rounded-2xl shadow-sm border border-slate-100">
        <div class="flex items-center">
            <div class="p-3 rounded-xl bg-orange-50 text-orange-600 mr-4">
                <i class="fas fa-calendar-check text-xl"></i>
            </div>
            <div>
                <p class="text-sm text-slate-500 font-medium">今日開工登記</p>
                <p class="text-2xl font-bold text-slate-800"><?php echo $today_entries; ?></p>
            </div>
        </div>
    </div>
</div>

<!-- Quick Actions -->
<h2 class="text-lg font-bold text-slate-800 mb-4">常用功能</h2>
<div class="grid grid-cols-2 md:grid-cols-4 lg:grid-cols-6 gap-4 mb-10">
    <a href="work_entry.php" class="bg-white p-4 rounded-xl shadow-sm border border-slate-100 hover:shadow-md transition text-center">
        <i class="fas fa-user-clock text-blue-500 text-2xl mb-2 block"></i>
        <span class="text-sm font-medium text-slate-700">開工登記</span>
    </a>
    <a href="bills.php" class="bg-white p-4 rounded-xl shadow-sm border border-slate-100 hover:shadow-md transition text-center">
        <i class="fas fa-file-invoice-dollar text-orange-500 text-2xl mb-2 block"></i>
        <span class="text-sm font-medium text-slate-700">帳單報銷</span>
    </a>
    <a href="employees.php" class="bg-white p-4 rounded-xl shadow-sm border border-slate-100 hover:shadow-md transition text-center">
        <i class="fas fa-user-plus text-green-500 text-2xl mb-2 block"></i>
        <span class="text-sm font-medium text-slate-700">新增員工</span>
    </a>
    <a href="sites.php" class="bg-white p-4 rounded-xl shadow-sm border border-slate-100 hover:shadow-md transition text-center">
        <i class="fas fa-plus-circle text-indigo-500 text-2xl mb-2 block"></i>
        <span class="text-sm font-medium text-slate-700">新增地盤</span>
    </a>
    <a href="quotations.php" class="bg-white p-4 rounded-xl shadow-sm border border-slate-100 hover:shadow-md transition text-center">
        <i class="fas fa-file-contract text-purple-500 text-2xl mb-2 block"></i>
        <span class="text-sm font-medium text-slate-700">製作報價</span>
    </a>
    <a href="finance.php" class="bg-white p-4 rounded-xl shadow-sm border border-slate-100 hover:shadow-md transition text-center">
        <i class="fas fa-calculator text-red-500 text-2xl mb-2 block"></i>
        <span class="text-sm font-medium text-slate-700">財報計算</span>
    </a>
</div>

<!-- AI 工程總結報告 -->
<div class="mb-10">
    <div class="flex justify-between items-center mb-4">
        <h2 class="text-lg font-bold text-slate-800 flex items-center gap-2">
            <i class="fas fa-robot text-purple-500"></i> AI 工程總結報告
        </h2>
        <button onclick="loadSummary()" id="summary-refresh" class="text-sm text-purple-600 hover:text-purple-800 font-medium flex items-center gap-1">
            <i class="fas fa-sync-alt" id="summary-icon"></i> 生成報告
        </button>
    </div>
    <div id="summary-container" class="bg-white rounded-2xl shadow-sm border border-slate-100 p-6">
        <div class="text-center py-10 text-slate-400">
            <i class="fas fa-robot text-4xl mb-3 block"></i>
            <p>按「生成報告」按鈕，AI 會根據系統數據自動生成工程總結</p>
        </div>
    </div>
</div>

<script>
function loadSummary() {
    const container = document.getElementById('summary-container');
    const icon = document.getElementById('summary-icon');
    icon.className = 'fas fa-sync-alt fa-spin';
    
    container.innerHTML = '<div class="text-center py-10 text-purple-500"><i class="fas fa-spinner fa-spin text-3xl mb-3 block"></i><p>AI 正在分析系統數據...</p></div>';
    
    const formData = new FormData();
    formData.append('generate', '1');
    
    fetch('summary_ai.php', { method: 'POST', body: formData })
    .then(r => r.json())
    .then(data => {
        icon.className = 'fas fa-sync-alt';
        if (data.success) {
            let html = '';
            html += '<div class="border-b border-slate-100 pb-4 mb-4">';
            html += '<h3 class="text-xl font-bold text-slate-900">' + data.title + '</h3>';
            html += '<p class="text-slate-600 text-sm mt-2">' + data.summary + '</p>';
            html += '</div>';
            
            if (data.highlights && data.highlights.length > 0) {
                html += '<div class="grid grid-cols-1 md:grid-cols-2 gap-3 mb-4">';
                data.highlights.forEach(function(h) {
                    html += '<div class="bg-gradient-to-r from-purple-50 to-blue-50 rounded-xl p-4 border border-purple-100">';
                    html += '<div class="flex items-start gap-2">';
                    html += '<i class="fas fa-star text-yellow-500 mt-0.5"></i>';
                    html += '<p class="text-sm text-slate-700">' + h + '</p>';
                    html += '</div></div>';
                });
                html += '</div>';
            }
            
            html += '<div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-4">';
            html += '<div class="bg-slate-50 rounded-xl p-4 border border-slate-100">';
            html += '<h4 class="text-xs font-bold text-slate-500 uppercase tracking-wider mb-2"><i class="fas fa-hard-hat mr-1"></i> 開工情況</h4>';
            html += '<p class="text-sm text-slate-700">' + data.work_analysis + '</p>';
            html += '</div>';
            html += '<div class="bg-slate-50 rounded-xl p-4 border border-slate-100">';
            html += '<h4 class="text-xs font-bold text-slate-500 uppercase tracking-wider mb-2"><i class="fas fa-chart-line mr-1"></i> 財務狀況</h4>';
            html += '<p class="text-sm text-slate-700">' + data.financial_insight + '</p>';
            html += '</div>';
            html += '</div>';
            
            if (data.suggestion) {
                html += '<div class="bg-amber-50 border border-amber-200 rounded-xl p-4 flex items-start gap-3">';
                html += '<i class="fas fa-lightbulb text-amber-500 mt-0.5"></i>';
                html += '<div><p class="text-xs font-bold text-amber-700 uppercase tracking-wider">AI 建議</p>';
                html += '<p class="text-sm text-amber-800 mt-1">' + data.suggestion + '</p></div>';
                html += '</div>';
            }
            
            container.innerHTML = html;
        } else {
            container.innerHTML = '<div class="text-center py-10 text-red-500"><i class="fas fa-exclamation-circle text-3xl mb-3 block"></i><p>' + (data.error || '生成失敗') + '</p></div>';
        }
    })
    .catch(error => {
        icon.className = 'fas fa-sync-alt';
        container.innerHTML = '<div class="text-center py-10 text-red-500"><i class="fas fa-exclamation-circle text-3xl mb-3 block"></i><p>請求失敗：' + error.message + '</p></div>';
    });
}
</script>

<?php include __DIR__ . '/../includes/footer.php'; ?>
