<?php
$server = $server ?? [];
$groups = $groups ?? [];
$hasPassword = !empty($server['ssh_password']);
$hasKey = !empty($server['ssh_key']);
?>

<div class="page-header">
    <div class="page-header-left">
        <div class="back-link">
            <a href="/servers/<?= $server['id'] ?>"><i class="fas fa-arrow-left"></i> Back to Server</a>
        </div>
        <h1 class="page-title">Edit Server</h1>
        <p class="page-subtitle"><?= htmlspecialchars($server['name'] ?? '') ?></p>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <form method="POST" action="/servers/<?= $server['id'] ?>/edit" class="form">
            <input type="hidden" name="_csrf_token" value="<?= $this->csrfToken() ?>">

            <div class="form-section">
                <h3><i class="fas fa-info-circle"></i> General Information</h3>
                <div class="form-row">
                    <div class="form-group">
                        <label for="name">Server Name *</label>
                        <input type="text" id="name" name="name" class="form-input"
                               value="<?= htmlspecialchars($server['name'] ?? '') ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="group_name">Group / Category</label>
                        <input type="text" id="group_name" name="group_name" class="form-input"
                               value="<?= htmlspecialchars($server['group_name'] ?? '') ?>"
                               list="groupList">
                        <datalist id="groupList">
                            <?php foreach ($groups as $group): ?>
                                <option value="<?= htmlspecialchars($group) ?>">
                            <?php endforeach; ?>
                        </datalist>
                    </div>
                </div>
                <div class="form-group">
                    <label for="description">Description</label>
                    <textarea id="description" name="description" class="form-input" rows="3"><?= htmlspecialchars($server['description'] ?? '') ?></textarea>
                </div>
            </div>

            <div class="form-section">
                <h3><i class="fas fa-plug"></i> Connection Details</h3>
                <div class="form-row form-row-3">
                    <div class="form-group">
                        <label for="ip_address">IP Address / Hostname *</label>
                        <input type="text" id="ip_address" name="ip_address" class="form-input"
                               value="<?= htmlspecialchars($server['ip_address'] ?? '') ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="ssh_port">SSH Port *</label>
                        <input type="number" id="ssh_port" name="ssh_port" class="form-input"
                               value="<?= $server['ssh_port'] ?? 22 ?>" min="1" max="65535" required>
                    </div>
                    <div class="form-group">
                        <label for="ssh_user">SSH User *</label>
                        <input type="text" id="ssh_user" name="ssh_user" class="form-input"
                               value="<?= htmlspecialchars($server['ssh_user'] ?? '') ?>" required>
                    </div>
                </div>
            </div>

            <div class="form-section">
                <h3><i class="fas fa-key"></i> Authentication (leave blank to keep current)</h3>
                <div class="form-group">
                    <label for="ssh_password">New SSH Password</label>
                    <div class="input-group">
                        <input type="password" id="ssh_password" name="ssh_password" class="form-input"
                               placeholder="Enter new password or leave blank">
                        <button type="button" class="input-toggle-password" tabindex="-1">
                            <i class="fas fa-eye"></i>
                        </button>
                    </div>
                    <?php if ($hasPassword): ?>
                        <small class="form-hint">Currently using password authentication.</small>
                    <?php endif; ?>
                </div>
                <div class="form-group">
                    <label for="ssh_key">New SSH Private Key</label>
                    <textarea id="ssh_key" name="ssh_key" class="form-input font-mono" rows="6"
                              placeholder="Paste new private key or leave blank"><?= htmlspecialchars($server['ssh_key'] ?? '') ?></textarea>
                    <?php if ($hasKey): ?>
                        <small class="form-hint">Currently using key-based authentication.</small>
                    <?php endif; ?>
                </div>
            </div>

            <div class="form-section">
                <div class="form-group">
                    <label class="checkbox-label">
                        <input type="checkbox" name="is_active" value="1" <?= ($server['is_active'] ?? 1) ? 'checked' : '' ?>>
                        <span>Server active</span>
                    </label>
                </div>
            </div>

            <div class="form-actions">
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-save"></i> Update Server
                </button>
                <a href="/servers/<?= $server['id'] ?>" class="btn btn-secondary">Cancel</a>
            </div>
        </form>
    </div>
</div>
