<?php
require_once('config.php');
require_once('inc/header.php');

$conn = new mysqli('localhost', 'mahamudf_skytimesphp', 'mahamudf_skytimesphp', 'mahamudf_skytimesphp');

// কালার টেবিল তৈরি
$conn->query("CREATE TABLE IF NOT EXISTS `mahamudfutureit_tts_colors` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `color_key` varchar(50) NOT NULL,
    `color_value` varchar(20) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `color_key` (`color_key`)
)");

// 5টি প্রিসেট কালার থিম
$presets = [
    'default' => ['name' => '🎨 ডিফল্ট', 'primary' => '#6366f1', 'secondary' => '#a855f7', 'wave' => '#6366f1', 'progress' => '#6366f1', 'play_bg' => '#ffffff', 'play_icon' => '#1e293b', 'text' => '#f1f5f9', 'bg_start' => '#1e293b', 'bg_end' => '#0f172a'],
    'red' => ['name' => '❤️ ক্রিমসন', 'primary' => '#dc2626', 'secondary' => '#b91c1c', 'wave' => '#ef4444', 'progress' => '#dc2626', 'play_bg' => '#ffffff', 'play_icon' => '#7f1d1d', 'text' => '#fef2f2', 'bg_start' => '#2d1a1a', 'bg_end' => '#1a0f0f'],
    'green' => ['name' => '💚 এমারেল্ড', 'primary' => '#10b981', 'secondary' => '#059669', 'wave' => '#34d399', 'progress' => '#10b981', 'play_bg' => '#ffffff', 'play_icon' => '#064e3b', 'text' => '#ecfdf5', 'bg_start' => '#1a2e25', 'bg_end' => '#0f1f18'],
    'blue' => ['name' => '💙 রয়্যাল', 'primary' => '#3b82f6', 'secondary' => '#1d4ed8', 'wave' => '#60a5fa', 'progress' => '#3b82f6', 'play_bg' => '#ffffff', 'play_icon' => '#1e3a8a', 'text' => '#eff6ff', 'bg_start' => '#1a2a4a', 'bg_end' => '#0f1a30'],
    'amber' => ['name' => '🟡 অ্যাম্বার', 'primary' => '#f59e0b', 'secondary' => '#d97706', 'wave' => '#fbbf24', 'progress' => '#f59e0b', 'play_bg' => '#ffffff', 'play_icon' => '#78350f', 'text' => '#fffbeb', 'bg_start' => '#2d2418', 'bg_end' => '#1a150f']
];

if(isset($_POST['save_colors'])) {
    $selected_preset = $_POST['preset_select'];
    if(isset($presets[$selected_preset])) {
        $p = $presets[$selected_preset];
        
        // প্রতিটি কালার আলাদা করে সেভ করুন
        $conn->query("INSERT INTO mahamudfutureit_tts_colors (color_key, color_value) VALUES ('primary_color', '{$p['primary']}') ON DUPLICATE KEY UPDATE color_value = '{$p['primary']}'");
        $conn->query("INSERT INTO mahamudfutureit_tts_colors (color_key, color_value) VALUES ('secondary_color', '{$p['secondary']}') ON DUPLICATE KEY UPDATE color_value = '{$p['secondary']}'");
        $conn->query("INSERT INTO mahamudfutureit_tts_colors (color_key, color_value) VALUES ('wave_color', '{$p['wave']}') ON DUPLICATE KEY UPDATE color_value = '{$p['wave']}'");
        $conn->query("INSERT INTO mahamudfutureit_tts_colors (color_key, color_value) VALUES ('progress_color', '{$p['progress']}') ON DUPLICATE KEY UPDATE color_value = '{$p['progress']}'");
        $conn->query("INSERT INTO mahamudfutureit_tts_colors (color_key, color_value) VALUES ('play_bg', '{$p['play_bg']}') ON DUPLICATE KEY UPDATE color_value = '{$p['play_bg']}'");
        $conn->query("INSERT INTO mahamudfutureit_tts_colors (color_key, color_value) VALUES ('play_icon', '{$p['play_icon']}') ON DUPLICATE KEY UPDATE color_value = '{$p['play_icon']}'");
        $conn->query("INSERT INTO mahamudfutureit_tts_colors (color_key, color_value) VALUES ('text_color', '{$p['text']}') ON DUPLICATE KEY UPDATE color_value = '{$p['text']}'");
        $conn->query("INSERT INTO mahamudfutureit_tts_colors (color_key, color_value) VALUES ('bg_start', '{$p['bg_start']}') ON DUPLICATE KEY UPDATE color_value = '{$p['bg_start']}'");
        $conn->query("INSERT INTO mahamudfutureit_tts_colors (color_key, color_value) VALUES ('bg_end', '{$p['bg_end']}') ON DUPLICATE KEY UPDATE color_value = '{$p['bg_end']}'");
        
        echo '<div class="alert alert-success">✅ প্লেয়ার কালার সেভ করা হয়েছে!</div>';
    }
}

// বর্তমান নির্বাচিত প্রিসেট খুঁজে বের করুন
$current_colors = [];
$result = $conn->query("SELECT color_key, color_value FROM mahamudfutureit_tts_colors");
while($row = $result->fetch_assoc()) {
    $current_colors[$row['color_key']] = $row['color_value'];
}

$current_preset = 'default';
foreach($presets as $key => $preset) {
    if($current_colors['primary_color'] == $preset['primary'] && 
       $current_colors['secondary_color'] == $preset['secondary']) {
        $current_preset = $key;
        break;
    }
}
?>

<div class="pagetitle">
    <h1><i class="bi bi-palette me-2"></i> প্লেয়ার কালার সেটিংস</h1>
    <nav>
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
            <li class="breadcrumb-item"><a href="audio-news-settings.php">Audio News</a></li>
            <li class="breadcrumb-item active">Player Color</li>
        </ol>
    </nav>
</div>

<section class="section">
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0"><i class="bi bi-palette me-2"></i> কালার থিম সিলেক্ট করুন</h5>
                </div>
                <div class="card-body">
                    <form method="POST" id="colorForm">
                        <div class="row">
                            <?php foreach($presets as $key => $preset): ?>
                            <div class="col-md-2 col-sm-4 col-6 mb-3">
                                <div class="border rounded p-2 text-center preset-card" data-preset="<?php echo $key; ?>" style="cursor:pointer; <?php echo $current_preset == $key ? 'border:2px solid #667eea; background:#f0f0ff;' : ''; ?>">
                                    <div class="d-flex justify-content-center gap-1 mb-2">
                                        <div style="width:25px; height:25px; background:<?php echo $preset['primary']; ?>; border-radius:5px;"></div>
                                        <div style="width:25px; height:25px; background:<?php echo $preset['secondary']; ?>; border-radius:5px;"></div>
                                        <div style="width:25px; height:25px; background:<?php echo $preset['play_bg']; ?>; border-radius:5px; border:1px solid #ddd;"></div>
                                    </div>
                                    <div class="fw-bold small"><?php echo $preset['name']; ?></div>
                                </div>
                            </div>
                            <?php endforeach; ?>
                        </div>
                        
                        <input type="hidden" name="preset_select" id="preset_select" value="<?php echo $current_preset; ?>">
                        
                        <div class="mt-3">
                            <button type="submit" name="save_colors" class="btn btn-primary">
                                <i class="bi bi-save me-1"></i> কালার সংরক্ষণ
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</section>

<script>
document.querySelectorAll('.preset-card').forEach(card => {
    card.addEventListener('click', function() {
        document.querySelectorAll('.preset-card').forEach(c => {
            c.style.border = '1px solid #dee2e6';
            c.style.background = '';
        });
        this.style.border = '2px solid #667eea';
        this.style.background = '#f0f0ff';
        document.getElementById('preset_select').value = this.dataset.preset;
    });
});
</script>

<?php require_once('inc/footer.php'); ?>