<?php
$pdo = db();
$users = $pdo->query("SELECT * FROM users WHERE role='user' ORDER BY created_at DESC")->fetchAll();
$now_iso = date('Y-m-d\TH:i:s.uP');
?>
<header class="page-head">
    <h1>Subscribers</h1>
    <p class="page-sub">Create new accounts, extend expiries, change passwords, delete users.</p>
</header>

<section class="card">
    <h2>Create subscriber</h2>
    <form method="post" action="?page=users" class="form-row">
        <input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
        <input type="hidden" name="action" value="create_user">
        <input type="text"     name="username" placeholder="username"     required minlength="2" maxlength="40">
        <input type="text"     name="password" placeholder="password"     required minlength="3">
        <input type="number"   name="days"     placeholder="days"         value="30" min="1" max="3650" required>
        <select name="xtream_mode">
            <option value="shared">Shared Xtream</option>
            <option value="own">Own server</option>
        </select>
        <button type="submit" class="btn-primary">Create</button>
    </form>
    <details style="margin-top:8px">
        <summary class="hint">Per-user Xtream (only when "Own server" selected) →</summary>
        <p class="hint">If you pick "Own server" above, also fill these. Otherwise leave blank.</p>
        <form method="post" action="?page=users" class="form-row">
            <input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
            <input type="hidden" name="action" value="create_user">
            <input type="text"   name="username"        placeholder="username"        required>
            <input type="text"   name="password"        placeholder="password"        required>
            <input type="number" name="days"            placeholder="days" value="30" min="1" max="3650" required>
            <input type="hidden" name="xtream_mode"     value="own">
            <input type="url"    name="xtream_server"   placeholder="http://server:port" style="flex:2">
            <input type="text"   name="xtream_username" placeholder="xtream user">
            <input type="text"   name="xtream_password" placeholder="xtream pass">
            <button type="submit" class="btn-secondary">Create (own server)</button>
        </form>
    </details>
</section>

<section class="card">
    <h2><?= count($users) ?> subscribers</h2>
    <?php if (!$users): ?>
        <p class="hint">No subscribers yet.</p>
    <?php else: ?>
    <div class="table-wrap">
    <table class="users-table">
        <thead>
            <tr>
                <th>Username</th>
                <th>Created</th>
                <th>Expires</th>
                <th>Mode</th>
                <th class="ta-right">Actions</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($users as $u):
            $expired = !empty($u['expiry_at']) && $u['expiry_at'] < $now_iso;
        ?>
            <tr>
                <td><strong><?= h($u['username']) ?></strong></td>
                <td class="muted"><?= h(substr((string)$u['created_at'], 0, 10)) ?></td>
                <td class="<?= $expired ? 'expired' : '' ?>">
                    <?= $u['expiry_at'] ? h(substr($u['expiry_at'], 0, 10)) : '<span class="muted">no expiry</span>' ?>
                    <?= $expired ? ' <span class="badge-bad">EXPIRED</span>' : '' ?>
                </td>
                <td><?= h($u['xtream_mode']) ?></td>
                <td class="ta-right">
                    <details class="row-actions">
                        <summary class="btn-tiny">Manage</summary>
                        <div class="row-action-pop">
                            <form method="post" action="?page=users" class="row-form">
                                <input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
                                <input type="hidden" name="action" value="update_user">
                                <input type="hidden" name="user_id" value="<?= h($u['id']) ?>">
                                <label>Extend by days
                                    <input type="number" name="extend_days" placeholder="+30" step="1">
                                </label>
                                <label>New password
                                    <input type="text" name="new_password" placeholder="leave blank to keep">
                                </label>
                                <details>
                                    <summary>Xtream override</summary>
                                    <select name="xtream_mode">
                                        <option value="<?= h($u['xtream_mode']) ?>" selected><?= h($u['xtream_mode']) ?> (current)</option>
                                        <option value="shared">shared</option>
                                        <option value="own">own</option>
                                    </select>
                                    <input type="url"  name="xtream_server"   placeholder="server" value="<?= h($u['xtream_server']) ?>">
                                    <input type="text" name="xtream_username" placeholder="user"   value="<?= h($u['xtream_username']) ?>">
                                    <input type="text" name="xtream_password" placeholder="pass"   value="<?= h($u['xtream_password']) ?>">
                                </details>
                                <button type="submit" class="btn-secondary">Save</button>
                            </form>
                            <form method="post" action="?page=users" onsubmit="return confirm('Delete user <?= h($u['username']) ?>?')" class="row-form">
                                <input type="hidden" name="csrf" value="<?= h(csrf_token()) ?>">
                                <input type="hidden" name="action" value="delete_user">
                                <input type="hidden" name="user_id" value="<?= h($u['id']) ?>">
                                <button type="submit" class="btn-danger">Delete user</button>
                            </form>
                        </div>
                    </details>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    </div>
    <?php endif; ?>
</section>
