<?php
$file_path = 'weaprest.ini';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    header('Content-type: application/json');

    $array = array();
    $buffer = file_get_contents($file_path);
    foreach (explode(PHP_EOL, $buffer) as $str) {
        if (empty($str)) continue;
        $off = (substr($str, 0, 1) == ';');
        $displayname = ($off ? substr($str, 1) : $str);

        if ($displayname == $_POST['displayname']) {
            $array[] = $_POST['status'] . $displayname;

            //$log_adds = $sql->prepare("INSERT INTO pnluserlog SET pnl_user_id = ?, pnl_user_name = ?, pnl_user_serveripadress = ?, pnl_user_ipadress = ?, pnl_tarih = ?, pnl_log = ?");
            //$log_adds->execute(array($user_id, $username, $ipadress, $ipadresicik, $tarihcik, "<b>$displayname <u>($logyazdir)</u> $statu!</b>"));
        } else {
            $array[] = $str;
        }
    }

    file_put_contents($file_path, join(PHP_EOL, $array));

    exit(json_encode(array('result' => true)));
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title></title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
    <table class="table">
        <thead class="thead-light">
            <tr>
            <th scope="col">Silah İsimleri</th>
            <th scope="col">İŞLEM</th>
            <th scope="col">DURUM</th>
            </tr>
        </thead>
        <tbody>
            <?php
                $buffer = file_get_contents($file_path);
                foreach (explode(PHP_EOL, $buffer) as $str) {
                    if (empty($str)) continue;

                    $off = (substr($str, 0, 1) == ';');
                    $displayname = ($off ? substr($str, 1) : $str);

                    echo '<tr>';
                    echo '<td>' . $displayname . '</td>';
                    echo '<td><button class="btn btn-' . ($off ? 'primary' : 'info') . ' btn-sm" data-toggle="tooltip" data-placement="top" title="' . ($off ? 'Aç' : 'Kapat') . '" data-displayname="' . $displayname . '"><i class="bi bi-toggle-on"></i> <span>' . ($off ? 'Aç' : 'Kapat') . '</span></button></td>';
                    echo '<td><div class="badge badge-' . ($off ? 'danger' : 'success') . '">' . ($off ? 'YASAKLI' : 'YASAKLI DEĞİL') . '</div></td>';
                    echo '</tr>';
                }
            ?>
        </tbody>
    </table>

    <script src="https://code.jquery.com/jquery-1.11.3.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
    <script>
        $(function(){
            $(".btn").click(function(){
                let $btn = $(this);
                let $badge = $(this).parents("tr").find(".badge");
                $.ajax({
                    url: "",
                    type: "POST",
                    dataType: "JSON",
                    data: {
                        type: "silah",
                        displayname: $btn.data("displayname"),
                        status: ($btn.hasClass("btn-primary") ? "" : ";")
                    },
                    success: function(json){
                        if (json.result) {
                            if ($btn.hasClass("btn-primary")) {
                                $btn.attr("class", "btn btn-info btn-sm").attr("title", "Kapat").find("span").text("Kapat");
                                $badge.attr("class", "badge badge-success").text("YASAKLI DEĞİL");
                            } else {
                                $btn.attr("class", "btn btn-primary btn-sm").attr("title", "Aç").find("span").text("Aç");
                                $badge.attr("class", "badge badge-danger").text("YASAKLI");
                            }
                        }
                    }
                });
            });
        });
    </script>
</body>
</html>