<?php
require_once '../config/database.php';
require_once '../includes/functions.php';
$pageTitle = 'Task Settings';
include 'side.php';
require_once 'admin_protect.php';

$conn = getConnection();

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $taskType = sanitize($_POST['task_type']);
    $autoApprove = isset($_POST['auto_approve']) ? 1 : 0;
    $requiresScreenshot = isset($_POST['requires_screenshot']) ? 1 : 0;
    $minCompletionTime = intval($_POST['min_completion_time'] ?? 0);
    $maxDailyPerUser = intval($_POST['max_daily_per_user'] ?? 0);
    $isActive = isset($_POST['is_active']) ? 1 : 0;
    
    $stmt = $conn->prepare("
        INSERT INTO task_settings 
        (task_type, auto_approve, requires_screenshot, min_completion_time, max_daily_per_user, is_active)
        VALUES (?, ?, ?, ?, ?, ?)
        ON DUPLICATE KEY UPDATE
        auto_approve = VALUES(auto_approve),
        requires_screenshot = VALUES(requires_screenshot),
        min_completion_time = VALUES(min_completion_time),
        max_daily_per_user = VALUES(max_daily_per_user),
        is_active = VALUES(is_active)
    ");
    $stmt->bind_param("siiiii", $taskType, $autoApprove, $requiresScreenshot, $minCompletionTime, $maxDailyPerUser, $isActive);
    
    if ($stmt->execute()) {
        setFlash('success', '✅ Task settings updated successfully!');
    } else {
        setFlash('error', '❌ Failed to update settings.');
    }
    redirect('task-settings.php');
}

// Get all task settings
$settings = $conn->query("SELECT * FROM task_settings ORDER BY task_type ASC");

// Get pending approvals count
$pendingResult = $conn->query("SELECT COUNT(*) as count FROM task_completions WHERE status = 'pending'");
$pendingCount = $pendingResult ? $pendingResult->fetch_assoc()['count'] : 0;
?>

<style>
/* MODERN TASK SETTINGS PAGE */
.page-container {
    padding: clamp(12px, 3vw, 24px);
    max-width: 1400px;
    margin: 0 auto;
}

/* PAGE HEADER - THEME AWARE */
.page-header {
    margin-bottom: clamp(20px, 4vw, 32px);
    padding: clamp(16px, 3vw, 24px);
    background: linear-gradient(135deg, rgba(102, 126, 234, 0.05) 0%, rgba(118, 75, 162, 0.05) 100%);
    border-radius: 16px;
    border-left: 4px solid var(--primary);
}

/* Dark mode enhancement */
[data-theme="dark"] .page-header {
    background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
    border-left-color: var(--primary);
}

.header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 16px;
}

.page-title {
    font-size: clamp(20px, 4vw, 28px);
    font-weight: 900;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 12px;
    background: linear-gradient(135deg, var(--primary) 0%, #ec4899 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.btn-approvals {
    padding: 12px 24px;
    background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
    color: white;
    border: none;
    border-radius: 12px;
    font-size: clamp(12px, 2.5vw, 14px);
    font-weight: 800;
    cursor: pointer;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    box-shadow: 0 4px 12px rgba(245, 158, 11, 0.3);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.btn-approvals:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(245, 158, 11, 0.4);
}

.badge {
    background: linear-gradient(135deg, #ef4444, #dc2626);
    color: white;
    padding: 4px 10px;
    border-radius: 12px;
    font-size: clamp(10px, 2vw, 11px);
    font-weight: 800;
    box-shadow: 0 2px 8px rgba(239, 68, 68, 0.3);
}

/* INFO BOX - THEME AWARE */
.info-box {
    background: rgba(59, 130, 246, 0.1);
    border: 1px solid rgba(59, 130, 246, 0.3);
    border-radius: 14px;
    padding: clamp(14px, 3vw, 18px);
    margin-bottom: clamp(20px, 4vw, 32px);
    display: flex;
    gap: 14px;
    font-size: clamp(12px, 2.5vw, 14px);
    align-items: start;
}

.info-box i {
    font-size: clamp(18px, 4vw, 22px);
    margin-top: 2px;
    color: #3b82f6;
}

.info-box {
    color: var(--text-primary);
}

.info-box strong {
    font-weight: 800;
    color: var(--text-primary);
}

/* Dark mode specific */
[data-theme="dark"] .info-box {
    background: rgba(59, 130, 246, 0.15);
    border-color: rgba(59, 130, 246, 0.4);
}

/* SETTINGS GRID */
.settings-grid {
    display: grid;
    gap: clamp(16px, 3vw, 24px);
}

/* SETTING CARD - THEME AWARE */
.setting-card {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 18px;
    padding: clamp(20px, 4vw, 28px);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    overflow: hidden;
}

.setting-card::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 4px;
    background: linear-gradient(135deg, var(--primary), #ec4899);
    opacity: 0;
    transition: opacity 0.3s;
}

.setting-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.12);
    border-color: rgba(var(--theme-rgb), 0.2);
}

.setting-card:hover::before {
    opacity: 1;
}

/* Dark mode enhancement */
[data-theme="dark"] .setting-card {
    background: rgba(30, 41, 59, 0.6);
    border-color: rgba(148, 163, 184, 0.15);
}

[data-theme="dark"] .setting-card:hover {
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.3);
    border-color: rgba(var(--theme-rgb), 0.3);
}

/* SETTING HEADER - THEME AWARE */
.setting-header {
    display: flex;
    align-items: center;
    gap: 16px;
    margin-bottom: 24px;
    padding-bottom: 20px;
    border-bottom: 2px solid var(--border-color);
}

/* Dark mode enhancement */
[data-theme="dark"] .setting-header {
    border-bottom-color: rgba(148, 163, 184, 0.2);
}

.task-icon {
    width: clamp(52px, 10vw, 64px);
    height: clamp(52px, 10vw, 64px);
    border-radius: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: clamp(22px, 5vw, 28px);
    color: white;
    position: relative;
    overflow: hidden;
}

.task-icon::after {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(45deg, transparent, rgba(255,255,255,0.2), transparent);
    transform: translateX(-100%);
    transition: transform 0.6s;
}

.setting-card:hover .task-icon::after {
    transform: translateX(100%);
}

.task-info h3 {
    font-size: clamp(16px, 3.5vw, 20px);
    font-weight: 900;
    color: var(--text-primary);
    margin: 0 0 4px 0;
    text-transform: capitalize;
    background: linear-gradient(135deg, var(--text-primary) 0%, var(--primary) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.task-info p {
    font-size: clamp(11px, 2.5vw, 13px);
    color: var(--text-secondary);
    margin: 0;
    font-weight: 600;
}

/* SETTINGS FORM */
.settings-form {
    display: grid;
    gap: 16px;
}

/* SWITCH GROUP - THEME AWARE */
.switch-group {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    background: var(--darker-bg);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    transition: all 0.3s;
    gap: 12px;
}

.switch-group:hover {
    background: rgba(var(--theme-rgb), 0.05);
    border-color: rgba(var(--theme-rgb), 0.2);
}

.switch-label {
    font-size: clamp(12px, 2.5vw, 14px);
    font-weight: 700;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 8px;
}

.switch-label i {
    color: var(--primary);
    opacity: 0.8;
}

/* Dark mode enhancement */
[data-theme="dark"] .switch-group {
    background: rgba(30, 41, 59, 0.5);
    border-color: rgba(148, 163, 184, 0.2);
}

[data-theme="dark"] .switch-group:hover {
    background: rgba(var(--theme-rgb), 0.1);
    border-color: rgba(var(--theme-rgb), 0.3);
}

/* MODERN SWITCH */
.switch {
    position: relative;
    display: inline-block;
    width: 54px;
    height: 30px;
    flex-shrink: 0;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, #64748b, #475569);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    border-radius: 30px;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
}

.slider:before {
    position: absolute;
    content: "";
    height: 24px;
    width: 24px;
    left: 3px;
    bottom: 3px;
    background: white;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    border-radius: 50%;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

input:checked + .slider {
    background: linear-gradient(135deg, #10b981, #059669);
    box-shadow: 0 0 12px rgba(16, 185, 129, 0.3);
}

input:checked + .slider:before {
    transform: translateX(24px);
}

/* FORM ROW */
.form-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
}

/* FORM GROUP */
.form-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.form-label {
    font-size: clamp(11px, 2.5vw, 13px);
    font-weight: 700;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 6px;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

.form-label i {
    color: var(--primary);
    opacity: 0.8;
}

.form-control {
    padding: 12px 14px;
    background: var(--darker-bg);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    color: var(--text-primary);
    font-size: clamp(13px, 2.5vw, 14px);
    font-weight: 600;
    transition: all 0.3s;
}

.form-control:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(var(--theme-rgb), 0.15);
    background: var(--card-bg);
}

/* Placeholder color for both themes */
.form-control::placeholder {
    color: var(--text-secondary);
    opacity: 0.6;
}

/* Dark mode enhancement */
[data-theme="dark"] .form-control {
    border-color: rgba(148, 163, 184, 0.2);
}

[data-theme="dark"] .form-control:focus {
    border-color: var(--primary);
    background: rgba(30, 41, 59, 0.8);
}

/* SAVE BUTTON */
.btn-save {
    padding: 14px 28px;
    background: linear-gradient(135deg, var(--primary) 0%, #ec4899 100%);
    color: white;
    border: none;
    border-radius: 12px;
    font-size: clamp(13px, 2.5vw, 15px);
    font-weight: 800;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    box-shadow: 0 4px 12px rgba(var(--theme-rgb), 0.3);
    margin-top: 8px;
}

.btn-save:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(var(--theme-rgb), 0.4);
}

.btn-save:active {
    transform: translateY(0);
}

/* RESPONSIVE */
@media (max-width: 768px) {
    .header-row {
        flex-direction: column;
        align-items: stretch;
    }
    
    .btn-approvals {
        width: 100%;
        justify-content: center;
    }
    
    .form-row {
        grid-template-columns: 1fr;
    }
    
    .setting-header {
        flex-direction: column;
        text-align: center;
    }
    
    .switch-group {
        flex-direction: column;
        gap: 12px;
        text-align: center;
    }
}

/* SMOOTH TRANSITIONS FOR DARK MODE */
* {
    transition: background-color 0.3s ease, border-color 0.3s ease, color 0.3s ease;
}
</style>

<div class="page-container">
    <div class="page-header">
        <div class="header-row">
            <h1 class="page-title">
                <i class="fas fa-cog"></i> Task Settings
            </h1>
            <a href="task-approvals.php" class="btn-approvals">
                <i class="fas fa-clock"></i> 
                Pending Approvals
                <?php if ($pendingCount > 0): ?>
                    <span class="badge"><?php echo $pendingCount; ?></span>
                <?php endif; ?>
            </a>
        </div>
    </div>

    <div class="info-box">
        <i class="fas fa-info-circle"></i>
        <div>
            <strong>Configure task completion settings</strong><br>
            Auto-approve tasks instantly or require manual approval. Set minimum completion times and daily limits per user.
        </div>
    </div>

    <div class="settings-grid">
        <?php 
        $taskConfig = [
            'video' => [
                'icon' => 'fa-video', 
                'color' => 'linear-gradient(135deg, #f59e0b, #d97706)', 
                'desc' => 'Video watching tasks'
            ],
            'survey' => [
                'icon' => 'fa-poll', 
                'color' => 'linear-gradient(135deg, #06b6d4, #0891b2)', 
                'desc' => 'External survey completions'
            ],
            'quiz' => [
                'icon' => 'fa-question-circle', 
                'color' => 'linear-gradient(135deg, #8b5cf6, #7c3aed)', 
                'desc' => 'Quiz questions and answers'
            ],
            'trivia' => [
                'icon' => 'fa-question-circle', 
                'color' => 'linear-gradient(135deg, #ec4899, #db2777)', 
                'desc' => 'Trivia questions'
            ],
            'meme' => [
                'icon' => 'fa-images', 
                'color' => 'linear-gradient(135deg, #ef4444, #dc2626)', 
                'desc' => 'Meme contests and submissions'
            ],
            'chat' => [
                'icon' => 'fa-comments', 
                'color' => 'linear-gradient(135deg, #8b5cf6, #7c3aed)', 
                'desc' => 'Chat and messaging tasks'
            ],
            'transcription' => [
                'icon' => 'fa-keyboard', 
                'color' => 'linear-gradient(135deg, #10b981, #059669)', 
                'desc' => 'Audio/video transcription'
            ],
            'ads' => [
                'icon' => 'fa-ad', 
                'color' => 'linear-gradient(135deg, #3b82f6, #2563eb)', 
                'desc' => 'Advertisement viewing'
            ],
            'blog' => [
                'icon' => 'fa-blog', 
                'color' => 'linear-gradient(135deg, #f97316, #ea580c)', 
                'desc' => 'Blog post writing'
            ],
            'spin' => [
                'icon' => 'fa-spinner', 
                'color' => 'linear-gradient(135deg, #14b8a6, #0d9488)', 
                'desc' => 'Spin wheel tasks'
            ],
            'external' => [
                'icon' => 'fa-link', 
                'color' => 'linear-gradient(135deg, #6b7280, #4b5563)', 
                'desc' => 'External third-party tasks'
            ]
        ];
        
        if ($settings && $settings->num_rows > 0):
            while ($setting = $settings->fetch_assoc()): 
                $config = $taskConfig[$setting['task_type']] ?? [
                    'icon' => 'fa-tasks', 
                    'color' => 'linear-gradient(135deg, #6b7280, #4b5563)', 
                    'desc' => 'Task type'
                ];
        ?>
        <div class="setting-card">
            <form method="POST" class="settings-form">
                <input type="hidden" name="task_type" value="<?php echo htmlspecialchars($setting['task_type']); ?>">
                
                <div class="setting-header">
                    <div class="task-icon" style="background: <?php echo $config['color']; ?>;">
                        <i class="fas <?php echo $config['icon']; ?>"></i>
                    </div>
                    <div class="task-info">
                        <h3><?php echo htmlspecialchars($setting['task_type']); ?> Tasks</h3>
                        <p><?php echo $config['desc']; ?></p>
                    </div>
                </div>
                
                <div class="switch-group">
                    <span class="switch-label">
                        <i class="fas fa-check-circle"></i> Auto-Approve Completions
                    </span>
                    <label class="switch">
                        <input type="checkbox" name="auto_approve" <?php echo $setting['auto_approve'] ? 'checked' : ''; ?>>
                        <span class="slider"></span>
                    </label>
                </div>
                
                <div class="switch-group">
                    <span class="switch-label">
                        <i class="fas fa-camera"></i> Require Screenshot Proof
                    </span>
                    <label class="switch">
                        <input type="checkbox" name="requires_screenshot" <?php echo $setting['requires_screenshot'] ? 'checked' : ''; ?>>
                        <span class="slider"></span>
                    </label>
                </div>
                
                <div class="switch-group">
                    <span class="switch-label">
                        <i class="fas fa-toggle-on"></i> Task Type Active
                    </span>
                    <label class="switch">
                        <input type="checkbox" name="is_active" <?php echo $setting['is_active'] ? 'checked' : ''; ?>>
                        <span class="slider"></span>
                    </label>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="form-label">
                            <i class="fas fa-clock"></i> Min Completion Time (seconds)
                        </label>
                        <input type="number" name="min_completion_time" class="form-control" 
                               value="<?php echo $setting['min_completion_time']; ?>" min="0">
                    </div>
                    
                    <div class="form-group">
                        <label class="form-label">
                            <i class="fas fa-list-ol"></i> Max Daily Per User
                        </label>
                        <input type="number" name="max_daily_per_user" class="form-control" 
                               value="<?php echo $setting['max_daily_per_user'] ?? 0; ?>" 
                               min="0" placeholder="0 = unlimited">
                    </div>
                </div>
                
                <button type="submit" class="btn-save">
                    <i class="fas fa-save"></i> Save Settings
                </button>
            </form>
        </div>
        <?php 
            endwhile;
        else:
        ?>
        <div class="info-box" style="background: linear-gradient(135deg, rgba(239, 68, 68, 0.08), rgba(239, 68, 68, 0.04)); border-color: rgba(239, 68, 68, 0.25); color: #ef4444;">
            <i class="fas fa-exclamation-circle"></i>
            <div>
                <strong>No task settings found</strong><br>
                Please create task settings in the database first.
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>

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