<?php
require_once 'config/database.php';
require_once 'app/helpers/auth.php';
require_once 'app/helpers/rbac.php';
requireLogin();

if (!can('user.manage')) {
    echo "<div class='alert alert-danger m-4'>Access denied</div>";
    exit;
}

$page_js = 'alert_delivery.js';

include 'views/layouts/header.php';
include 'views/layouts/sidebar.php';
?>
<main class="main-content">
<?php include 'views/layouts/topbar.php'; ?>

<div class="container-fluid p-4">
    <div id="alertBox" class="alert d-none"></div>

    <div class="card shadow-sm border-0 mb-4">
        <div class="card-body p-4 p-lg-5">
            <div class="row g-4 align-items-center">
                <div class="col-lg-8">
                    <span class="badge bg-primary bg-opacity-10 text-primary mb-3 px-3 py-2">Alerts</span>
                    <h2 class="mb-2">Alert Delivery</h2>
                    <p class="mb-0 text-muted">Monitor email and SMS alert delivery status across your fleet system.</p>
                </div>
                <div class="col-lg-4 text-lg-end">
                    <button class="btn btn-outline-secondary" id="btnRefresh"><i class="bi bi-arrow-clockwise"></i> Refresh</button>
                </div>
            </div>
        </div>
    </div>

    <div class="row g-4 mb-4">
        <div class="col-12 col-sm-6 col-xl-3">
            <div class="card kpi-card shadow-sm border-0 rounded-4 h-100">
                <div class="card-body py-3 px-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="kpi-label">Total</div>
                            <div class="kpi-value" id="kpiTotal">0</div>
                        </div>
                        <div class="kpi-icon text-primary fs-3"><i class="bi bi-bell"></i></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-12 col-sm-6 col-xl-3">
            <div class="card kpi-card shadow-sm border-0 rounded-4 h-100">
                <div class="card-body py-3 px-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="kpi-label">Sent</div>
                            <div class="kpi-value" id="kpiSent">0</div>
                        </div>
                        <div class="kpi-icon text-success fs-3"><i class="bi bi-check-circle"></i></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-12 col-sm-6 col-xl-3">
            <div class="card kpi-card shadow-sm border-0 rounded-4 h-100">
                <div class="card-body py-3 px-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="kpi-label">Failed</div>
                            <div class="kpi-value" id="kpiFailed">0</div>
                        </div>
                        <div class="kpi-icon text-danger fs-3"><i class="bi bi-exclamation-triangle"></i></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-12 col-sm-6 col-xl-3">
            <div class="card kpi-card shadow-sm border-0 rounded-4 h-100">
                <div class="card-body py-3 px-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <div class="kpi-label">Pending / Other</div>
                            <div class="kpi-value" id="kpiOther">0</div>
                        </div>
                        <div class="kpi-icon text-secondary fs-3"><i class="bi bi-clock"></i></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="card shadow-sm border-0 mb-4">
        <div class="card-body py-3 px-3" id="alertSettings">
            <div class="text-center text-muted">Loading settings...</div>
        </div>
    </div>

    <div class="card shadow-sm border-0">
        <div class="card-body">
            <div class="row mb-3">
                <div class="col-md-4">
                    <input type="text" id="searchBox" class="form-control" placeholder="Search event, channel or recipient...">
                </div>
                <div class="col-md-3">
                    <select id="alertStatusFilter" class="form-select">
                        <option value="">All statuses</option>
                        <option value="sent">Sent</option>
                        <option value="failed">Failed</option>
                        <option value="pending">Pending</option>
                        <option value="skipped">Skipped</option>
                    </select>
                </div>
            </div>
            <div class="table-responsive">
                <table class="table align-middle mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>Created</th>
                            <th>Event</th>
                            <th>Channel</th>
                            <th>Recipient</th>
                            <th>Status</th>
                            <th>Attempts</th>
                            <th>Response</th>
                            <th class="text-end">Actions</th>
                        </tr>
                    </thead>
                    <tbody id="alertDeliveryTable">
                        <tr><td colspan="8" class="text-center text-muted py-4">Loading...</td></tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

</main>

<?php include 'views/layouts/footer.php'; ?>
