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

if ($_SESSION['user']['level'] == "") {
    header('location:' . base_url('id'));
}
$total_success = mysqli_num_rows($db->query("SELECT * FROM pembelian_guest WHERE status = 'Success' AND username='{$_SESSION['user']['username']}'"));
$total_harga = $db->query("SELECT SUM(harga+fee) AS total FROM pembelian_guest WHERE status = 'Success' AND username='{$_SESSION['user']['username']}'")->fetch_assoc()['total'];
if(isset($_POST['change_phone'])){
    $phone = $_POST['phone_new'];
    $password = $_POST['pass'];
    if(!$phone || !$password){
        $_SESSION['alert'] = ['danger','Failed','Masih Ada Form Yang Kosong'];
        exit(header('location: /account/'));
    }else{
        if(password_verify($password, $_SESSION['user']['password']) === false) {
				$_SESSION['alert'] = ['danger', 'Failed!', 'Invalid Password.'];
				exit(header('location: /account/'));
		}else{
		    $suc = $db->query("UPDATE users SET phone='$phone' WHERE username='{$_SESSION['user']['username']}'");
		    if($suc == true){
		        $_SESSION['alert'] = ['success','Success!','Nomor Telpon Berhasil Diganti'];
		        exit(header('location: /account/'));
		    }else{
		        $_SESSION['alert'] = ['danger', 'Failed!', 'Nomor Telpon Gagal Diganti'];
				exit(header('location: /account/'));
		    }
		}
    }
}
if(isset($_POST['change_email'])){
    $email = $_POST['email_new'];
    $password = $_POST['pass'];
    if(!$email || !$password){
        $_SESSION['alert'] = ['danger','Failed','Masih Ada Form Yang Kosong'];
        exit(header('location: /account/'));
    }else{
        if(password_verify($password, $_SESSION['user']['password']) === false) {
				$_SESSION['alert'] = ['danger', 'Failed!', 'Password Lama Yang Anda Masukkan Salah.'];
				exit(header('location: /account/'));
		}else{
		    $suc = $db->query("UPDATE users SET email='$email' WHERE username='{$_SESSION['user']['username']}'");
		    if($suc == true){
		        $_SESSION['alert'] = ['success','Success!','Nomor Telpon Berhasil Diganti'];
		        exit(header('location: /account/'));
		    }else{
		        $_SESSION['alert'] = ['danger', 'Failed!', 'Nomor Telpon Gagal Diganti'];
				exit(header('location: /account/'));
		    }
		}
    }
}

if(isset($_POST['change_pass'])){
    $pass_new = $_POST['pass_new'];
    $pass_new1 = $_POST['pass_new1'];
    $password = $_POST['pass'];
    if(!$pass_new || !$password){
        $_SESSION['alert'] = ['danger','Failed','Masih Ada Form Yang Kosong'];
        exit(header('location: /account/'));
    }else if($pass_new <> $pass_new1){
        $_SESSION['alert'] = ['danger','Failed','Password Tidak Cocok'];
        exit(header('location: /account/'));
    }else{
        if(password_verify($password, $_SESSION['user']['password']) === false) {
				$_SESSION['alert'] = ['danger', 'Failed!', 'Password Lama Yang Anda Masukkan Salah.'];
				exit(header('location: /account/'));
		}else{
		    $passs = password_hash($pass_new, PASSWORD_DEFAULT);
		    $suc = $db->query("UPDATE users SET password='$passs' WHERE username='{$_SESSION['user']['username']}'");
		    if($suc == true){
		        $_SESSION['alert'] = ['success','Success!','Password Berhasil Diganti'];
		        exit(header('location: /account/'));
		    }else{
		        $_SESSION['alert'] = ['danger', 'Failed!', 'Password Gagal Diganti'];
				exit(header('location: /account/'));
		    }
		}
    }
}
if(isset($_POST['btn_api_key'])){
    $apikey = random(64);
    if($db->query("UPDATE users SET api_key='$apikey' WHERE username='{$_SESSION['user']['username']}'") == true){
        $_SESSION['alert'] = ['success','Success!','Berhasil Memperbarui Api Key'];
		exit(header('location: /account/'));
    }else{
        $_SESSION['alert'] = ['danger','Failed!','Gagal Memperbarui Api Key'];
		exit(header('location: /account/'));
    }
}
if(isset($_POST['btn_api_id'])){
    $apiid = random(15);
    if($db->query("UPDATE users SET api_id='$apiid' WHERE username='{$_SESSION['user']['username']}'") == true){
        $_SESSION['alert'] = ['success','Success!','Berhasil Memperbarui Api Key'];
		exit(header('location: /account/'));
    }else{
        $_SESSION['alert'] = ['danger','Failed!','Gagal Memperbarui Api Key'];
		exit(header('location: /account/'));
    }
}
if(isset($_POST['btn_whitelist'])){
    $whitelist = $_POST['whitelist'];
    if(!$whitelist){
        $_SESSION['alert'] = ['danger','Failed!','Silahkan Isi Whitelist IP'];
		    exit(header('location: /account/'));
    }else{
        if($db->query("UPDATE users SET ip_static='$whitelist' WHERE username='{$_SESSION['user']['username']}'") == true){
            $_SESSION['alert'] = ['success','Success!','Berhasil Memperbarui Whitelist IP'];
		    exit(header('location: /account/'));
        }else{
            $_SESSION['alert'] = ['danger','Failed!','Gagal Memperbarui Whitelist IP'];
		    exit(header('location: /account/'));
        }
    }
}
require '../layouts/header.php';
?>
<div class="row mb-5">
    <div class="col-12 col-lg-4 mx-auto">
        <div class="card shadow bg-dark">
            <div class="card-body">
                <div class="row">
                    <div class="col-12 text-end">
                        <button class="btn btn-danger btn-sm" type="button" onclick="modal('Setting Api','<?=base_url('account/setting-api?id='.$_SESSION['user']['id']);?>','lg')"><i class="fas fa-code"></i> SETTING API</button>
                    </div>
                    <div class="col-12 text-center">
                        <img src="<?= opt_get('aWNvbi13ZWI=') ?>" width="150" class="avatar avatar-128 rounded-circle text-white p-2">
                    </div>
                </div>
                <hr>
                <dl>
                <table style="width:100%; font-size:13px;">
                    <tbody>
                        <tr>
                            <th>Name</th>
                            <td>: <?= $_SESSION['user']['name'];?></td>
                        </tr>
                        <tr>
                            <th>Username</th>
                            <td>: <?= $_SESSION['user']['username'];?></td>
                        </tr>
                        <tr>
                            <th>Email</th>
                            <td>: <?= $_SESSION['user']['email'];?> <span style="color:#28C76F!important;cursor: pointer;" type="button" onclick="modal('Change Email','<?=base_url('account/change-email?id='.$_SESSION['user']['id']);?>')">(Ubah)</span></td>
                        </tr>
                        <tr>
                            <th>Number</th>
                            <td>: <?= $_SESSION['user']['phone'];?> <span style="color:#28C76F!important;cursor: pointer;" type="button" onclick="modal('Change Phone','<?=base_url('account/change-phone?id='.$_SESSION['user']['id']);?>')">(Ubah)</span></td>
                        </tr>
                        <tr>
                            <th>Password</th>
                            <td>: XXXXXXXXX <span style="color:#28C76F!important;cursor: pointer;" type="button" onclick="modal('Change Password','<?=base_url('account/change-pass?id='.$_SESSION['user']['id']);?>')">(Ubah)</span></td>
                        </tr>
                        <tr>
                            <th>Balance</th>
                            <td>: <span class="badge bg-primary" id="balance" data-bs-toggle="tooltip" data-bs-placement="top" title="Click to add balance" type="button" onclick="window.location.href = '<?= base_url('account/deposit/');?>';">Rp <?= number_format($_SESSION['user']['saldo'],0,',','.');?></span> <span class="badge bg-primary" data-bs-toggle="tooltip" data-bs-placement="top" title="Click to add balance" type="button" onclick="window.location.href = '<?= base_url('account/deposit/');?>';">Deposit</span></td>
                        </tr>
                        <tr>
                            <th>Total Success</th>
                            <td>: <?= number_format($total_success,0,',','.');?> order</td>
                        </tr>
                        <tr>
                            <th>Total Purchase</th>
                            <td>: <span class="badge bg-success">Rp <?= number_format($total_harga,0,',','.');?></span></td>
                        </tr>
                        <tr>
                            <th>Level</th>
                            <td>: <?php if($_SESSION['user']['level'] == "Admin" || $_SESSION['user']['level'] == "Vip"):?><span class="badge bg-primary"><?= $_SESSION['user']['level'];?> </span><?php else:?><span class="badge bg-primary" id="status" data-bs-toggle="tooltip" data-bs-placement="top" title="Click to upgrade account" type="button" onclick="window.location.href = '<?= base_url('account/upgrade-level');?>';"><?= $_SESSION['user']['level'];?> </span> <span class="badge bg-primary" data-bs-toggle="tooltip" data-bs-placement="top" title="Click to upgrade account" type="button" onclick="window.location.href = '<?= base_url('account/upgrade-level');?>';">Upgrade </span> <?php endif;?></td>
                        </tr>
                    </tbody>
                </table>
                </dl>
            </div>
        </div>
    </div>
<div class="col-12 col-lg-8 mx-auto">
        <div class="card shadow bg-dark">
            <div class="card-header">
                <h5 class="card-title">History</h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                            <table class="table table-striped table-dark table-bordered table-hover dataTable no-footer"  style="font-size:12px;" id="tblInvoices">
                                <thead>
                                    <tr>                                    
                                        <th>Order ID</th>
                                        <th>Tanggal</th>
                                        <th>Produk</th>
                                        <th>Layanan</th>
                                        <th>ID</th>
                                        <th>Harga</th>
                                        <th>Status</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php 
                                    $checkk = $db->query("SELECT * FROM pembelian_guest   WHERE username='{$_SESSION['user']['username']}' ORDER BY id");
                                    while($history = $checkk->fetch_assoc()):
                                    ?>
                                    <tr>                                   
                                        
                                        <td><a href="<?= base_url('id/invoice?no='.$history['order_id']);?>">#<?= $history['order_id'];?></a></td>
                                        <td><?= format_date('id',$history['date_create']); ?></td>
                                        <td><?= $history['games'];?></td>
                                        <td><?= $history['layanan'];?></td>
                                        <td><?= $history['target']; ?><?php if($history['additional_target'] != null): ?>(<?= $history['additional_target'] ?? null ;?>) <?php endif; ?></td>
                                        <td>Rp. <?= number_format($history['harga']+$history['fee'],0,',','.');?></td>
                                        <?php if($history['status'] == "Success"){
                                            $bgg = "success";
                                            $stts = "SUCCESS";
                                            $icon = "far fa-check-circle";
                                        }else if($history['status'] == "Processing"){
                                            $bgg= "info";
                                            $stts = "PROCESSING";
                                            $icon = "fas fa-circle-notch fa-spin";
                                        }else if($history['status'] == "Waiting"){
                                            $bgg = "secondary";
                                            $stts = "WAITING";
                                            $icon = "fas fa-circle-notch fa-spin";
                                        }else if($history['status'] == "Canceled"){
                                            $bgg = "danger";
                                            $stts = "CANCELED";
                                            $icon = "fa-times-circle";
                                        }else if($history['status'] == "Pending"){
                                            $bgg = "warning";
                                            $stts = "PENDING";
                                            $icon = "";
                                        }
                                        ?>
                                        <td><span class="badge p-2 bg-<?=$bgg;?>"><i class="<?= $icon;?>"></i> <?= $stts;?></span></td>
                                    </tr>
                                    <?php endwhile;?>
                                </tbody>
                            </table>
                </div>
            </div>
        </div>
    </div>
</div>

<?php require '../layouts/footer-admin.php';?>
<div class="modal fade text-left" id="SModal" tabindex="-1" role="dialog" aria-labelledby="SModal" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog" id="SModal-size" role="document">
        <div class="modal-content bg-dark">
            <div class="modal-header">
                <h4 class="modal-title" id="SModal-title"></h4>
                <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" id="SModal-body"></div>
        </div>
    </div>
</div>
<script>
$(document).ready(function() {
	$('#tblInvoices').dataTable();
 });
    function modal(name, link, size) {
        var sizes = '';
        if (size == 'smaller' || size == 'xs') sizes = 'modal-xs';
        if (size == 'small' || size == 'sm') sizes = 'modal-sm';
        if (size == 'large' || size == 'lg') sizes = 'modal-lg';
        if (size == 'larger' || size == 'xl') sizes = 'modal-xl';

        $.ajax({
            type: "GET",
            url: link,
            beforeSend: function() {
                $('#SModal-body').html('Loading...');
            },
            success: function(result) {
                $('#SModal-body').html(result);
            },
            error: function() {
                $('#SModal-body').html('Failed to get contents...');
            }
        });

        $('#SModal-title').html(name);
        $('#SModal-size').removeClass('modal-xs');
        $('#SModal-size').removeClass('modal-sm');
        $('#SModal-size').removeClass('modal-lg');
        $('#SModal-size').removeClass('modal-xl');
        $('#SModal-size').addClass(sizes);
        $('#SModal').modal('show');
    }
</script>