<?php
include('includes/auth.php');

$table_name = 'devices';

// Delete device
if (isset($_GET['delete'])) {
    $db->delete($table_name, 'id = :id', [':id' => (int) $_GET['delete']]);
    header("Location: devices.php?status=2");
    exit;
}

// Clear all devices
if (isset($_GET['clear_all'])) {
    $db->delete($table_name);
    header("Location: devices.php?status=2");
    exit;
}

// --- All redirects handled, now output HTML ---
include('includes/header.php');

$res = $db->select($table_name, '*', '', 'last_seen DESC');
?>

<div class="md-card md-card--elevated">
    <div class="md-card__header">
        <?= icon('devices', 'md-card__header-icon') ?>
        <h2 class="md-card__title">Connected Devices</h2>
        <div class="md-card__header-actions">
            <span class="md-chip md-chip--info">
                <?= icon('smartphone') ?>
                <?= count($res) ?> device(s)
            </span>
            <?php if (count($res) > 0): ?>
            <button class="md-button md-button--outlined md-button--error"
                    onclick="if(confirm('Clear ALL devices? This cannot be undone.')) window.location='devices.php?clear_all=1'">
                <?= icon('delete_sweep') ?>
                <span class="md-button__label">Clear All</span>
            </button>
            <?php endif; ?>
        </div>
    </div>
    <div class="md-card__content md-card__content--table">
        <?php if (empty($res)): ?>
            <div class="md-empty-state">
                <?= icon('devices', 'md-empty-state__icon') ?>
                <p class="md-empty-state__text">No devices have connected yet.</p>
            </div>
        <?php else: ?>
        <div class="md-table-container">
            <table class="md-table">
                <thead>
                    <tr>
                        <th>Device ID</th>
                        <th>MAC Address</th>
                        <th>IP Address</th>
                        <th>Version</th>
                        <th>First Seen</th>
                        <th>Last Seen</th>
                        <th>Action</th>
                        <th class="md-table__actions-col">Delete</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($res as $row): ?>
                    <tr>
                        <td title="<?= htmlspecialchars($row['device_id'] ?? '') ?>">
                            <code><?= htmlspecialchars(substr($row['device_id'] ?? '', 0, 12)) ?>…</code>
                        </td>
                        <td><?= htmlspecialchars($row['mac_address'] ?: '—') ?></td>
                        <td><?= htmlspecialchars($row['ip_address'] ?: '—') ?></td>
                        <td>
                            <span class="md-chip md-chip--small">
                                <?= htmlspecialchars($row['version_code'] ?: 'N/A') ?>
                            </span>
                        </td>
                        <td><?= htmlspecialchars($row['first_seen'] ?: '—') ?></td>
                        <td><?= htmlspecialchars($row['last_seen'] ?: '—') ?></td>
                        <td>
                            <span class="md-chip md-chip--<?= ($row['action'] ?? '') === 'getDevices_v3' ? 'success' : 'primary' ?>">
                                <?= htmlspecialchars($row['action'] ?: 'unknown') ?>
                            </span>
                        </td>
                        <td class="md-table__actions">
                            <button class="md-icon-button md-icon-button--error" title="Delete"
                                    onclick="showDeleteDialog('devices.php?delete=<?= (int) $row['id'] ?>')">
                                <?= icon('delete') ?>
                            </button>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </div>
</div>

<?php include('includes/footer.php'); ?>
