<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/session.php';

// Check permissions
if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

// Stats
$stats = [
    'total' => 0,
    'submitted' => 0,
    'pending' => 0,
    'failed' => 0
];

$sql = "SELECT submission_status, COUNT(*) as count FROM etims_invoices GROUP BY submission_status";
$res = $conn->query($sql);
while ($row = $res->fetch_assoc()) {
    $stats['total'] += $row['count'];
    $stats[$row['submission_status']] = $row['count'];
}

// Recent Errors
$errors = $conn->query("SELECT * FROM etims_api_log WHERE result_code != 200 ORDER BY created_at DESC LIMIT 5");

?>

<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'E-TIMS Dashboard')); ?>
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">

                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">E-TIMS Dashboard</h4>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-xl-3 col-md-6">
                            <div class="card card-animate">
                                <div class="card-body">
                                    <h6 class="text-muted text-uppercase fw-semibold">Total Invoices</h6>
                                    <h4 class="mb-0"><?php echo number_format($stats['total']); ?></h4>
                                </div>
                            </div>
                        </div>
                        <div class="col-xl-3 col-md-6">
                            <div class="card card-animate">
                                <div class="card-body">
                                    <h6 class="text-muted text-uppercase fw-semibold text-success">Submitted</h6>
                                    <h4 class="mb-0 text-success"><?php echo number_format($stats['submitted']); ?></h4>
                                </div>
                            </div>
                        </div>
                        <div class="col-xl-3 col-md-6">
                            <div class="card card-animate">
                                <div class="card-body">
                                    <h6 class="text-muted text-uppercase fw-semibold text-warning">Pending</h6>
                                    <h4 class="mb-0 text-warning"><?php echo number_format($stats['pending']); ?></h4>
                                </div>
                            </div>
                        </div>
                        <div class="col-xl-3 col-md-6">
                            <div class="card card-animate">
                                <div class="card-body">
                                    <h6 class="text-muted text-uppercase fw-semibold text-danger">Failed</h6>
                                    <h4 class="mb-0 text-danger"><?php echo number_format($stats['failed']); ?></h4>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-12">
                            <div class="card">
                                <div class="card-header">
                                    <h5 class="card-title mb-0">Recent API Errors</h5>
                                </div>
                                <div class="card-body">
                                    <table class="table table-bordered table-sm">
                                        <thead>
                                            <tr>
                                                <th>Date</th>
                                                <th>Endpoint</th>
                                                <th>Code</th>
                                                <th>Response</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php while ($err = $errors->fetch_assoc()): ?>
                                                <tr>
                                                    <td><?php echo $err['created_at']; ?></td>
                                                    <td><?php echo htmlspecialchars($err['endpoint']); ?></td>
                                                    <td class="text-danger"><?php echo $err['response_code']; ?></td>
                                                    <td><small><?php echo htmlspecialchars(substr($err['response_body'], 0, 100)) . '...'; ?></small>
                                                    </td>
                                                </tr>
                                            <?php endwhile; ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>

</html>