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

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

if(isset($_POST['mem-tombol'])){
    $mems = $_POST['member'];
    $prem = $_POST['premium'];
    $ress = $_POST['reseller'];
    if(!$prem || !$mems || !$ress){
        $_SESSION['alert'] = ['danger', 'Failed!', 'Masih ada data yang kosong.'];
        exit(header('location: /admin/guest/harga'));
    }else{
        $db->query("UPDATE options SET opt_value='$mems' WHERE opt_name='df_harga_member'");
	    $db->query("UPDATE options SET opt_value='$prem' WHERE opt_name='df_harga_premium'");
	    $db->query("UPDATE options SET opt_value='$ress' WHERE opt_name='df_harga_reseller'");
	    $_SESSION['alert'] = ['success', 'Success!', 'Berhasil Update Harga.'];
	    exit(header('location: /admin/guest/harga'));
    }
}
$harga = $db->query("SELECT * FROM options WHERE opt_name='df_harga_member'")->fetch_assoc();
$harga_prem = $db->query("SELECT * FROM options WHERE opt_name='df_harga_premium'")->fetch_assoc();
$harga_ress = $db->query("SELECT * FROM options WHERE opt_name='df_harga_reseller'")->fetch_assoc();
    
include_once '../../layouts/header-admin.php'; ?>
<style>
    .box-games-harga.text-white h5 {
        color: #fff !important;
    }
</style>
<div class="row justify-content-center">
    <div class="col-md-10 col-12">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">Kostum Harga</h4>
            </div>
            <div class="card-body card-dashboard">
                <form action="" method="POST">
                    <span>Dalam Bentuk Persen (%)</span>
                    <div class="form-group mb-3">
                        <label>Harga Basic: </label>
                        <input type="text" class="form-control" placeholder="MyPulsaa" autocomplete="off" value="<?= $harga['opt_value']; ?>" name="member">
                    </div>
                    <div class="form-group mb-3">
                        <label>Harga Reseller</label>
                        <input type="text" class="form-control" placeholder="MyPulsaa" autocomplete="off" value="<?= $harga_ress['opt_value']; ?>" name="reseller">
                    </div>
                    <div class="form-group mb-3">
                        <label>Harga Vip</label>
                        <input type="text" class="form-control" placeholder="MyPulsaa" autocomplete="off" value="<?= $harga_prem['opt_value']; ?>" name="premium">
                    </div>
                    <div class="text-right mb-3">
                        <button class="btn btn-success" type="submit" name="mem-tombol">Simpan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="col-md-10 col-12">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">Harga</h4>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-4">
                        <?php
                        $query_games_guest = mysqli_query($db, "SELECT * FROM games_guest");
                        while($fetch_games_guest = mysqli_fetch_assoc($query_games_guest)): 
                        ?>
                        <div id="btn-games-<?= $fetch_games_guest['id']; ?>" class="border mb-1 box-games-harga" style="border-radius: 8px;padding: 8px;cursor: pointer;" onclick="show_produk('<?= $fetch_games_guest['product']; ?>', '<?= $fetch_games_guest['id']; ?>');">
                            <img src="/assets/games/<?= $fetch_games_guest['img']; ?>" width="35" class="mr-1 d-inline-block" style="border-radius: 5px;">
                            <h5 class="d-inline-block"><?= $fetch_games_guest['name']; ?></h5>
                        </div>
                        <?php endwhile; ?>
                    </div>
    <div class="col-md-8">
        <div class="table-responsive">
            <table class="table border">
                <thead>
                    <tr class="text-center">
                        <th rowspan="2">No</th>
                        <th rowspan="2">Produk</th>
                        <th colspan="3">Harga</th>
                    </tr>
                    <tr class="text-center">
                        <th>Basic</th>
                        <th>Reseller</th>
                        <th>Vip</th>
                    </tr>
                </thead>
               <tbody id="result-ajax-produk">
                    <tr>
                        <td colspan="4" align="center">Tidak ada produk</td>
                    </tr>
               </tbody>
             </table>
        </div>
    </div>
</div>

<?php include_once '../../layouts/footer-admin.php'; ?>
<script>
    function show_produk(produk, id) {
        $(".box-games-harga").removeClass('bg-primary text-white');
        $("#btn-games-" + id).addClass('bg-primary text-white');
        
        $.ajax({
            url: '<?= base_url(); ?>/admin/guest/ajax/get-harga.php',
            data: 'produk=' + produk,
            type: 'POST',
            dataType: 'html',
            success: function(result) {
                $("#result-ajax-produk").html(result);
            }
        });
    }
    </script>















