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

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

if (isset($_GET['id'])) {
    $id = addslashes(trim(htmlspecialchars($_GET['id'])));
    
    if (!empty($id)) {
        $query_games = mysqli_query($db, "SELECT * FROM games_guest WHERE id = '$id'");
        
        if (mysqli_num_rows($query_games) === 1) {
            $fetch_games = mysqli_fetch_assoc($query_games);
        } else {
            exit(header('location:/admin/guest/product/'));
        }
    } else {
        exit(header('location:/admin/guest/product/'));
    }
} else {
    exit(header('location:/admin/guest/product/'));
}

if (isset($_POST['tombol'])) {
    
    if (isset($_POST['name']) AND isset($_POST['operator']) AND isset($_POST['sistem_id']) AND isset($_POST['validate_status']) AND isset($_POST['keterangan'])) {
        $name = $_POST['name'];
        $sub_name = $_POST['sub_name'];
        $category = $_POST['category'];
        $product = $_POST['operator'];
        $sistem_id = $_POST['sistem_id'];
        $tipe = $_POST['tipe'];
        $validate_status = $_POST['validate_status'];
        $validate_code = $_POST['validate_code'];
        $urutan = $_POST['urutan'];
        $keterangan = $_POST['keterangan'];
        
        if (empty($name) OR empty($product) OR empty($sistem_id) OR empty($validate_status) OR empty($urutan) OR empty($keterangan)) {
            $_SESSION['alert'] = ['danger', 'Failed!', 'Masih ada data yang kosong.'];
            exit(header('location:/admin/guest/product/'));
        } else {
            
            $nama_file = $fetch_games['img'];
            
            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'])) {
                        
                        unlink('../../../assets/games/' . $nama_file);
                        
                        $nama_file = uniqid() . '.' . $format_file;
                        
                        move_uploaded_file($_FILES['gambar']['tmp_name'], '../../../assets/games/' . $nama_file);
                    } else {
                        $_SESSION['alert'] = ['danger', 'Failed!', 'Format gambar tidak sesuai.'];
                        exit(header('location:/admin/guest/product/'));

                    }
                }
            }
            
            $petunjuk = $fetch_games['petunjuk'];
            
            if (isset($_FILES['petunjuk'])) {
                if ($_FILES['petunjuk']['error'] !== 4) {
                    
                    $format_file = end(explode('.', strtolower($_FILES['petunjuk']['name'])));
                    
                    if (in_array($format_file, ['jpg', 'jpeg', 'png'])) {
                        
                        $petunjuk = uniqid() . '.' . $format_file;
                        
                        move_uploaded_file($_FILES['petunjuk']['tmp_name'], '../../../assets/images/' . $petunjuk);
                        
                        $petunjuk = base_url() . 'assets/images/' . $petunjuk;
                    } else {
                        $_SESSION['alert'] = ['danger', 'Failed!', 'Format petunjuk tidak sesuai.'];
                        exit(header('location:/admin/guest/product/'));

                    }
                }
            }
            
            if ($validate_status == 'Y') {
                if (empty($validate_code)) {
                    $_SESSION['alert'] = ['danger', 'Failed!', 'Kode validasi ID games tidak boleh kosong.'];
                    exit(header('location:/admin/guest/product/'));
                } else {
                    $validate_id = $validate_code;
                }
            } else {
                $validate_id = 'Tydacks';
            }
            
            mysqli_query($db, "UPDATE games_guest SET name = '$name', sub_name = '$sub_name', tipe_product='$tipe',category = '$category', product = '$product', sistem_id = '$sistem_id', validate_id = '$validate_id', img = '$nama_file', urutan = '$urutan', keterangan = '$keterangan', petunjuk = '$petunjuk' WHERE id = '$id'");
            
            $_SESSION['alert'] = ['success', 'Success!', 'Data berhasil di simpan.'];
            exit(header('location:/admin/guest/product/'));
            
        }
    } else {
        $_SESSION['alert'] = ['danger', 'Failed!', 'Masih ada data yang kosong.'];
        exit(header('location:/admin/guest/product/'));
    }
}

include_once '../../../layouts/header-admin.php'; ?>

<div class="row justify-content-center">
    <div class="col-md-8 col-12">
        <div class="card bg-dark">
            <div class="card-header">
                <h4 class="card-title">Edit Produk</h4>
            </div>
            <div class="card-body card-dashboard">
                <form action="" method="POST" enctype="multipart/form-data">
                    <div class="form-group mb-3">
                        <label>Nama Produk</label>
                        <input type="text" class="form-control" autocomplete="off" name="name" value="<?= $fetch_games['name']; ?>">
                    </div>
                    <div class="form-group mb-3">
                        <label>Sub Name</label>
                        <input type="text" class="form-control" autocomplete="off" name="sub_name" value="<?= $fetch_games['sub_name']; ?>">
                    </div>
                    <div class="form-group mb-3">
                        <label>Kategori</label>
                        <select class="form-control" name="category">
                            <?php
                            $query_games_category = mysqli_query($db, "SELECT DISTINCT kategori FROM games_kategori WHERE NOT kategori_id='0' ORDER BY urutan ASC");
                            while($fetch_games_category = mysqli_fetch_assoc($query_games_category)): ?>
                            <option value="<?= $fetch_games_category['kategori']; ?>" <?= $fetch_games_category['kategori'] == $fetch_games['category'] ? 'selected' : ''; ?>><?= $fetch_games_category['kategori']; ?></option>
                            <?php endwhile; ?>
                        </select>
                    </div>
                    <div class="form-group mb-3">
                        <label>Gambar</label><br>
                        <div class="mb-1">
                            <img src="/assets/games/<?= $fetch_games['img']; ?>" width="100">
                        </div>
                        <div class="custom-file">
                            <input type="file" accept=".jpg, .jpeg, .png" class="custom-file-input" id="customFile" name="gambar">
                            <label class="custom-file-label" for="customFile"><?= $fetch_games['img']; ?></label>
                        </div>
                    </div>
                    <div class="form-group mb-3">
                                <label>Pilih Kategori Produk</label>
                                <select class="form-control" name="tipe" id="tipe">
                                    <option value="0">Pilih salah satu</option>
                                    <option value="VGAME">Voucher Game</option>
                                    <option value="PULSA">Pulsa</option>
                                    <option value="DATA">Data</option>
                                    <option value="EMONEY">E-Money</option>
                                    <option value="SMSDANTELPON">Paket SMS & Telpon</option>
                                    <option value="SOSMED">Sosial Media</option>
                                </select>
                            </div>
                            <div class="form-group mb-3">
                                <label>Produk</label>
                                <select class="form-control" name="operator" id="operator">
                                    <option value="0">Pilih salah satu</option>
                                </select>
                            </div>
                        <div class="form-group mb-3">
                                <label>Sistem ID</label>
                                <select class="form-control" name="sistem_id">
                                    <option value="0">Pilih salah satu</option>
                                    <?php $sis = $db->query("SELECT * FROM form_input ORDER BY id ASC");
                                    while($siss = $sis->fetch_assoc()):?>
                                    <option value="<?= $siss['slug'];?>" <?= $fetch_games['sistem_id'] == $siss['slug'] ? 'selected' : '';?>><?= $siss['name'];?></option>
                                    <?php endwhile;?>
                                </select>
                            </div>
                    <div class="form-group mb-3">
                        <label>Validate ID</label>
                        <select class="form-control" name="validate_status" onchange="select_validate(this.value);">
                            <option value="Y" <?= $fetch_games['validate_id'] !== 'Tydacks' ? 'selected' : ''; ?>>Ya</option>
                            <option value="N" <?= $fetch_games['validate_id'] == 'Tydacks' ? 'selected' : ''; ?>>Tidak</option>
                        </select>
                        <div id="input-lynchstore" class="<?= $fetch_games['validate_id'] == 'Tydacks' ? 'd-none' : ''; ?>">
                            <input type="text" name="validate_code" class="form-control mt-1" placeholder="Kode Games" autocomplete="off" value="<?= $fetch_games['validate_id']; ?>">
                            <small>Cek kode games <a href="https://prnt.sc/7aMwdGi09FI4" target="_blank">disini</a></small>
                        </div>
                    </div>
                    <div class="form-group mb-3">
                        <label>Urutan</label>
                        <input type="number" class="form-control" name="urutan" autocomplete="off" value="<?= $fetch_games['urutan']; ?>">
                    </div>
                    <div class="form-group mb-3">
                        <label>Keterangan</label>
                        <textarea name="keterangan"><?= $fetch_games['keterangan']; ?></textarea>
                    </div>
                    <div class="form-group mb-3">
                        <label>Foto Petunjuk</label>
                        <div class="mb-1">
                            <img src="<?= $fetch_games['petunjuk']; ?>" width="100">
                        </div>
                        <div class="custom-file">
                            <input type="file" accept=".jpg, .jpeg, .png" class="custom-file-input" id="customFile" name="petunjuk">
                            <label class="custom-file-label" for="customFile"><?= $fetch_games['petunjuk']; ?></label>
                        </div>
                    </div>
                    <a href="/admin/guest/product/" class="btn btn-warning float-left">Kembali</a>
                    <div class="text-right">
                        <button type="reset" class="btn btn-default">Batal</button>
                        <button type="submit" class="btn btn-primary" name="tombol">Simpan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include_once '../../../layouts/footer-admin.php'; ?>
<script src="https://cdn.ckeditor.com/4.17.2/standard/ckeditor.js"></script>
<script>
    CKEDITOR.replace('keterangan', {
        height: 400,
    });
</script>
<script type="text/javascript">
		$(document).ready(function() {
		    $("#tipe").change(function() {
			    var tipe = $("#tipe").val();
		        $.ajax({
			        url: '<?= base_url(); ?>ajax/list-produk.php',
			        data: 'tipe=' + tipe,
			        type: 'POST',
			        dataType: 'html',
			        success: function(msg) {
				        $("#operator").html(msg);
			        }
		        });
	        });
		});
</script>
<script>
    function modal_tambah() {
        $("#exampleModal").modal('show');
    }
    function select_validate(status) {
        if (status == 'N') {
            $("#input-lynchstore").addClass('d-none');
        } else {
            $("#input-lynchstore").removeClass('d-none');
        }
    }
</script>
<script>
    $(".custom-file-input").on("change", function() {
        var fileName = $(this).val().split("\\").pop();
        $(this).siblings(".custom-file-label").addClass("selected").html(fileName);
    });
</script>