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

if ($_SESSION['user']['level'] !== "Admin") {
    header('location:' . base_url('id'));
}

if (isset($_GET['hapus'])) {
    $hapus = $_GET['hapus'];
    
    mysqli_query($db, "DELETE FROM metode_guest WHERE id = '$hapus'");
    
    $_SESSION['alert'] = ['success', 'Success!', 'Data berhasil di hapus.'];
    exit(header('location: /admin/guest/metode'));
}

if (isset($_POST['tombol'])) {
    if (isset($_POST['tipe']) AND isset($_POST['metode']) AND isset($_POST['sistem'])) {
        
        $tipe = $_POST['tipe'];
        $metode = $_POST['metode'];
        $sistem = $_POST['sistem'];
        $rek = $_POST['rek'];
        $ket = $_POST['ket'];
        $sub_ket = $_POST['sub_ket'];
        
        if (empty($tipe) OR empty($metode) OR empty($sistem)) {
            $_SESSION['alert'] = ['danger', 'Failed!', 'Masih ada data yang kosong.'];
            exit(header('location: /admin/guest/metode'));
        } else {
            
            if (isset($_FILES['img'])) {
                if ($_FILES['img']['error'] == 4) {
                    $_SESSION['alert'] = ['danger', 'Failed!', 'Gambar metode tidak boleh kosong.'];
                    exit(header('location: /admin/guest/metode'));
            
                } else {
                    $format_file = end(explode('.', strtolower($_FILES['img']['name'])));
                    
                    if (in_array($format_file, ['jpg', 'jpeg', 'png'])) {
                        $nama_file = uniqid() . '.' . $format_file;
                        
                        move_uploaded_file($_FILES['img']['tmp_name'], '../../assets/metode/' . $nama_file);
                    } else {
                        $_SESSION['alert'] = ['danger', 'Failed!', 'Format gambar tidak sesuai.'];
                        exit(header('location: /admin/guest/metode'));
                
                    }
                    
                    if ($sistem !== "Mutasi" && $sistem !== "Manual") {
                        if (isset($_POST['kode'])) {
                            
                            $kode = $_POST['kode'];
                            
                            if (empty($kode)) {
                                $_SESSION['alert'] = ['danger', 'Failed!', 'Kode metode tidak boleh kosong.'];
                                exit(header('location: /admin/guest/metode'));
                        
                            }
                        } else {
                            $_SESSION['alert'] = ['danger', 'Failed!', 'Kode metode tidak boleh kosong.'];
                            exit(header('location: /admin/guest/metode'));
                    
                        }
                    } else {
                        $kode = '';
                    }
                    
                    mysqli_query($db, "INSERT INTO metode_guest VALUES ('','$tipe','$metode','$sistem','$kode','$nama_file','$rek','$ket','$sub_ket')");
                    
                    $_SESSION['alert'] = ['success', 'Success!', 'Data berhasil di tambahkan.'];
                    exit(header('location: /admin/guest/metode'));
                }
            } else {
                $_SESSION['alert'] = ['danger', 'Failed!', 'Gambar metode tidak boleh kosong.'];
                exit(header('location: /admin/guest/metode'));
        
            }
        }
    } else {
        $_SESSION['alert'] = ['danger', 'Failed!', 'Masih ada data yang kosong.'];
        exit(header('location: /admin/guest/metode'));
    }
}

if (isset($_POST['edit'])) {
    if (isset($_POST['tipe']) AND isset($_POST['metode']) AND isset($_POST['sistem'])) {
        
        $id = $_POST['id'];
        $tipe = $_POST['tipe'];
        $metode = $_POST['metode'];
        $sistem = $_POST['sistem'];
        $rek = $_POST['rek'];
        $ket = $_POST['ket'];
        $sub_ket = $_POST['sub_ket'];
        
        if (empty($tipe) OR empty($metode) OR empty($sistem)) {
            $_SESSION['alert'] = ['danger', 'Failed!', 'Masih ada data yang kosong.'];
            exit(header('location: /admin/guest/metode'));
        } else {
            
            if ($sistem !== "Manual" && $sistem !== "Mutasi") {
                if (isset($_POST['kode'])) {
                    
                    $kode = $_POST['kode'];
                    
                    if (empty($kode)) {
                        $_SESSION['alert'] = ['danger', 'Failed!', 'Kode metode tidak boleh kosong.'];
                        exit(header('location: /admin/guest/metode'));
                
                    }
                } else {
                    $_SESSION['alert'] = ['danger', 'Failed!', 'Kode metode tidak boleh kosong.'];
                    exit(header('location: /admin/guest/metode'));
            
                }
            } else {
                $kode = '';
            }
            
            mysqli_query($db, "UPDATE metode_guest SET tipe = '$tipe', metode = '$metode', kode = '$kode', sistem = '$sistem', rek = '$rek', ket = '$ket', sub_ket = '$sub_ket' WHERE id = '$id'");
            
            if (isset($_FILES['img'])) {
                
                if ($_FILES['img']['error'] !== 4) {
                    
                    $format_file = end(explode('.', strtolower($_FILES['img']['name'])));
                    
                    if (in_array($format_file, ['jpg', 'jpeg', 'png'])) {
                        
                        $nama_file = uniqid() . '.' . $format_file;
                        
                        move_uploaded_file($_FILES['img']['tmp_name'], '../../assets/metode/' . $nama_file);
                        
                        mysqli_query($db, "UPDATE metode_guest SET img = '$nama_file' WHERE id = '$id'");
                    }
                }
            }
            
            $_SESSION['alert'] = ['success', 'Success!', 'Data berhasil di simpan.'];
            exit(header('location: /admin/guest/metode'));
    
        }
    }
}

include_once '../../layouts/header-admin.php'; ?>
<div class="row justify-content-center">
    <div class="col-md-8 col-12">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">Metode Pembayaran</h4>
            </div>
            <div class="card-body card-dashboard">
                <div class="mb-2">
                    <button class="btn btn-outline-primary" type="button" onclick="modal_tambah();">Tambah Metode</button>
                </div>
                <div class="table-responsive">
                    <table class="table table-striped table-dark table-bordered table-hover" id="tbb">
                        <thead>
                        <tr>
                            <th>Gambar</th>
                            <th>Metode</th>
                            <th>Create By</th>
                            <th>Tipe</th>
                            <th>Sistem</th>
                            <th width="10">Action</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        $query_metode_guest = mysqli_query($db, "SELECT * FROM metode_guest ORDER BY id DESC");
                        while($fetch_metode_guest = mysqli_fetch_assoc($query_metode_guest)) :
                        ?>
                        <tr>
                            <td><img src="/assets/metode/<?= $fetch_metode_guest['img']; ?>" class="p-2 bg-light" style="border-radius:6px;" width="60"></td>
                            <td><?= $fetch_metode_guest['metode']; ?></td>
                            <td><code>wcode.id</code></td>
                            <td><?= $fetch_metode_guest['tipe']; ?></td>
                            <td><b><?= $fetch_metode_guest['sistem']; ?></b></td>
                            <td width="10" style="white-space: nowrap;">
                                <button onclick="edit_metode('<?= $fetch_metode_guest['id']; ?>');" class="btn btn-info btn-sm">Edit</button>
                                <a href="/admin/guest/metode.php?hapus=<?= $fetch_metode_guest['id']; ?>" class="btn btn-danger btn-sm">Hapus</a>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="modal-tambah" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content bg-dark">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Tambah Metode</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </button>
            </div>
            <div class="modal-body">
                <form action="" method="POST" enctype="multipart/form-data">
                <div class="form-group mb-3">
                    <label>Tipe</label>
                    <select class="form-control" name="tipe">
                        <option value="">Pilih salah satu</option>
                        <option value="Saldo Akun">Saldo Akun</option>
                        <option value="Transfer Bank">Transfer Bank</option>
                        <option value="Virtual Account">Virtual Account</option>
                        <option value="Convenience Store">Convenience Store</option>
                        <option value="E-Wallet">E-Wallet</option>
                    </select>
                </div>
                <div class="form-group mb-3">
                    <label>Metode</label>
                    <input type="text" class="form-control" autocomplete="off" name="metode">
                </div>
                <div class="form-group mb-3">
                    <label>Gambar</label><br>
                    <div class="custom-file">
                        <input type="file" accept=".jpg, .jpeg, .png" class="custom-file-input" id="customFile" name="img">
                        <label class="custom-file-label" for="customFile"><?= $fetch_games['img']; ?></label>
                    </div>
                </div>
                <div class="form-group mb-3">
                    <label>Sistem</label>
                    <select class="form-control" name="sistem" onchange="select_sistem(this.value);">
                        <option value="">Pilih Sistem Payment</option>
                        <option value="Mutasi">Mutasi</option>
                        <option value="Manual">Manual</option>
                        <option value="Tripay">Tripay</option>
                        <option value="Xendit">Xendit</option>
                    </select>
                    <input type="text" class="form-control mt-1 d-none" id="input-kode" name="kode" placeholder="Kode Metode" autocomplete="off">
                    <input type="text" class="form-control mt-1" id="input-rek" name="rek" placeholder="Rekening Tujuan a/n LynchStore" autocomplete="off">
                </div>
                <div class="form-group mb-3">
                    <label>Keterangan</label>
                    <input type="text" class="form-control" autocomplete="off" name="ket">
                </div>
                <div class="form-group mb-3">
                    <label>Sub Keterangan</label>
                    <input type="text" class="form-control" autocomplete="off" name="sub_ket">
                </div>
                <div class="float-right">
                    <button type="button" data-bs-dismiss="modal" aria-label="Close" class="btn bg-light fw-bold">BACK</button>
                    <button type="submit" class="btn btn-primary" name="tombol">Simpan</button>
                </div>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="modal-edit" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content bg-dark">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Edit Metode</h5>
                <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close"><span aria-hidden="true" style="text-shadow: none; color:white;">&times;</span></button>
            </div>
            <div class="modal-body">
                <form action="" method="POST" enctype="multipart/form-data">
                    <div id="result-edit-metode"></div>
                    <div class="float-right mt-3">
                        <button type="button" data-bs-dismiss="modal" aria-label="Close" class="btn bg-light fw-bold">BACK</button>
                        <button type="submit" class="btn btn-primary" name="edit">Simpan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include_once '../../layouts/footer-admin.php'; ?>
<script>

    function modal_tambah() {
        $("#modal-tambah").modal('show');
    }
    
    function select_sistem(sistem) {
            if(sistem == 'Mutasi' || sistem == 'Manual'){
                $("#input-kode").addClass('d-none');
                $("#input-rek").removeClass('d-none');
            } else if(sistem == 'Xendit' || sistem == 'Tripay'){
                $("#input-kode").removeClass('d-none');
                $("#input-rek").addClass('d-none');
            }
        }
    
    function edit_metode(id) {
        $.ajax({
            url: '<?= base_url(); ?>/admin/guest/ajax/edit-metode.php?edit=' + id,
            success: function(result) {
                $("#result-edit-metode").html(result);
                
                $("#modal-edit").modal('show');
            }
        });
    }
    $(document).ready(function() {
	$('#tbb').dataTable();
 });
    $(".custom-file-input").on("change", function() {
        var fileName = $(this).val().split("\\").pop();
        $(this).siblings(".custom-file-label").addClass("selected").html(fileName);
    });
</script>