<?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 slider settings
$slider = DatabaseHelper::loadJson('slider.json', []);

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

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['action']) && $_POST['action'] === 'update_hero') {
        $slider['hero']['title'] = htmlspecialchars($_POST['title'] ?? 'Women Fashion');
        $slider['hero']['subtitle'] = htmlspecialchars($_POST['subtitle'] ?? 'Let get a look');
        $slider['hero']['button_text'] = htmlspecialchars($_POST['button_text'] ?? 'Shop');
        $slider['hero']['button_link'] = htmlspecialchars($_POST['button_link'] ?? 'shop.php');
        $slider['hero']['position'] = $_POST['position'] ?? 'left';
        $slider['hero']['bold_title'] = isset($_POST['bold_title']) ? true : false;
        $slider['hero']['multiline_enabled'] = isset($_POST['multiline_enabled']) ? true : false;
        
        if (DatabaseHelper::saveJson('slider.json', $slider)) {
            $message = 'Slider hero content updated successfully!';
            $messageType = 'success';
        } else {
            $message = 'Error saving slider settings.';
            $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="url"],
    .form-group select,
    .form-group textarea {
        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="url"]:focus,
    .form-group select:focus,
    .form-group textarea: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;
    }

    .checkbox-group {
        display: flex;
        align-items: center;
        gap: 10px;
        margin-bottom: 15px;
    }

    .checkbox-group input[type="checkbox"] {
        width: 18px;
        height: 18px;
        cursor: pointer;
    }

    .checkbox-group label {
        margin: 0;
        cursor: pointer;
        font-weight: 500;
    }

    .preview-section {
        background: #f9f9f9;
        padding: 20px;
        border-radius: 6px;
        margin-top: 20px;
        border: 1px solid #e0e0e0;
    }

    .preview-section h3 {
        margin-top: 0;
        font-size: 14px;
        color: #667eea;
        margin-bottom: 15px;
    }

    .preview-hero {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        padding: 30px;
        border-radius: 6px;
        color: #fff;
    }

    .preview-hero h1 {
        margin-top: 0;
        font-size: 28px;
        margin-bottom: 8px;
    }

    .preview-hero p {
        margin: 0 0 12px 0;
        font-size: 14px;
        opacity: 0.9;
    }

    .preview-hero a {
        background: #F07E2D;
        color: #fff;
        padding: 10px 20px;
        text-decoration: none;
        border-radius: 4px;
        display: inline-block;
        font-size: 14px;
    }

    .position-select {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 12px;
        margin-top: 10px;
    }

    .position-option {
        padding: 12px;
        border: 2px solid #ddd;
        border-radius: 6px;
        text-align: center;
        cursor: pointer;
        transition: all 0.3s ease;
    }

    .position-option input[type="radio"] {
        display: none;
    }

    .position-option input[type="radio"]:checked + label {
        font-weight: 600;
        color: #F07E2D;
    }

    .position-option:has(input[type="radio"]:checked) {
        border-color: #F07E2D;
        background: #fff5f8;
    }

    .position-option label {
        cursor: pointer;
        margin: 0;
        display: block;
    }

    .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;
    }

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

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

        .position-select {
            grid-template-columns: 1fr;
        }
    }
</style>
</head>
<body class="admin-with-sidebar" style= " display:flex ; flex-direction:column; ">
<!-- ?php include 'header.php'; ?>  -->
<div class="admin-main-content">
<div class="admin-content-area">
    <div class="admin-header">
        <h1>Slider Settings</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; ?>

    <!-- Hero Content Form -->
    <div class="form-section">
        <h2>Hero Slider Content</h2>
        <form method="POST" action="">
            <input type="hidden" name="action" value="update_hero">
            
            <div class="grid-2">
                <div class="form-group">
                    <label for="title">Hero Title</label>
                    <input type="text" id="title" name="title" value="<?php echo htmlspecialchars($slider['hero']['title'] ?? 'Women Fashion'); ?>" placeholder="e.g., Women Fashion">
                    <div class="help-text">Main headline displayed on the slider</div>
                </div>

                <div class="form-group">
                    <label for="position">Text Position</label>
                    <div class="position-select">
                        <div class="position-option">
                            <input type="radio" id="pos-left" name="position" value="left" <?php echo (($slider['hero']['position'] ?? 'left') === 'left') ? 'checked' : ''; ?>>
                            <label for="pos-left">⬅ Left</label>
                        </div>
                        <div class="position-option">
                            <input type="radio" id="pos-right" name="position" value="right" <?php echo (($slider['hero']['position'] ?? 'left') === 'right') ? 'checked' : ''; ?>>
                            <label for="pos-right">Right ➡</label>
                        </div>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label for="subtitle">Hero Subtitle</label>
                <input type="text" id="subtitle" name="subtitle" value="<?php echo htmlspecialchars($slider['hero']['subtitle'] ?? 'Let get a look'); ?>" placeholder="e.g., Let get a look">
                <div class="help-text">Secondary text or tagline</div>
            </div>

            <div class="grid-2">
                <div class="form-group">
                    <label for="button_text">Button Text</label>
                    <input type="text" id="button_text" name="button_text" value="<?php echo htmlspecialchars($slider['hero']['button_text'] ?? 'Shop'); ?>" placeholder="e.g., Shop Collection">
                </div>

                <div class="form-group">
                    <label for="button_link">Button Link</label>
                    <input type="url" id="button_link" name="button_link" value="<?php echo htmlspecialchars($slider['hero']['button_link'] ?? 'shop.php'); ?>" placeholder="e.g., shop.php or https://...">
                </div>
            </div>

            <div style="border-top: 1px solid #e0e0e0; padding-top: 20px; margin-top: 20px;">
                <h3 style="margin-top: 0; color: #333; font-size: 14px;">Text Styling</h3>
                
                <div class="checkbox-group">
                    <input type="checkbox" id="bold_title" name="bold_title" <?php echo ($slider['hero']['bold_title'] ?? true) ? 'checked' : ''; ?>>
                    <label for="bold_title">Bold Title (Font Weight: 700)</label>
                </div>

                <div class="checkbox-group">
                    <input type="checkbox" id="multiline_enabled" name="multiline_enabled" <?php echo ($slider['hero']['multiline_enabled'] ?? false) ? 'checked' : ''; ?>>
                    <label for="multiline_enabled">Enable Multiline Text Support</label>
                </div>
                <div class="help-text">When enabled, use line breaks in title/subtitle for multiple lines</div>
            </div>

            <!-- Preview -->
            <div class="preview-section">
                <h3>Preview</h3>
                <div class="preview-hero" style="text-align: <?php echo (($slider['hero']['position'] ?? 'left') === 'left') ? 'left' : 'right'; ?>;">
                    <h1 style="font-weight: <?php echo ($slider['hero']['bold_title'] ?? true) ? '700' : '400'; ?>;">
                        <?php echo htmlspecialchars($slider['hero']['title'] ?? 'Women Fashion'); ?>
                    </h1>
                    <p><?php echo htmlspecialchars($slider['hero']['subtitle'] ?? 'Let get a look'); ?></p>
                    <a><?php echo htmlspecialchars($slider['hero']['button_text'] ?? 'Shop'); ?></a>
                </div>
            </div>

            <div class="btn-group">
                <button type="submit" class="btn btn-primary">Save Slider Settings</button>
            </div>
        </form>
    </div>    <div style="margin-top: 30px; padding: 20px; background: #e3f2fd; border-radius: 8px; border-left: 4px solid #667eea; color: #1565c0; font-size: 13px;">
        <strong>💡 Tips:</strong>
        <ul style="margin: 8px 0; padding-left: 20px;">
            <li>Position controls where the text box appears on the slider</li>
            <li>Bold title adds visual emphasis to your main headline</li>
            <li>Keep button links relative (shop.php) or absolute (https://...)</li>
            <li>Changes appear on the homepage instantly</li>
        </ul>
    </div>
</div>

