{% extends "ewoooc_base.html" %}
{% block title %}郵件發送記錄 - EwoooC{% endblock %}
{% block extra_css %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/page-vendor-tools.css') }}">
{% endblock %}

{% block content %}
<section class="vendor-tools-page" data-vendor-tool="email">
  <header class="vendor-tools-header">
    <div>
      <span class="vendor-tools-kicker"><i class="fas fa-boxes-stacked"></i> Vendor Stockout</span>
      <h1>郵件發送記錄</h1>
      <p>追蹤廠商缺貨通知的發送狀態、成功率與錯誤訊息，方便營運快速補救。</p>
    </div>
    <div class="vendor-tools-nav" aria-label="廠商缺貨工具導覽">
    <a href="/vendor-stockout/" ><i class="fas fa-table-columns"></i>總覽</a>
    <a href="/vendor-stockout/list" ><i class="fas fa-list-check"></i>缺貨清單</a>
    <a href="/vendor-stockout/import" ><i class="fas fa-file-arrow-up"></i>匯入</a>
    <a href="/vendor-stockout/vendor-management" ><i class="fas fa-building"></i>廠商</a>
    <a href="/vendor-stockout/send-email" class="is-active" aria-current="page"><i class="fas fa-paper-plane"></i>郵件記錄</a>
    <a href="/vendor-stockout/history" ><i class="fas fa-clock-rotate-left"></i>歷史</a>
</div>
  </header>

  <div class="vendor-tools-body">
<div class="container-fluid" style="padding: 2rem;">
        <!-- 標題 -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2 class="mb-0">
                <i class="fas fa-paper-plane text-primary me-3"></i>郵件發送記錄
            </h2>
            <a href="/vendor-stockout" class="btn btn-outline-secondary">
                <i class="fas fa-home me-2"></i>返回主頁
            </a>
        </div>

        <!-- 統計卡片 -->
        <div class="row mb-4">
            <div class="col-md-3 mb-3">
                <div class="card stats-card">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <div class="text-muted small">總發送數</div>
                                <h3 class="mb-0" id="statTotal">0</h3>
                            </div>
                            <div class="fs-1 text-primary">
                                <i class="fas fa-envelope"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card stats-card">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <div class="text-muted small">成功</div>
                                <h3 class="mb-0 text-success" id="statSuccess">0</h3>
                            </div>
                            <div class="fs-1 text-success">
                                <i class="fas fa-check-circle"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card stats-card">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <div class="text-muted small">失敗</div>
                                <h3 class="mb-0 text-danger" id="statFailed">0</h3>
                            </div>
                            <div class="fs-1 text-danger">
                                <i class="fas fa-times-circle"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card stats-card">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <div class="text-muted small">成功率</div>
                                <h3 class="mb-0 text-info" id="statRate">0%</h3>
                            </div>
                            <div class="fs-1 text-info">
                                <i class="fas fa-chart-line"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 篩選區 -->
        <div class="card mb-3" style="border-radius: 12px;">
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label small">狀態</label>
                        <select class="form-select" id="filterStatus">
                            <option value="">全部</option>
                            <option value="sent">成功</option>
                            <option value="failed">失敗</option>
                            <option value="pending">待發送</option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label small">廠商代碼</label>
                        <input type="text" class="form-control" id="filterVendor" placeholder="輸入廠商代碼">
                    </div>
                    <div class="col-md-2">
                        <label class="form-label small">開始日期</label>
                        <input type="date" class="form-control" id="filterDateFrom">
                    </div>
                    <div class="col-md-2">
                        <label class="form-label small">結束日期</label>
                        <input type="date" class="form-control" id="filterDateTo">
                    </div>
                    <div class="col-md-2 d-flex align-items-end">
                        <button class="btn btn-primary w-100" onclick="loadData()">
                            <i class="fas fa-search me-2"></i>查詢
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <!-- 資料表格 -->
        <div class="table-container">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>批次編號</th>
                            <th>廠商</th>
                            <th>收件者</th>
                            <th>主旨</th>
                            <th>商品數</th>
                            <th>狀態</th>
                            <th>發送時間</th>
                        </tr>
                    </thead>
                    <tbody id="tableBody">
                        <tr>
                            <td colspan="8" class="text-center text-muted">
                                <i class="fas fa-spinner fa-spin me-2"></i>載入中...
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>

            <!-- 分頁 -->
            <div class="d-flex justify-content-between align-items-center mt-3">
                <div class="text-muted">
                    共 <span id="totalCount">0</span> 筆記錄
                </div>
                <nav>
                    <ul class="pagination mb-0" id="pagination"></ul>
                </nav>
            </div>
        </div>
    </div>
  </div>
</section>
{% endblock %}

{% block extra_js %}
<script>
        let currentPage = 1;
        const pageSize = 20;

        // 載入統計資料
        async function loadStats() {
            try {
                const response = await fetch('/vendor-stockout/api/email/stats');
                const result = await response.json();

                if (result.success) {
                    const data = result.data;
                    document.getElementById('statTotal').textContent = data.total_sent;
                    document.getElementById('statSuccess').textContent = data.success_count;
                    document.getElementById('statFailed').textContent = data.failed_count;
                    document.getElementById('statRate').textContent = data.success_rate + '%';
                }
            } catch (error) {
                console.error('載入統計資料失敗:', error);
            }
        }

        // 載入郵件記錄
        async function loadData(page = 1) {
            currentPage = page;

            const status = document.getElementById('filterStatus').value;
            const vendor = document.getElementById('filterVendor').value;
            const dateFrom = document.getElementById('filterDateFrom').value;
            const dateTo = document.getElementById('filterDateTo').value;

            const params = new URLSearchParams({
                page: page,
                page_size: pageSize
            });

            if (status) params.append('status', status);
            if (vendor) params.append('vendor_code', vendor);
            if (dateFrom) params.append('date_from', dateFrom);
            if (dateTo) params.append('date_to', dateTo);

            try {
                const response = await fetch(`/vendor-stockout/api/email/logs?${params}`);
                const result = await response.json();

                if (result.success) {
                    renderTable(result.data);
                    renderPagination(result.total, page);
                    document.getElementById('totalCount').textContent = result.total;
                } else {
                    alert('查詢失敗: ' + result.message);
                }
            } catch (error) {
                console.error('載入資料失敗:', error);
                alert('載入資料失敗: ' + error.message);
            }
        }

        // 渲染表格
        function renderTable(data) {
            const tbody = document.getElementById('tableBody');

            if (data.length === 0) {
                tbody.innerHTML = `
                    <tr>
                        <td colspan="8" class="text-center text-muted">
                            <i class="fas fa-inbox me-2"></i>沒有記錄
                        </td>
                    </tr>
                `;
                return;
            }

            tbody.innerHTML = data.map(item => {
                const statusClass = item.status === 'sent' ? 'status-sent' :
                                   item.status === 'failed' ? 'status-failed' : 'status-pending';
                const statusText = item.status === 'sent' ? '成功' :
                                  item.status === 'failed' ? '失敗' : '待發送';

                return `
                    <tr>
                        <td>${item.id}</td>
                        <td><small>${item.batch_id}</small></td>
                        <td>
                            <div><strong>${item.vendor_name}</strong></div>
                            <small class="text-muted">${item.vendor_code}</small>
                        </td>
                        <td><small>${item.recipient_email}</small></td>
                        <td style="max-width: 300px;">
                            <small>${item.subject}</small>
                            ${item.error_message ? `<div class="text-danger small mt-1">❌ ${item.error_message}</div>` : ''}
                        </td>
                        <td>${item.product_count}</td>
                        <td><span class="status-badge ${statusClass}">${statusText}</span></td>
                        <td><small>${item.sent_at || item.created_at}</small></td>
                    </tr>
                `;
            }).join('');
        }

        // 渲染分頁
        function renderPagination(total, current) {
            const totalPages = Math.ceil(total / pageSize);
            const pagination = document.getElementById('pagination');

            if (totalPages <= 1) {
                pagination.innerHTML = '';
                return;
            }

            let html = '';

            // 上一頁
            html += `
                <li class="page-item ${current === 1 ? 'disabled' : ''}">
                    <a class="page-link" href="#" onclick="loadData(${current - 1}); return false;">上一頁</a>
                </li>
            `;

            // 頁碼
            for (let i = 1; i <= totalPages; i++) {
                if (i === 1 || i === totalPages || (i >= current - 2 && i <= current + 2)) {
                    html += `
                        <li class="page-item ${i === current ? 'active' : ''}">
                            <a class="page-link" href="#" onclick="loadData(${i}); return false;">${i}</a>
                        </li>
                    `;
                } else if (i === current - 3 || i === current + 3) {
                    html += '<li class="page-item disabled"><span class="page-link">...</span></li>';
                }
            }

            // 下一頁
            html += `
                <li class="page-item ${current === totalPages ? 'disabled' : ''}">
                    <a class="page-link" href="#" onclick="loadData(${current + 1}); return false;">下一頁</a>
                </li>
            `;

            pagination.innerHTML = html;
        }

        // 初始化
        document.addEventListener('DOMContentLoaded', function() {
            loadStats();
            loadData(1);

            // 每 30 秒自動重新整理統計資料
            setInterval(loadStats, 30000);
        });
    </script>
{% endblock %}
