<?php
session_start();
require_once '../config.php';
load('middleware', 'user');
$tanggal = date('Y-m-d H:i:s');
$username = $_SESSION['user']['username'];
if ($_SESSION['user']['level'] == "" || $_SESSION['user']['level'] == "Admin" || $_SESSION['user']['level'] == "Vip"){
    header("location: ".base_url('id'));
}else{
    if (isset($_POST['up'])) {
		$post_level = $_POST['role'];
		if($post_level == "Reseller"){
		    $harga = opt_get('aGFyZ2FfcmVzZWxsZXI=');
		}else if($post_level == "Vip"){
		    $harga = opt_get('aGFyZ2FfcHJlbWl1bQ==');
		}
		if($_SESSION['user']['saldo'] <> $harga){
		    $_SESSION['alert'] = ['danger','Failed!','Saldo Anda Tidak Mencukupi Untuk Upgrade ke '.$post_level];
		    exit(header('location: /account/upgrade-level'));
		}else{
		    if($post_level == "Reseller"){
		        $db->query("UPDATE users SET level='$post_level',saldo='saldo-{$harga}' WHERE username='$username'");
		        $_SESSION['alert'] = ['success','Success!','Upgrade akun ke Reseller Berhasil'];
		        exit(header('location: /account/upgrade-level'));
		    }else if($post_level == "Vip"){
		        $db->query("UPDATE users SET level='$post_level',saldo='saldo-{$harga}' WHERE username='$username'");
		        $_SESSION['alert'] = ['success','Success!','Upgrade akun ke Vip Berhasil'];
		        exit(header('location: /account/upgrade-level'));
		    }
		}
    }
}
	require_once '../layouts/header.php';
?>
	    <div class="row justify-content-center mb-4">
            <div class="col-12 col-md-8 mt-4">
                <div class="card bg-dark shadow">
                    <div class="card-header">
                        <h4 class="card-title float-left">UPGRADE LEVEL</h4>
                    </div>
                    <div class="card-body">
                        <form class="form-horizontal" method="POST">
                            <div class="form-group mb-3">
                                <h4>Informasi:</h4>
                                <ol>
                                    <li>Upgrade Level Ini Menggunakan Saldo Akun</li>
                                    <li>Pastikan Saldo Anda Mencukupi Untuk Upgrade Level</li>
                                    <li>Pastikan Sudah Memilih Level Yang Benar, Dikarenakan Tidak Bisa Dibatalkan</li>
                                </ol>
                            </div>	 
                            <div class="form-group mb-3">
                                <h4>Select Level:</h4>
	                            <div class="list-group list-group-numbered">
	                                <?php if($_SESSION['user']['level'] == "Member"):?>
                                    <label class="list-group-item bg-dark text-white d-flex justify-content-between align-items-start">
                                    <input type="radio" class="form-check-input me-1" name="role" id="role" value="Reseller" data-price="<?= opt_get('aGFyZ2FfcmVzZWxsZXI=');?>">
                                    <div class="ms-2 me-auto">
                                        <div class="fw-bold">Reseller</div> Akun Reseller Mendapatkan Harga Murah
                                    </div>
                                    <span class="badge bg-primary rounded-pill">Rp. <?= number_format(opt_get('aGFyZ2FfcmVzZWxsZXI='),0,',','.');?></span>
                                    </label>
                                    <?php endif;?>
                                    <label class="list-group-item bg-dark text-white d-flex justify-content-between align-items-start active">
                                    <input type="radio" class="form-check-input me-1" name="role" id="role" value="Vip" data-price="<?= opt_get('aGFyZ2FfcHJlbWl1bQ==');?>">
                                    <div class="ms-2 me-auto">
                                        <div class="fw-bold">Vip</div> Akun Vip Mendapatkan Harga Lebih Murah
                                    </div>
                                    <span class="badge bg-primary rounded-pill">Rp. <?= number_format(opt_get('aGFyZ2FfcHJlbWl1bQ=='),0,',','.');?></span>
                                    </label>
                                </div>
	                        </div>
	                        
	                        <div class="col-12">
                                <button type="submit" class="pull-right btn btn-block btn--md btn-primary waves-effect w-md waves-light" name="up">Upgrade</button>
                            </div>
		               </form>
		            </div>
	            </div>
	        </div>
        </div>	
<?php include_once '../layouts/footer.php'; ?>
<script>
    $(document).ready(() => {
            $("input[type='radio'][name='role']").change(function() {
                $('label.active').removeClass('active')
                if ($(this).is(':checked')) {
                    $(this).parent('label').addClass('active')
                }
            });
            $("input[type=radio]#role").change(rolePrice)
        });
</script>