<?php
session_start();
require_once '../../config.php';
load('middleware', 'user');
if ($_SESSION['user']['level'] !== "Admin") {
    header('location:' . base_url('id'));
}
function slug($text, string $divider = '-') {
    
    $text = preg_replace('/[0-9]+/', '', $text);
    $text = preg_replace('~[^\pL\d]+~u', $divider, $text);
    $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
    $text = preg_replace('~[^-\w]+~', '', $text);
    $text = trim($text, $divider);
    $text = preg_replace('~-+~', $divider, $text);
    $text = strtolower($text);
    
    if (empty($text)) {
        return 'n-a';
    }
    return $text;
}if (isset($_GET['hapus'])) {
    $id = addslashes(trim(htmlspecialchars($_GET['hapus'])));
    $fet = $db->query("SELECT * FROM form_input WHERE id='$id'")->fetch_assoc();
    mysqli_query($db, "DELETE FROM form_input WHERE id = '$id'");
    unlink('../../id/target/'.$fet['slug'].'.php');
    $_SESSION['alert'] = ['success', 'Success!', 'Form Input Berhasil di hapus.'];
    exit(header('location: '.base_url('admin/setting/input')));
}

if (isset($_POST['tombol'])) {
    
    if (isset($_POST['label']) AND isset($_POST['total_input']) AND isset($_POST['name']) AND isset($_POST['input1'])) {
        $name = $_POST['name'];
        $label = $_POST['label'];
        $tipe = $_POST['total_input'];
        $form_input1 = $_POST['input1'];
        $form_input2 = $_POST['input2'];
        
        if (empty($name) OR empty($label) OR empty($tipe) OR empty($form_input1)) {
            $_SESSION['alert'] = ['danger', 'Failed!', 'Masih ada data yang kosong.'];
            exit(header('location: '.base_url('admin/setting/input')));
            
        } else {
                    $slug = slug($name);
                    
                    if ($slug == 'defaults') {
                        $_SESSION['alert'] = ['danger', 'Failed!', 'Gagal menambahkan form input.'];
                        exit(header('location: '.base_url('admin/setting/input')));
                    } else {
                        mysqli_query($db, "INSERT INTO form_input VALUES ('','$name','$label','$slug','$tipe','$form_input1','$form_input2')");
                        if($tipe == "1"){
$write = '<div class="row">
            <div class="col">
                <div class="card bg-dark shadow">
                    <div class="card-header">
                        <h5 class="card-title"><i class="fa-regular fa-circle-1"></i> Lengkapi Data</h5>
                    </div>
                    <div class="card-body">
                        <div id="userData">
                            <div class="row row-cols row-cols-md">
                                <div class="col-12">
                                    <div class="form-group mt-3">
                                        <label for="'.$form_input1.'">'.$form_input1.'</label>
                                        <input class="form-control" placeholder=" '.$form_input1.'" type="text" name="target" id="target" required="">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <br>
                        <?php if($fetch_games["petunjuk"] == null):?>
                        <?php else:?>
                            <button class="btn btn-primary btn-sm" type="button" data-bs-toggle="modal" data-bs-target="#petunjukModal">Petunjuk</button>
                        <?php endif;?>
                    </div>
                </div>
            </div>
        </div>';
$open = fopen('../../id/target/'.$slug.'.php', 'w');
fwrite($open, $write);
                        }else if($tipe == 2){
$write = '<div class="row">
            <div class="col">
                <div class="card bg-dark shadow">
                    <div class="card-header">
                        <h5 class="card-title"><i class="fa-regular fa-circle-1"></i> Lengkapi Data</h5>
                    </div>
                    <div class="card-body">
                        <div id="userData">
                            <div class="row row-cols row-cols-md">
                                <div class="col-6">
                                    <div class="form-group mt-2">
                                        <label for="'.$form_input1.'">'.$form_input1.'</label>
                                        <input class="form-control" placeholder=" '.$form_input1.'" type="text" name="target" id="target" required="">
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="form-group mt-2">
                                        <label for="'.$form_input2.'">'.$form_input2.'</label>
                                        <input class="form-control" placeholder="'.$form_input2.'" type="text" name="server" id="server"  required="">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <br>
                        <?php if($fetch_games["petunjuk"] == null):?>
                        <?php else:?>
                            <button class="btn btn-primary btn-sm" type="button" data-bs-toggle="modal" data-bs-target="#petunjukModal">Petunjuk</button>
                        <?php endif;?>
                    </div>
                </div>
            </div>
        </div>';
$open = fopen('../../id/target/'.$slug.'.php', 'w');
fwrite($open, $write);
                        }
                        $_SESSION['alert'] = ['success', 'Success!', 'Form Input berhasil di tambahkan.'];
                        exit(header('location: '.base_url('admin/setting/input')));
                    }
                }
    } else {
        $_SESSION['alert'] = ['danger', 'Failed!', 'Masih ada data yang kosong.'];
        exit(header('location: '.base_url('admin/setting/input')));
    }
}
require '../../layouts/header-admin.php';
?>
    <div class="row justify-content-center">
    <div class="col-md-6 col-12">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">Tambah Input</h4>
            </div>
            <div class="card-body">
                <form action="" method="POST" enctype="multipart/form-data">
                    <div class="form-group mb-3">
                        <label class="mb-1">Name</label>
                            <input type="text" class="form-control" name="name" placeholder="wcode.id">
                        </div>
                    <div class="form-group mb-3">
                        <label class="mb-1">Label Form Input</label>
                            <input type="text" class="form-control" name="label" id="label" placeholder="wcode.id">
                    </div>
                    <div class="form-group mb-3">
                        <label class="mb-1">Jumlah Input</label>
                            <select class="form-control" name="total_input" id="total_input" onchange="input_total(this.value);">
                                <option value="1"> 1 Input</option>
                                <option value="2"> 2 Input</option>
                            </select>
                    </div>
                    <div class="form-group mb-3">
                        <label class="mb-1">Form Input 1</label>
                            <input type="text" class="form-control" name="input1" id="input1" placeholder="User ID">
                        </div>
                    <div class="form-group mb-3 d-none" id="form_input2">
                        <label class="mb-1">Form Input 2</label>
                            <input type="text" class="form-control" name="input2" id="input2" placeholder="Server ID">
                        </div>
                        <div class="text-right">
                        <button class="btn btn-light" type="reset">Batal</button>
                        <button class="btn btn-primary" type="submit" name="tombol">Tambah</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="col-md-6 col-12">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">
                    <i class="mdi mdi-numeric-1-box-multiple-outline text-primary"></i> <span id="label_get">Masukkan Data User</span>
                </h4>
            </div>
            <div class="card-body">
                <div id="userData">
                    <div class="row row-cols row-cols-md">
                        <div class="col-6">
                            <div class="form-group mt-3">
                                <label for="target" id="form1">User ID</label>
                                <input class="form-control" placeholder=" User ID" type="number" name="target" id="target" required="">
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="form-group mt-3 d-none" id="input_form2">
                                <label for="server" id="form2">Server ID</label>
                                <input class="form-control" placeholder="Server ID" type="number" name="server" id="server"  required="">
                            </div>
                         </div>
                    </div>
                </div>
                <br>
                <button class="btn btn-primary btn-sm" type="button" data-bs-toggle="modal" data-bs-target="#petunjukModal">Petunjuk</button>
            </div>
        </div>
    <div class="card bg-dark shadow mt-4">
        <div class="card-header">
                <h4 class="card-title">DATA INPUT</h4>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table" id="tbb">
                        <thead>
                        <tr>
                            <th>Name</th>
                            <th>Total Form Input</th>
                            <th width="10">Action</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php 
                        $no = 1;
                        $query_input = mysqli_query($db, "SELECT * FROM form_input ORDER BY id DESC");
                        while($fetch_input = mysqli_fetch_assoc($query_input)): 
                        ?>
                        <tr>
                            <td><?= $fetch_input['name'];?></td>
                            <?php if($fetch_input['tipe'] == "1"):?>
                            <td>1 Input</td>
                            <?php else:?>
                            <td>2 Input</td>
                            <?php endif;?>
                            <td style="white-space: nowrap;">
                                <a class="btn btn-danger btn-sm" href="/admin/setting/input?hapus=<?= $fetch_input['id']; ?>">Hapus</a>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
<script>
    function input_total(total) {
        if (total == '1') {
            $("#form_input2").addClass('d-none');
            $("#input_form2").addClass('d-none');
        } else {
            $("#form_input2").removeClass('d-none');
            $("#input_form2").removeClass('d-none');
        }
    }
    $(document).ready(function(){
       $("#tbb").dataTable(); 
    });
    $(document).ready(function(){
    $("#input1").on("input", function(){
        $("#form1").text($(this).val());
        $("#target")[0].placeholder=$(this).val();
    });
    $("#label").on("input", function(){
        $("#label_get").text($(this).val());
    });
    $("#input2").on("input", function(){
        $("#form2").text($(this).val());
        $("#server")[0].placeholder= $(this).val();
    });
});
</script>
<?php require '../../layouts/footer-admin.php';?>