<?php
session_start();
require_once '../../config.php';
load('middleware', 'user');

if ($_SESSION['user']['level'] !== "Admin") {
    header('location:' . base_url('/id'));
}
function validate_img($files) {
    if ($files['error'] == 0) {
        $format = strtolower(end(explode('.', $files['name'])));
        
        if (in_array($format, ['jpg', 'jpeg', 'png'])) {
            if ($files['size'] > 5000000) {
                return false;
            } else {
                $nama_baru = uniqid() . '.' . $format;
                
                move_uploaded_file($files['tmp_name'], '../../assets/images/' . $nama_baru);
                return $nama_baru;
            }
        } else {
            return false;
        }
    } else {
        return false;
    }
}
if(isset($_POST['tombol_sosial'])){
    opt_update('d2EtYWRtaW4=', $_POST['wa-admin']);
    opt_update('aWctYWRtaW4=', $_POST['ig-admin']);
    opt_update('YWxhbWF0LWFkbWlu', $_POST['alamat-admin']);
    opt_update('ZW1haWwtYWRtaW4=', $_POST['email-admin']);
    $_SESSION['alert'] = ['success', 'Success!', 'Data berhasil di simpan.'];
    exit(header("Location: ".base_url('/admin/setting/')));
}
if(isset($_POST['tombol_wa'])){
    opt_update('dHAtcGhvbmU=', $_POST['nomor']);
    opt_update('dHAta2V5', $_POST['key']);
    $_SESSION['alert'] = ['success', 'Success!', 'Data berhasil di simpan.'];
    exit(header("Location: ".base_url('/admin/setting/')));
}
if(isset($_POST['tombol_ck'])){
    opt_update('Y2VraWQta2V5', $_POST['ck-api']);
    $_SESSION['alert'] = ['success', 'Success!', 'Data berhasil di simpan.'];
    exit(header("Location: ".base_url('/admin/setting/')));
}
if(isset($_POST['tombol_lainnya'])){
    opt_update('aGFyZ2FfcmVzZWxsZXI=', $_POST['reseller']);
    opt_update('aGFyZ2FfcHJlbWl1bQ==', $_POST['vip']);
    opt_update('YmFuay1udW1iZXI=', $_POST['bank-number']);
    opt_update('YmFuay1jb2Rl', $_POST['bank-code']);
    $_SESSION['alert'] = ['success', 'Success!', 'Data berhasil di simpan.'];
    exit(header("Location: ".base_url('/admin/setting/')));
}
if (isset($_POST['tombol_logo'])) {
    
    opt_update('Z3Vlc3Rfc3RhdHVz', $_POST['guest_status']);
    opt_update('cG9wdXBfZ3Vlc3Rfc3RhdHVz', $_POST['popup_guest_status']);
    opt_update('cG9wdXAtbmV3cw==', $_POST['keterangan']);
    
    $popup_guest_link = validate_img($_FILES['popup_guest_link']);
    if ($popup_guest_link !== false) {
        opt_update('cG9wdXBfZ3Vlc3RfbGluaw==', '/assets/images/' . $popup_guest_link);
    }
    
    $logo_guest = validate_img($_FILES['logo_guest']);
    if ($logo_guest !== false) {
        opt_update('aWNvbi13ZWI=', '/assets/images/' . $logo_guest);
    }
    
    $_SESSION['alert'] = ['success', 'Success!', 'Data berhasil di simpan.'];
    exit(header("Location: ".base_url('/admin/setting/')));
}

if (isset($_POST['tombol'])) {
    
    opt_update('dGl0bGU=', $_POST['title']);
    opt_update('dGl0bGVfd2Vi', $_POST['title_web']);
    opt_update('YXV0aG9y', $_POST['author']);
    opt_update('ZGVzY3JpcHRpb24=', $_POST['description']);
    opt_update('a2V5d29yZHM=', $_POST['keywords']);
    opt_update('cmVjYXB0Y2hhLXNpdGU=', $_POST['re_site']);
    opt_update('cmVjYXB0Y2hhLWtleQ==', $_POST['re_secret']);
    $_SESSION['alert'] = ['success', 'Success!', 'Data berhasil di edit.'];
    exit(header("Location: ".base_url('/admin/setting/')));
}
if (isset($_GET['hapus'])) {
    $hapus = addslashes(trim(htmlspecialchars(mysqli_real_escape_string($db, $_GET['hapus']))));
    
    $query_slide = mysqli_query($db, "SELECT * FROM slider WHERE id = '$hapus'");
    
    if (mysqli_num_rows($query_slide) === 1) {
        $fetch_slide = mysqli_fetch_assoc($query_slide);
        
        unlink('../../assets/slide/' . $fetch_slide['img']);
        
        mysqli_query($db, "DELETE FROM slider WHERE id = '$hapus'");
        
        $_SESSION['alert'] = ['success', 'Success!', 'Data berhasil di hapus.'];
        exit(header("Location: ".base_url('/admin/setting/')));

    } else {
        $_SESSION['alert'] = ['danger', 'Failed!', 'Data slide tidak ditemukan.'];
        exit(header("Location: ".base_url('/admin/setting/')));
    }
}

if (isset($_POST['tombol_slide'])) {
    
    if (isset($_FILES['gambar'])) {
        if ($_FILES['gambar']['error'] !== 4) {
            
            $format_file = end(explode('.', strtolower($_FILES['gambar']['name'])));
        
            if (in_array($format_file, ['jpg', 'jpeg', 'png'])) {
                
                $nama_file = uniqid() . '.' . $format_file;
                
                move_uploaded_file($_FILES['gambar']['tmp_name'], '../../assets/slide/' . $nama_file);
                
                mysqli_query($db, "INSERT INTO slider VALUES ('','$nama_file','Guest')");
                
                $_SESSION['alert'] = ['success', 'Success!', 'Gambar slide berhasil ditambah.'];
                exit(header("Location: ".base_url('/admin/setting/')));
        
            } else {
                $_SESSION['alert'] = ['danger', 'Failed!', 'Format gambar tidak sesuai.'];
                exit(header("Location: ".base_url('/admin/setting/')));
            }
        } else {
            $_SESSION['alert'] = ['danger', 'Failed!', 'Gambar tidak boleh kosong.'];
            exit(header("Location: ".base_url('/admin/setting/')));
        }
    } else {
        $_SESSION['alert'] = ['danger', 'Failed!', 'Gambar tidak boleh kosong.'];
        exit(header("Location: ".base_url('/admin/setting/')));
    }
}
include_once '../../layouts/header-admin.php'; ?>

<style>
    .input-group-text {
        background: #e5e5e5;
    }
</style>
<div class="row justify-content-center">
    <div class="col-12">
        <ul class="nav nav-pills justify-content-left mt-4 horitab-scroll" id="myTab" role="tablist">
            <li class="nav-item ms-1 nav-tab" role="presentation">
                <button class="btn-sm btn-category nav-link active" id="General-tab" data-bs-toggle="tab" data-bs-target="#General" type="button" role="tab" aria-controls="General" aria-selected="true"><small>General</small></button>
            </li>
            <li class="nav-item ms-1 nav-tab" role="presentation">
                <button class="btn-sm btn-category nav-link" id="Logo-tab" data-bs-toggle="tab" data-bs-target="#Logo" type="button" role="tab" aria-controls="Logo" aria-selected="true"><small>Logo</small></button>
            </li>
            <li class="nav-item ms-1 nav-tab" role="presentation">
                <button class="btn-sm btn-category nav-link" id="Slide-tab" data-bs-toggle="tab" data-bs-target="#Slide" type="button" role="tab" aria-controls="Slide" aria-selected="true"><small>Slide</small></button>
            </li>
            <li class="nav-item ms-1 nav-tab" role="presentation">
                <button class="btn-sm btn-category nav-link" id="WA-tab" data-bs-toggle="tab" data-bs-target="#WA" type="button" role="tab" aria-controls="WA" aria-selected="true"><small>WA Gateway</small></button>
            </li>
            <li class="nav-item ms-1 nav-tab" role="presentation">
                <button class="btn-sm btn-category nav-link" id="Sosial-tab" data-bs-toggle="tab" data-bs-target="#Sosial" type="button" role="tab" aria-controls="Sosial" aria-selected="true"><small>Sosial Media</small></button>
            </li>
            <li class="nav-item ms-1 nav-tab" role="presentation">
                <button class="btn-sm btn-category nav-link" id="Lainnya-tab" data-bs-toggle="tab" data-bs-target="#Lainnya" type="button" role="tab" aria-controls="Lainnya" aria-selected="true"><small>Lainnya</small></button>
            </li>
        </ul>
        <div class="tab-content" id="myTabContent">
        <div id="General" class="tab-pane active" role="tabpanel" aria-labelledby="General-tab">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">General</h4>
            </div>
            <div class="card-body card-dashboard">
                <form action="" method="POST" enctype="multipart/form-data">
                    <div class="form-group mb-3">
                        <label>Nama Website</label>
                        <input type="text" class="form-control" autocomplete="off" placeholder="wcode.id" value="<?= opt_get('dGl0bGU='); ?>" name="title">
                    </div>
                    <div class="form-group mb-3">
                        <label>Nav Title</label>
                        <input type="text" class="form-control" autocomplete="off" placeholder="wcode.id" value="<?= opt_get('dGl0bGVfd2Vi'); ?>" name="title_web">
                    </div>
                    <div class="form-group mb-3">
                        <label>Author</label>
                        <input type="text" class="form-control" autocomplete="off" placeholder="wcode.id" value="<?= opt_get('YXV0aG9y'); ?>" name="author">
                    </div>
                    <div class="form-group mb-3">
                        <label>Deskripsi</label>
                        <input type="text" class="form-control" autocomplete="off" placeholder="wcode.id" value="<?= opt_get('ZGVzY3JpcHRpb24='); ?>" name="description">
                    </div>
                    <div class="form-group mb-3">
                        <label>Keywords</label>
                        <input type="text" class="form-control" autocomplete="off" placeholder="wcode.id" value="<?= opt_get('a2V5d29yZHM='); ?>" name="keywords">
                    </div>
                    <div class="form-group mb-3">
                        <label>Recaptcha Secret</label>
                        <input type="text" class="form-control" autocomplete="off" placeholder="wcode.id" value="<?= opt_get('cmVjYXB0Y2hhLWtleQ=='); ?>" name="re_secret">
                    </div>
                    <div class="form-group mb-3">
                        <label>Recaptcha Site</label>
                        <input type="text" class="form-control" autocomplete="off" placeholder="wcode.id" value="<?= opt_get('cmVjYXB0Y2hhLXNpdGU='); ?>" name="re_site">
                    </div>
                    <div class="text-right">
                        <button class="btn btn-success" type="submit" name="tombol">Simpan</button>
                    </div>
                </form>
            </div>
        </div>
        </div>
        <div id="WA" class="tab-pane fade" role="tabpanel" aria-labelledby="WA-tab">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">WhatsApp Gateway</h4>
            </div>
            <div class="card-body card-dashboard">
                <form action="" method="POST">
                    <div class="form-group mb-3">
                        <label>Nomor WhatsApp</label>
                        <input type="number" class="form-control" placeholder="wcode.id" autocomplete="off" value="<?= opt_get('dHAtcGhvbmU=');?>" name="nomor">
                    </div>
                    <div class="form-group mb-3">
                        <label>API KEY</label>
                        <input type="text" class="form-control" placeholder="wcode.id" autocomplete="off" value="<?= opt_get('dHAta2V5');?>" name="key">
                    </div>
                    <div class="text-right">
                        <button class="btn btn-primary" type="submit" name="tombol_wa">Simpan</button>
                    </div>
                    </div>
                </form>
            </div>
        </div>
        
        <div id="Lainnya" class="tab-pane fade" role="tabpanel" aria-labelledby="Lainnya-tab">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">Lainnya</h4>
            </div>
            <div class="card-body card-dashboard">
                <form action="" method="POST">
                    <h5 class="mb-2">Penarikkan Saldo Mutasi OVO,Gopay</h5>
                    <div class="form-group mb-2">
                        <label>Bank</label>
                        <select class="form-control" name="bank-code">
                            <option value="">Pilih Bank</option>
                            <option value="BCA" <?= opt_get('YmFuay1jb2Rl') == "BCA" ? "selected" : "";?>>Bank Central Asia</option>
                            <option value="BRI" <?= opt_get('YmFuay1jb2Rl') == "BRI" ? "selected" : "";?>>Bank Rakyat Indonesia</option>
                            <option value="BNI" <?= opt_get('YmFuay1jb2Rl') == "BNI" ? "selected" : "";?>>Bank Negara Indonesia</option>
                            <option value="PERMATA" <?= opt_get('YmFuay1jb2Rl') == "PERMATA" ? "selected" : "";?>>Bank Permata</option>
                            <option value="MANDIRI" <?= opt_get('YmFuay1jb2Rl') == "MANDIRI" ? "selected" : "";?>>Bank Mandiri</option>
                            <optioon value="CIMB" <?= opt_get('YmFuay1jb2Rl') == "CIMB" ? "selected" : "";?>>Bank Cimb Niaga</option>
                        </select>
                    </div>
                    <div class="form-group mb-4">
                        <label>No Rekening</label>
                        <input type="text" class="form-control" placeholder="wcode.id" autocomplete="off" value="<?= opt_get('YmFuay1udW1iZXI=');?>" name="bank-number">
                    </div>
                    <h5 class="mb-2">Kostum Harga Upgrade Level</h5>
                    <div class="form-group mb-2">
                        <label>Reseller</label>
                        <input type="number" class="form-control" placeholder="wcode.id" autocomplete="off" value="<?= opt_get('aGFyZ2FfcmVzZWxsZXI=');?>" name="reseller">
                    </div>
                    <div class="form-group mb-2">
                        <label>Vip</label>
                        <input type="text" class="form-control" placeholder="wcode.id" autocomplete="off" value="<?= opt_get('aGFyZ2FfcHJlbWl1bQ==');?>" name="vip">
                    </div>
                    <div class="text-right">
                        <button class="btn btn-primary" type="submit" name="tombol_lainnya">Simpan</button>
                    </div>
                    </div>
                </form>
            </div>
        </div>
        <div id="Logo" class="tab-pane fade" role="tabpanel" aria-labelledby="Logo-tab">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">Logo</h4>
            </div>
            <div class="card-body card-dashboard">
                <form action="" method="POST" enctype="multipart/form-data">
                    <div class="form-group mb-3">
                        <label>Favicon Web</label> <br>
                        <img src="<?= opt_get('aWNvbi13ZWI='); ?>" width="125" class="mb-1">
                        <div class="custom-file">
                            <input type="file" class="custom-file-input" id="customFile" name="logo_guest">
                            <label class="custom-file-label" for="customFile">Ganti gambar</label>
                        </div>
                    </div>
                    <div class="form-group mb-3">
                        <label>Status Popup</label>
                        <select class="form-control" name="popup_guest_status">
                            <option value="On" <?= opt_get('cG9wdXBfZ3Vlc3Rfc3RhdHVz') == 'On' ? 'selected' : ''; ?>>On (aktif)</option>
                            <option value="Off" <?= opt_get('cG9wdXBfZ3Vlc3Rfc3RhdHVz') == 'Off' ? 'selected' : ''; ?>>Off (nonaktif)</option>
                        </select>
                    </div>
                    <div class="form-group mb-3">
                        <label>Gambar Popup</label><br>
                        <img src="<?= opt_get('cG9wdXBfZ3Vlc3RfbGluaw=='); ?>" width="125" class="mb-1">
                        <div class="custom-file">
                            <input type="file" class="custom-file-input" id="customFile" name="popup_guest_link">
                            <label class="custom-file-label" for="customFile">Ganti gambar</label>
                        </div>
                    </div>
                    <div class="col-md-6">
                            <div class="form-group mb-3">
                                <label>News Popop</label>
                                <textarea name="keterangan"><?= opt_get('cG9wdXAtbmV3cw=='); ?></textarea>
                            </div>
                        </div><br>
                    <div class="text-right">
                        <button type="submit" class="btn btn-success" name="tombol_logo">Simpan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div id="CheckId" class="tab-pane fade" role="tabpanel" aria-labelledby="CheckId-tab">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">Api Check ID</h4>
            </div>
            <div class="card-body card-dashboard">
                <form action="" method="POST">
                    <div class="form-group mb-3">
                        <label>Api Key</label>
                        <input type="text" class="form-control" autocomplete="off" value="<?= opt_get('Y2VraWQta2V5');?>" name="ck-api">
                    </div>
                    <small>Hubungi Developer di <a href="https://wa.me/6281352725651">WhatsApp</a> untuk perpanjang api nickname</small>
                    <div class="text-right">
                        <button class="btn btn-primary" type="submit" name="tombol_ck">Simpan</button>
                        <br><br>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div id="Sosial" class="tab-pane fade" role="tabpanel" aria-labelledby="Sosial-tab">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">Sosial Media</h4>
            </div>
            <div class="card-body card-dashboard">
                <form action="" method="POST">
                    <div class="form-group mb-3">
                        <label>WhatsApp</label>
                        <input type="text" class="form-control" autocomplete="off" value="<?= opt_get('d2EtYWRtaW4=');?>" name="wa-admin">
                    </div>
                    <div class="form-group mb-3">
                        <label>Instagram</label>
                        <input type="text" class="form-control" autocomplete="off" value="<?= opt_get('aWctYWRtaW4=');?>" name="ig-admin">
                    </div>
                    <div class="form-group mb-3">
                        <label>Email</label>
                        <input type="text" class="form-control" autocomplete="off" value="<?= opt_get('ZW1haWwtYWRtaW4=');?>" name="email-admin">
                    </div>
                    <div class="form-group mb-3">
                        <label>Alamat</label>
                        <input type="text" class="form-control" autocomplete="off" value="<?= opt_get('YWxhbWF0LWFkbWlu');?>" name="alamat-admin">
                    </div>
                    <div class="text-right">
                        <button class="btn btn-primary" type="submit" name="tombol_sosial">Simpan</button>
                        <br><br>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div id="Slide" class="tab-pane fade" role="tabpanel" aria-labelledby="Slide-tab">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">Slide</h4>
            </div>
            <div class="card-body card-dashboard">
                <div class="table-responsive">
                    <table class="table">
                        <tr>
                            <th>Gambar</th>
                            <th width="10">Action</th>
                        </tr>
                        <?php 
                        $no = 1;
                        $query_pembelian_guest = mysqli_query($db, "SELECT * FROM slider WHERE posisi = 'Guest' ORDER BY id DESC");
                        while($fetch_pembelian_guest = mysqli_fetch_assoc($query_pembelian_guest)): 
                        ?>
                        <tr>
                            <td>
                                <img src="../../assets/slide/<?= $fetch_pembelian_guest['img']; ?>" style="max-height: 100px">
                            </td>
                            <td style="white-space: nowrap;">
                                <a class="btn btn-danger btn-sm" href="/admin/setting?hapus=<?= $fetch_pembelian_guest['id']; ?>">Hapus</a>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                        <?php if (mysqli_num_rows($query_pembelian_guest) == 0): ?>
                        <tr>
                            <td colspan="7" align="center">Tidak ada data</td>
                        </tr>
                        <?php endif; ?>
                    </table>
                </div>
            </div>
        </div>
        <div class="card bg-dark shadow">
            <div class="card-body">
                <h4 class="card-title">Tambah Slide</h4>
                <form action="" method="POST" enctype="multipart/form-data">
                    <div class="form-group mb-3">
                        <label class="mb-1">Gambar Slide</label>
                        <div class="custom-file">
                            <input type="file" class="custom-file-input" id="customFile" name="gambar">
                            <label class="custom-file-label" for="customFile">Pilih Gambar</label>
                        </div>
                    </div>
                    <div class="text-right">
                        <button class="btn btn-success" type="submit" name="tombol_slide">Tambahkan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
</div>
</div>

<script src="https://cdn.ckeditor.com/4.17.2/standard/ckeditor.js"></script>
<script>
    CKEDITOR.replace('keterangan', {
        height: 400,
    });
</script>

<script>
$(".custom-file-input").on("change", function() {
    var fileName = $(this).val().split("\\").pop();
    $(this).siblings(".custom-file-label").addClass("selected").html(fileName);
});
</script>
<script>
    function selec_style(id) {
        $("#input-games-style").val(id);
        
        $(".games-style").removeClass('style-active');
        $("#games-style-" + id).addClass('style-active');
    }
</script>
<?php include_once '../../layouts/footer-admin.php'; ?>