<?php
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

// Check if admin is logged in
if (empty($_SESSION['is_admin']) || $_SESSION['is_admin'] !== true) {
    header('Location: admin_login.php');
    exit;
}

require_once __DIR__ . '/helpers/database_helper.php';

// Load footer configuration
$footerConfig = DatabaseHelper::loadJson('footer_config.json', []);

$message = '';
$messageType = '';

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['action']) && $_POST['action'] === 'update_footer') {
        // Update Contact Info
        $footerConfig['columns']['contact']['title'] = htmlspecialchars($_POST['contact_title'] ?? 'Contact Us');
        $footerConfig['columns']['contact']['icon'] = htmlspecialchars($_POST['contact_icon'] ?? '📍');
        
        // Update Stores
        $footerConfig['columns']['stores']['title'] = htmlspecialchars($_POST['stores_title'] ?? 'Our Stores');
        $footerConfig['columns']['stores']['icon'] = htmlspecialchars($_POST['stores_icon'] ?? '🏪');
        
        // Update Links
        $footerConfig['columns']['links']['title'] = htmlspecialchars($_POST['links_title'] ?? 'Useful Links');
        $footerConfig['columns']['links']['icon'] = htmlspecialchars($_POST['links_icon'] ?? '🔗');
        
        // Update Newsletter
        $footerConfig['columns']['newsletter']['title'] = htmlspecialchars($_POST['newsletter_title'] ?? 'Newsletter');
        $footerConfig['columns']['newsletter']['icon'] = htmlspecialchars($_POST['newsletter_icon'] ?? '📧');
        $footerConfig['columns']['newsletter']['description'] = htmlspecialchars($_POST['newsletter_description'] ?? '');
        $footerConfig['columns']['newsletter']['button_text'] = htmlspecialchars($_POST['newsletter_button'] ?? 'Subscribe');
        
        // Update Bottom Section
        $footerConfig['bottom_section']['copyright'] = htmlspecialchars($_POST['copyright'] ?? '© ' . date('Y') . ' Woo Fashion. All rights reserved.');
        
        if (DatabaseHelper::saveJson('footer_config.json', $footerConfig)) {
            $message = 'Footer configuration updated successfully!';
            $messageType = 'success';
        } else {
            $message = 'Error saving footer configuration.';
            $messageType = 'error';
        }
    }
}

// include 'admin_sidebar.php';
?>

<style>
    .admin-content-area {
        padding: 30px;
        margin-left: 320px;
        background: #f9f9f9;
        min-height: calc(100vh - 200px);
    }

    .admin-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 30px;
    }

    .admin-header h1 {
        font-size: 28px;
        margin: 0;
        color: #333;
    }

    .back-btn {
        background: #667eea;
        color: #fff;
        padding: 10px 20px;
        text-decoration: none;
        border-radius: 6px;
        font-size: 14px;
    }

    .back-btn:hover {
        background: #5568d3;
    }

    .message {
        padding: 15px 20px;
        border-radius: 6px;
        margin-bottom: 20px;
        font-weight: 600;
    }

    .message.success {
        background: #d4edda;
        color: #155724;
        border: 1px solid #c3e6cb;
    }

    .message.error {
        background: #f8d7da;
        color: #721c24;
        border: 1px solid #f5c6cb;
    }

    .form-section {
        background: #fff;
        padding: 25px;
        border-radius: 8px;
        margin-bottom: 25px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    }

    .form-section h2 {
        font-size: 18px;
        margin-top: 0;
        margin-bottom: 20px;
        color: #333;
        border-bottom: 2px solid #F07E2D;
        padding-bottom: 10px;
    }

    .form-group {
        margin-bottom: 20px;
    }

    .form-group label {
        display: block;
        margin-bottom: 8px;
        font-weight: 600;
        color: #333;
        font-size: 14px;
    }

    .form-group input[type="text"],
    .form-group input[type="email"],
    .form-group textarea,
    .form-group select {
        width: 100%;
        padding: 10px 12px;
        border: 1px solid #ddd;
        border-radius: 6px;
        font-size: 14px;
        font-family: inherit;
    }

    .form-group input[type="text"]:focus,
    .form-group input[type="email"]:focus,
    .form-group textarea:focus,
    .form-group select:focus {
        outline: none;
        border-color: #F07E2D;
        box-shadow: 0 0 0 3px rgba(225, 123, 169, 0.1);
    }

    .form-group textarea {
        resize: vertical;
        min-height: 80px;
    }

    .grid-2 {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 20px;
    }

    .grid-3 {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 20px;
    }

    .column-box {
        border: 1px solid #e0e0e0;
        border-radius: 8px;
        padding: 20px;
        background: #f9f9f9;
    }

    .column-box h3 {
        margin-top: 0;
        color: #667eea;
        font-size: 15px;
    }

    .btn-group {
        display: flex;
        gap: 10px;
        margin-top: 25px;
        justify-content: flex-end;
    }

    .btn {
        padding: 12px 24px;
        border: none;
        border-radius: 6px;
        font-size: 14px;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
    }

    .btn-primary {
        background: #F07E2D;
        color: #fff;
    }

    .btn-primary:hover {
        background: #c76b8a;
    }

    .help-text {
        font-size: 12px;
        color: #999;
        margin-top: 5px;
    }

    .info-box {
        background: #e3f2fd;
        border-left: 4px solid #667eea;
        padding: 15px;
        border-radius: 4px;
        margin-top: 20px;
        font-size: 13px;
        color: #1565c0;
    }

    @media (max-width: 1024px) {
        .grid-3 {
            grid-template-columns: 1fr 1fr;
        }
    }

    @media (max-width: 768px) {
        .grid-2,
        .grid-3 {
            grid-template-columns: 1fr;
        }
    }
</style>

<div class="admin-content-area">
    <div class="admin-header">
        <h1>Footer Management</h1>

<?php
// Default link
$dashboardLink = "admin_dashboard.php";

// Adjust based on role
if (isset($_SESSION['role'])) {
    switch ($_SESSION['role']) {
        case 'admin':
            $dashboardLink = "admin_dashboard.php";
            break;
        case 'subadmin':
            $dashboardLink = "subadmin_dashboard.php";
            break;
        case 'editor':
            $dashboardLink = "subadmin_dashboard.php";
            break;
        default:
            $dashboardLink = "admin_dashboard.php"; // fallback
    }
}
?>

<a href="<?php echo $dashboardLink; ?>" class="back-btn">← Back to Dashboard</a>

<style>
.back-btn {
    background-color: orange;
    color: white;
    padding: 8px 14px;
    text-decoration: none;
    border-radius: 4px;
    font-size: 14px;
    font-weight: 500;
}
.back-btn:hover {
    background-color: darkorange;
}
</style>
        <!-- <a href="admin_dashboard.php" class="back-btn">← Back to Dashboard</a> -->
   
    </div>

    <?php if ($message): ?>
    <div class="message <?php echo $messageType; ?>">
        <?php echo htmlspecialchars($message); ?>
    </div>
    <?php endif; ?>

    <!-- Footer Configuration Form -->
    <form method="POST" action="">
        <input type="hidden" name="action" value="update_footer">

        <!-- Column Titles and Icons -->
        <div class="form-section">
            <h2>Column Headers</h2>
            
            <div class="grid-3">
                <!-- Contact Column -->
                <div class="column-box">
                    <h3>📍 Contact Us</h3>
                    <div class="form-group">
                        <label for="contact_title">Title</label>
                        <input type="text" id="contact_title" name="contact_title" value="<?php echo htmlspecialchars($footerConfig['columns']['contact']['title'] ?? 'Contact Us'); ?>" placeholder="Contact Us">
                    </div>
                    <div class="form-group">
                        <label for="contact_icon">Icon</label>
                        <input type="text" id="contact_icon" name="contact_icon" value="<?php echo htmlspecialchars($footerConfig['columns']['contact']['icon'] ?? '📍'); ?>" placeholder="📍" maxlength="2">
                        <div class="help-text">Use emoji or single character</div>
                    </div>
                </div>

                <!-- Stores Column -->
                <div class="column-box">
                    <h3>🏪 Our Stores</h3>
                    <div class="form-group">
                        <label for="stores_title">Title</label>
                        <input type="text" id="stores_title" name="stores_title" value="<?php echo htmlspecialchars($footerConfig['columns']['stores']['title'] ?? 'Our Stores'); ?>" placeholder="Our Stores">
                    </div>
                    <div class="form-group">
                        <label for="stores_icon">Icon</label>
                        <input type="text" id="stores_icon" name="stores_icon" value="<?php echo htmlspecialchars($footerConfig['columns']['stores']['icon'] ?? '🏪'); ?>" placeholder="🏪" maxlength="2">
                        <div class="help-text">Use emoji or single character</div>
                    </div>
                </div>

                <!-- Links Column -->
                <div class="column-box">
                    <h3>🔗 Useful Links</h3>
                    <div class="form-group">
                        <label for="links_title">Title</label>
                        <input type="text" id="links_title" name="links_title" value="<?php echo htmlspecialchars($footerConfig['columns']['links']['title'] ?? 'Useful Links'); ?>" placeholder="Useful Links">
                    </div>
                    <div class="form-group">
                        <label for="links_icon">Icon</label>
                        <input type="text" id="links_icon" name="links_icon" value="<?php echo htmlspecialchars($footerConfig['columns']['links']['icon'] ?? '🔗'); ?>" placeholder="🔗" maxlength="2">
                        <div class="help-text">Use emoji or single character</div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Newsletter Section -->
        <div class="form-section">
            <h2>Newsletter Column</h2>
            
            <div class="grid-2">
                <div class="form-group">
                    <label for="newsletter_title">Title</label>
                    <input type="text" id="newsletter_title" name="newsletter_title" value="<?php echo htmlspecialchars($footerConfig['columns']['newsletter']['title'] ?? 'Newsletter'); ?>" placeholder="Newsletter">
                </div>

                <div class="form-group">
                    <label for="newsletter_icon">Icon</label>
                    <input type="text" id="newsletter_icon" name="newsletter_icon" value="<?php echo htmlspecialchars($footerConfig['columns']['newsletter']['icon'] ?? '📧'); ?>" placeholder="📧" maxlength="2">
                    <div class="help-text">Use emoji or single character</div>
                </div>
            </div>

            <div class="form-group">
                <label for="newsletter_description">Description</label>
                <textarea id="newsletter_description" name="newsletter_description" placeholder="Enter newsletter subscription description"><?php echo htmlspecialchars($footerConfig['columns']['newsletter']['description'] ?? ''); ?></textarea>
            </div>

            <div class="form-group">
                <label for="newsletter_button">Subscribe Button Text</label>
                <input type="text" id="newsletter_button" name="newsletter_button" value="<?php echo htmlspecialchars($footerConfig['columns']['newsletter']['button_text'] ?? 'Subscribe'); ?>" placeholder="Subscribe">
            </div>
        </div>

        <!-- Copyright Section -->
        <div class="form-section">
            <h2>Bottom Section</h2>
            
            <div class="form-group">
                <label for="copyright">Copyright Text</label>
                <textarea id="copyright" name="copyright" placeholder="© 2025 Woo Fashion. All rights reserved."><?php echo htmlspecialchars($footerConfig['bottom_section']['copyright'] ?? ''); ?></textarea>
            </div>
        </div>

        <div class="form-section">
            <div class="btn-group">
                <button type="submit" class="btn btn-primary">Save Footer Settings</button>
            </div>
        </div>
    </form>

    <div class="info-box">
        <strong>💡 Tips:</strong>
        <ul style="margin: 8px 0; padding-left: 20px;">
            <li>Use emoji icons for a more visual appearance (📍, 🏪, 🔗, 📧)</li>
            <li>Keep column titles short and descriptive</li>
            <li>Newsletter description should be compelling and clear</li>
            <li>Footer displays in a 4-column responsive layout</li>
            <li>Changes appear on the website immediately</li>
        </ul>
    </div>

    <div class="info-box" style="background: #fff3e0; border-left-color: #ff9800; color: #e65100; margin-top: 15px;">
        <strong>📌 Note:</strong> To edit contact information, store locations, and links, please contact support or use direct file editing. Those sections require more detailed management options.
    </div>
</div>

