<?php include __DIR__ . '/../header.php'; ?>

<div class="container py-4">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <!-- Search bar at the top of the form for quick access -->
            <div class="mb-4">
                <form action="/admin/apks" method="GET">
                    <div class="input-group shadow-sm">
                        <span class="input-group-text bg-white border-0"><i class="fas fa-search text-primary"></i></span>
                        <input type="text" name="search" class="form-control border-0" placeholder="Search for existing APKs to edit..." aria-label="Search APKs">
                        <button class="btn btn-primary" type="submit">Search</button>
                    </div>
                </form>
            </div>

            <div class="card shadow-lg border-0 rounded-4">
                <div class="card-header bg-white py-3">
                    <h5 class="m-0 fw-bold"><?php echo $action === 'add' ? 'Add New APK' : 'Edit APK: ' . htmlspecialchars($apk['title']); ?></h5>
                </div>
                <div class="card-body p-4">
                    <form action="<?php echo $action === 'add' ? '/admin/apks/add' : '/admin/apks/edit/' . $apk['id']; ?>" method="POST" enctype="multipart/form-data">
                        <div class="mb-3">
                            <label for="title" class="form-label fw-medium">APK Title</label>
                            <input type="text" class="form-control" id="title" name="title" value="<?php echo htmlspecialchars($apk['title'] ?? ''); ?>" required>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="version" class="form-label fw-medium">Version</label>
                                <input type="text" class="form-control" id="version" name="version" value="<?php echo htmlspecialchars($apk['version'] ?? ''); ?>" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="category_id" class="form-label fw-medium"><?php echo __('categories'); ?></label>
                                <select class="form-select" id="category_id" name="category_id">
                                    <option value="">Uncategorized</option>
                                    <?php foreach ($categories as $cat): ?>
                                        <option value="<?php echo $cat['id']; ?>" <?php echo (isset($apk['category_id']) && $apk['category_id'] == $cat['id']) ? 'selected' : ''; ?>><?php echo htmlspecialchars($cat['name']); ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>

                        <div class="mb-3">
                            <label for="description" class="form-label fw-medium">Description</label>
                            <textarea class="form-control" id="description" name="description" rows="4" required><?php echo htmlspecialchars($apk['description'] ?? ''); ?></textarea>
                        </div>

                        <div class="mb-4">
                            <label class="form-label fw-medium">APK Icon</label>
                            <div class="d-flex align-items-center mb-2">
                                <?php if (!empty($apk['icon_path'])): ?>
                                    <img src="/<?php echo $apk['icon_path']; ?>" class="rounded me-3 border shadow-sm" width="64" height="64">
                                <?php endif; ?>
                                <div class="flex-grow-1">
                                    <input type="file" class="form-control" id="icon_file" name="icon_file" accept="image/*">
                                    <div class="form-text text-muted"><i class="fas fa-info-circle me-1"></i> Icons are automatically compressed to optimize speed.</div>
                                </div>
                            </div>
                            <div class="mb-3">
                                <label for="image_url" class="form-label fw-medium">External Icon URL (Optional)</label>
                                <input type="url" class="form-control" id="image_url" name="image_url" value="<?php echo htmlspecialchars($apk['image_url'] ?? ''); ?>" placeholder="https://example.com/icon.png">
                            </div>
                        </div>

                        <div class="mb-3">
                            <label for="download_url" class="form-label fw-medium">Download URL</label>
                            <input type="url" class="form-control" id="download_url" name="download_url" value="<?php echo htmlspecialchars($apk['download_url'] ?? ''); ?>" required>
                        </div>

                        <div class="row mb-4">
                            <div class="col-md-6">
                                <label for="status" class="form-label fw-medium">Status</label>
                                <select class="form-select" id="status" name="status">
                                    <option value="published" <?php echo (isset($apk['status']) && $apk['status'] === 'published') ? 'selected' : ''; ?>>Published</option>
                                    <option value="draft" <?php echo (isset($apk['status']) && $apk['status'] === 'draft') ? 'selected' : ''; ?>>Draft</option>
                                </select>
                            </div>
                            <div class="col-md-6 d-flex align-items-center mt-4">
                                <div class="form-check form-switch">
                                    <input class="form-check-input" type="checkbox" id="is_vip" name="is_vip" <?php echo (isset($apk['is_vip']) && $apk['is_vip']) ? 'checked' : ''; ?>>
                                    <label class="form-check-label fw-medium" for="is_vip">VIP APK</label>
                                </div>
                            </div>
                        </div>

                        <div class="d-grid gap-2 d-md-flex justify-content-md-end border-top pt-4">
                            <a href="/admin/apks" class="btn btn-light px-4 fw-bold">Cancel</a>
                            <button type="submit" class="btn btn-primary px-5 fw-bold"><i class="fas fa-save me-2"></i>Save APK</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include __DIR__ . '/../footer.php'; ?>