<?php
$db_file = 'database.json';
if (!file_exists($db_file)) file_put_contents($db_file, json_encode(['num_rules' => [], 'streak_rules' => []]));
$data = json_decode(file_get_contents($db_file), true);

// ১. নম্বর সিকোয়েন্স সেভ করা
if (isset($_POST['save_num_rule'])) {
    $seq = implode(',', array_filter($_POST['rule_seq'], 'strlen'));
    if (!empty($seq)) {
        $data['num_rules'][$seq] = [
            'num' => $_POST['res_num'],
            'color' => $_POST['res_color'],
            'type' => $_POST['res_type'] // এখন ইউজার নিজেই সিলেক্ট করতে পারবে
        ];
        file_put_contents($db_file, json_encode($data));
    }
}

// ২. স্ট্রিক (Big/Small count) লজিক সেভ করা
if (isset($_POST['save_streak_rule'])) {
    $key = $_POST['streak_count'] . '_' . $_POST['streak_type'];
    $data['streak_rules'][$key] = [
        'res_type' => $_POST['res_streak_type'],
        'res_color' => $_POST['res_streak_color']
    ];
    file_put_contents($db_file, json_encode($data));
}

// ডিলিট লজিক
if (isset($_POST['delete_rule'])) {
    unset($data[$_POST['type']][$_POST['id']]);
    file_put_contents($db_file, json_encode($data));
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Neon Admin Control</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --neon-green: #00ff88;
            --dark-bg: #0b0f19;
            --card-bg: #161b2a;
        }

        body { 
            background: var(--dark-bg); 
            color: #e2e8f0; 
            font-family: 'Segoe UI', sans-serif; 
        }

        /* Neon Glow Text Animation */
        .neon-text {
            color: var(--neon-green);
            text-shadow: 0 0 5px var(--neon-green), 0 0 10px var(--neon-green);
            font-family: 'Orbitron', sans-serif;
            animation: shimmer 1.5s infinite alternate;
        }

        @keyframes shimmer {
            from { opacity: 0.7; text-shadow: 0 0 5px var(--neon-green); }
            to { opacity: 1; text-shadow: 0 0 15px var(--neon-green), 0 0 25px var(--neon-green); }
        }

        .card { 
            background: var(--card-bg); 
            border: 1px solid #334155; 
            border-radius: 1.2rem; 
            margin-bottom: 25px; 
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.5);
        }

        .form-control, .form-select { 
            background: #0f172a; 
            color: white; 
            border: 1px solid #475569; 
            border-radius: 10px;
        }

        .form-control:focus, .form-select:focus {
            background: #0f172a;
            color: white;
            border-color: var(--neon-green);
            box-shadow: 0 0 10px var(--neon-green);
        }

        /* Glowing Button */
        .btn-neon {
            background: transparent;
            color: var(--neon-green);
            border: 2px solid var(--neon-green);
            font-weight: bold;
            text-transform: uppercase;
            letter-spacing: 1px;
            transition: 0.3s;
            box-shadow: 0 0 10px var(--neon-green);
            border-radius: 12px;
        }

        .btn-neon:hover {
            background: var(--neon-green);
            color: #000;
            box-shadow: 0 0 25px var(--neon-green);
        }

        label {
            font-size: 0.85rem;
            font-weight: 600;
            margin-bottom: 5px;
            color: #94a3b8;
        }

        .active-rule-item {
            background: rgba(255, 255, 255, 0.05);
            border-left: 4px solid var(--neon-green);
            padding: 12px;
            margin-bottom: 12px;
            border-radius: 8px;
            border: 1px solid rgba(255, 255, 255, 0.1);
            transition: 0.3s;
        }
        .active-rule-item:hover {
            background: rgba(255, 255, 255, 0.08);
            box-shadow: 0 0 10px rgba(0, 255, 136, 0.2);
        }
    </style>
</head>
<body class="p-4">
    <div class="container" style="max-width: 750px;">
        <h2 class="mb-4 text-center neon-text">SYSTEM LOGIC ARCHITECT</h2>
        
        <!-- নম্বর সিকোয়েন্স সেটার -->
        <div class="card p-4">
            <h5 class="neon-text mb-3" style="font-size: 1.1rem;">🔢 Sequence Pattern Logic</h5>
            <form method="POST">
                <label>Sequence Numbers (History Pattern)</label>
                <div id="num_box" class="d-flex flex-wrap gap-2 mb-3">
                    <input type="number" name="rule_seq[]" class="form-control w-25" placeholder="Col 1" required>
                    <input type="number" name="rule_seq[]" class="form-control w-25" placeholder="Col 2">
                </div>
                <button type="button" class="btn btn-sm btn-outline-info mb-4" onclick="addCol()">+ Add Condition Column</button>
                
                <div class="row g-3 border-top pt-3 border-secondary">
                    <div class="col-md-4">
                        <label>Next Result Number</label>
                        <input type="number" id="res_num" name="res_num" class="form-control" required min="0" max="9" oninput="generateLogic()">
                    </div>
                    <div class="col-md-4">
                        <label>Next Result Type</label>
                        <select id="res_type" name="res_type" class="form-select">
                            <option value="Big">Big</option>
                            <option value="Small">Small</option>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <label>Next Result Color</label>
                        <select id="res_color" name="res_color" class="form-select">
                            <option value="Red">Red</option>
                            <option value="Green">Green</option>
                            <option value="Violet-Red">Violet-Red</option>
                            <option value="Violet-Green">Violet-Green</option>
                        </select>
                    </div>
                </div>
                <button type="submit" name="save_num_rule" class="btn btn-neon w-100 mt-4 py-2">Save Sequence Logic</button>
            </form>
        </div>

        <!-- স্ট্রিক/কাউন্ট সেটার -->
        <div class="card p-4">
            <h5 class="neon-text mb-3" style="font-size: 1.1rem;">📊 Streak Analysis Logic</h5>
            <form method="POST">
                <div class="row g-3 mb-3">
                    <div class="col-6">
                        <label>Streak Count (Repeat)</label>
                        <input type="number" name="streak_count" class="form-control" placeholder="e.g. 5" required>
                    </div>
                    <div class="col-6">
                        <label>Current Trend Type</label>
                        <select name="streak_type" class="form-select">
                            <option value="Big">Big</option>
                            <option value="Small">Small</option>
                        </select>
                    </div>
                </div>
                <div class="row g-3 border-top pt-3 border-secondary">
                    <div class="col-6">
                        <label>Future Result Type</label>
                        <select name="res_streak_type" class="form-select">
                            <option value="Big">Big</option>
                            <option value="Small">Small</option>
                        </select>
                    </div>
                    <div class="col-6">
                        <label>Future Result Color</label>
                        <select name="res_streak_color" class="form-select">
                            <option value="Red">Red</option>
                            <option value="Green">Green</option>
                        </select>
                    </div>
                </div>
                <button type="submit" name="save_streak_rule" class="btn btn-neon w-100 mt-4 py-2" style="border-color: #38bdf8; color: #38bdf8; box-shadow: 0 0 10px #38bdf8;">Save Streak Logic</button>
            </form>
        </div>

        <!-- অ্যাক্টিভ রুলস লিস্ট -->
        <div class="card p-3">
            <h6 class="neon-text border-bottom border-secondary pb-2 mb-3" style="font-size: 0.9rem; text-transform: uppercase;">Central Intelligence Registry</h6>
            <div style="max-height: 250px; overflow-y: auto;">
                <?php foreach($data['num_rules'] as $key => $v): ?>
                    <div class="active-rule-item d-flex justify-content-between align-items-center">
                        <span class="small" style="color: #cbd5e1;">
                            <strong style="color: var(--neon-green); letter-spacing: 1px;">PATTERN:</strong> 
                            <span style="color: #ffffff; font-weight: 600;">[<?php echo $key; ?>]</span> 
                            <span style="color: #94a3b8; margin: 0 5px;">➔</span> 
                            <strong style="color: #fbbf24; letter-spacing: 1px;">GOTO:</strong> 
                            <span style="color: #ffffff; font-weight: bold; background: rgba(251, 191, 36, 0.1); padding: 2px 8px; border-radius: 4px;">
                                <?php echo "{$v['num']} ({$v['type']} / {$v['color']})"; ?>
                            </span>
                        </span>
                        <form method="POST">
                            <input type="hidden" name="type" value="num_rules">
                            <input type="hidden" name="id" value="<?php echo $key; ?>">
                            <button name="delete_rule" class="btn btn-sm btn-link text-danger text-decoration-none fw-bold">DELETE</button>
                        </form>
                    </div>
                <?php endforeach; ?>
                
                <?php foreach($data['streak_rules'] as $key => $v): ?>
                    <div class="active-rule-item d-flex justify-content-between align-items-center" style="border-left-color: #38bdf8;">
                        <span class="small" style="color: #cbd5e1;">
                            <strong style="color: #38bdf8; letter-spacing: 1px;">STREAK:</strong> 
                            <span style="color: #ffffff; font-weight: 600;">[<?php echo str_replace('_',' ',$key); ?>]</span> 
                            <span style="color: #94a3b8; margin: 0 5px;">➔</span> 
                            <strong style="color: #fbbf24; letter-spacing: 1px;">GOTO:</strong> 
                            <span style="color: #ffffff; font-weight: bold; background: rgba(56, 189, 248, 0.1); padding: 2px 8px; border-radius: 4px;">
                                <?php echo "{$v['res_type']} / {$v['res_color']}"; ?>
                            </span>
                        </span>
                        <form method="POST">
                            <input type="hidden" name="type" value="streak_rules">
                            <input type="hidden" name="id" value="<?php echo $key; ?>">
                            <button name="delete_rule" class="btn btn-sm btn-link text-danger text-decoration-none fw-bold">DELETE</button>
                        </form>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>

    <script>
        function addCol(){
            let box = document.getElementById('num_box');
            let input = document.createElement('input');
            input.type = "number"; 
            input.name = "rule_seq[]"; 
            input.className = "form-control w-25"; 
            input.placeholder = "Next Col";
            box.appendChild(input);
        }

        function generateLogic() {
            const val = document.getElementById('res_num').value;
            const typeField = document.getElementById('res_type');
            const colorField = document.getElementById('res_color');

            if (val === "") return;

            const map = {
                0: { t: 'Small', c: 'Violet-Red' },
                1: { t: 'Small', c: 'Green' },
                2: { t: 'Small', c: 'Red' },
                3: { t: 'Small', c: 'Green' },
                4: { t: 'Small', c: 'Red' },
                5: { t: 'Big',   c: 'Violet-Green' },
                6: { t: 'Big',   c: 'Red' },
                7: { t: 'Big',   c: 'Green' },
                8: { t: 'Big',   c: 'Red' },
                9: { t: 'Big',   c: 'Green' }
            };

            if (map[val]) {
                typeField.value = map[val].t;
                colorField.value = map[val].c;
            }
        }
    </script>
</body>
</html>