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

if (isset($_POST['add_pattern'])) {
    $input = $_POST['in1']."-".$_POST['in2']."-".$_POST['in3'];
    $output = [$_POST['out1'], $_POST['out2'], $_POST['out3']];
    $data[$input] = $output;
    file_put_contents($file, json_encode($data));
    $msg = "প্যাটার্ন সফলভাবে সেভ হয়েছে!";
}

if (isset($_GET['del'])) {
    unset($data[$_GET['del']]);
    file_put_contents($file, json_encode($data));
    header("Location: admin.php");
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Control Panel</title>
    <style>
        body { background: #080808; color: #fff; font-family: sans-serif; padding: 20px; }
        .box { background: #151515; padding: 25px; border-radius: 20px; max-width: 600px; margin: auto; border: 1px solid #333; }
        h2 { color: #00ff96; text-align: center; }
        .row { display: flex; gap: 10px; margin-bottom: 15px; justify-content: center; }
        input { width: 60px; height: 50px; background: #222; border: 1px solid #444; color: #00ff96; text-align: center; font-size: 20px; border-radius: 10px; }
        button { width: 100%; padding: 15px; background: #00ff96; border: none; font-weight: bold; cursor: pointer; border-radius: 10px; margin-top: 10px; }
        table { width: 100%; margin-top: 30px; border-collapse: collapse; }
        th, td { padding: 10px; border: 1px solid #333; text-align: center; }
        .del-btn { color: #ff4d4d; text-decoration: none; font-size: 12px; }
    </style>
</head>
<body>
    <div class="box">
        <h2>AI ADMIN PANEL</h2>
        <?php if(isset($msg)) echo "<p style='color:cyan; text-align:center;'>$msg</p>"; ?>
        <form method="POST">
            <p style="text-align:center; color:#888;">যদি ইউজার এই ইনপুট দেয়:</p>
            <div class="row">
                <input type="number" name="in1" placeholder="1st" required>
                <input type="number" name="in2" placeholder="2nd" required>
                <input type="number" name="in3" placeholder="3rd" required>
            </div>
            <p style="text-align:center; color:#888;">তবে এই রেজাল্ট দেখাবে:</p>
            <div class="row">
                <input type="number" name="out1" placeholder="1st" required style="border-color:cyan;">
                <input type="number" name="out2" placeholder="2nd" required style="border-color:cyan;">
                <input type="number" name="out3" placeholder="3rd" required style="border-color:cyan;">
            </div>
            <button type="submit" name="add_pattern">সেভ প্যাটার্ন (Save)</button>
        </form>

        <table>
            <tr><th>ইনপুট</th><th>আউটপুট</th><th>অ্যাকশন</th></tr>
            <?php foreach($data as $key => $val): ?>
            <tr>
                <td><?php echo str_replace("-", ",", $key); ?></td>
                <td><?php echo implode(",", $val); ?></td>
                <td><a href="?del=<?php echo $key; ?>" class="del-btn">মুছুন</a></td>
            </tr>
            <?php endforeach; ?>
        </table>
    </div>
</body>
</html>