<?php
/**
 * Admin Theme Settings
 * Digital Product Store
 */

$page_title = 'Theme Settings';
include __DIR__ . '/includes/header.php';

$error = '';
$success = '';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['update_theme'])) {
        $primary_color = sanitize($_POST['primary_color'] ?? '#6366f1');
        $secondary_color = sanitize($_POST['secondary_color'] ?? '#4f46e5');
        
        update_setting('theme_primary_color', $primary_color);
        update_setting('theme_secondary_color', $secondary_color);
        
        // Reload settings
        $settings = get_all_settings();
        
        $success = 'Theme colors updated successfully.';
    }
    
    // Handle logo upload
    if (isset($_FILES['logo']) && $_FILES['logo']['error'] === UPLOAD_ERR_OK) {
        $upload = upload_file($_FILES['logo'], 'images', ['jpg', 'jpeg', 'png', 'gif']);
        if ($upload['success']) {
            // Delete old logo
            $old_logo = get_setting('logo_url');
            if ($old_logo) {
                delete_file($old_logo);
            }
            update_setting('logo_url', $upload['path']);
            $settings = get_all_settings();
            $success = 'Logo uploaded successfully.';
        } else {
            $error = 'Failed to upload logo: ' . $upload['message'];
        }
    }
    
    // Handle favicon upload
    if (isset($_FILES['favicon']) && $_FILES['favicon']['error'] === UPLOAD_ERR_OK) {
        $upload = upload_file($_FILES['favicon'], 'images', ['ico', 'png']);
        if ($upload['success']) {
            // Delete old favicon
            $old_favicon = get_setting('favicon_url');
            if ($old_favicon) {
                delete_file($old_favicon);
            }
            update_setting('favicon_url', $upload['path']);
            $settings = get_all_settings();
            $success = 'Favicon uploaded successfully.';
        } else {
            $error = 'Failed to upload favicon: ' . $upload['message'];
        }
    }
    
    // Handle logo deletion
    if (isset($_POST['delete_logo'])) {
        $logo = get_setting('logo_url');
        if ($logo) {
            delete_file($logo);
            update_setting('logo_url', '');
            $settings = get_all_settings();
            $success = 'Logo removed successfully.';
        }
    }
    
    // Handle favicon deletion
    if (isset($_POST['delete_favicon'])) {
        $favicon = get_setting('favicon_url');
        if ($favicon) {
            delete_file($favicon);
            update_setting('favicon_url', '');
            $settings = get_all_settings();
            $success = 'Favicon removed successfully.';
        }
    }
}

$primary_color = get_setting('theme_primary_color', '#6366f1');
$secondary_color = get_setting('theme_secondary_color', '#4f46e5');
$logo_url = get_setting('logo_url', '');
$favicon_url = get_setting('favicon_url', '');
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h4 class="mb-0">Theme Settings</h4>
</div>

<?php if ($error): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>

<?php if ($success): ?>
<div class="alert alert-success"><?php echo $success; ?></div>
<?php endif; ?>

<div class="row">
    <!-- Logo & Favicon -->
    <div class="col-lg-6">
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-header bg-white">
                <h5 class="mb-0"><i class="fas fa-image"></i> Logo & Favicon</h5>
            </div>
            <div class="card-body">
                <!-- Logo -->
                <div class="mb-4">
                    <label class="form-label">Site Logo</label>
                    <?php if ($logo_url): ?>
                    <div class="mb-3">
                        <img src="<?php echo '../' . $logo_url; ?>" alt="Current Logo" class="img-fluid" style="max-height: 100px;">
                    </div>
                    <form method="POST" class="d-inline">
                        <button type="submit" name="delete_logo" class="btn btn-sm btn-outline-danger mb-3" onclick="return confirm('Remove logo?')">
                            <i class="fas fa-trash"></i> Remove Logo
                        </button>
                    </form>
                    <?php endif; ?>
                    <form method="POST" enctype="multipart/form-data">
                        <input type="file" name="logo" class="form-control" accept="image/*">
                        <div class="form-text">Recommended: PNG with transparent background, max height 60px</div>
                        <button type="submit" class="btn btn-primary mt-2">
                            <i class="fas fa-upload"></i> Upload Logo
                        </button>
                    </form>
                </div>
                
                <hr>
                
                <!-- Favicon -->
                <div class="mb-0">
                    <label class="form-label">Favicon</label>
                    <?php if ($favicon_url): ?>
                    <div class="mb-3">
                        <img src="<?php echo '../' . $favicon_url; ?>" alt="Current Favicon" style="width: 32px; height: 32px;">
                    </div>
                    <form method="POST" class="d-inline">
                        <button type="submit" name="delete_favicon" class="btn btn-sm btn-outline-danger mb-3" onclick="return confirm('Remove favicon?')">
                            <i class="fas fa-trash"></i> Remove Favicon
                        </button>
                    </form>
                    <?php endif; ?>
                    <form method="POST" enctype="multipart/form-data">
                        <input type="file" name="favicon" class="form-control" accept=".ico,.png">
                        <div class="form-text">Recommended: 32x32px ICO or PNG file</div>
                        <button type="submit" class="btn btn-primary mt-2">
                            <i class="fas fa-upload"></i> Upload Favicon
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Color Theme -->
    <div class="col-lg-6">
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-header bg-white">
                <h5 class="mb-0"><i class="fas fa-palette"></i> Color Theme</h5>
            </div>
            <div class="card-body">
                <form method="POST">
                    <input type="hidden" name="update_theme" value="1">
                    
                    <div class="mb-4">
                        <label class="form-label">Primary Color</label>
                        <div class="input-group">
                            <input type="color" name="primary_color" class="form-control form-control-color" value="<?php echo $primary_color; ?>" style="width: 60px;">
                            <input type="text" class="form-control" value="<?php echo $primary_color; ?>" readonly>
                        </div>
                        <div class="form-text">Main brand color used for buttons, links, and highlights</div>
                    </div>
                    
                    <div class="mb-4">
                        <label class="form-label">Secondary Color</label>
                        <div class="input-group">
                            <input type="color" name="secondary_color" class="form-control form-control-color" value="<?php echo $secondary_color; ?>" style="width: 60px;">
                            <input type="text" class="form-control" value="<?php echo $secondary_color; ?>" readonly>
                        </div>
                        <div class="form-text">Used for hover states and secondary elements</div>
                    </div>
                    
                    <!-- Preview -->
                    <div class="mb-4">
                        <label class="form-label">Preview</label>
                        <div class="p-3 border rounded" style="background: #f8f9fa;">
                            <button class="btn mb-2" style="background: <?php echo $primary_color; ?>; color: white;">Primary Button</button>
                            <button class="btn" style="background: <?php echo $secondary_color; ?>; color: white;">Secondary Button</button>
                        </div>
                    </div>
                    
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save"></i> Save Theme Colors
                    </button>
                </form>
            </div>
        </div>
        
        <!-- Theme CSS -->
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white">
                <h5 class="mb-0"><i class="fas fa-code"></i> Custom CSS</h5>
            </div>
            <div class="card-body">
                <p class="text-muted">Add your custom CSS below to further customize the theme:</p>
                <form method="POST" action="settings.php">
                    <textarea name="custom_css" class="form-control" rows="6" placeholder="/* Your custom CSS here */"><?php echo get_setting('custom_css', ''); ?></textarea>
                    <div class="form-text">This CSS will be loaded on all pages</div>
                    <a href="settings.php" class="btn btn-primary mt-2">
                        <i class="fas fa-edit"></i> Edit in Settings
                    </a>
                </form>
            </div>
        </div>
    </div>
</div>

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