<?php 
require '../../config.php';
session_start();
function rdate($f,$x = '-0 days') { return date($f, strtotime($x, strtotime(date('Y-m-d H:i:s')))); }

load('middleware', 'user');
if ($_SESSION['user']['level'] !== "Admin") {
    header('location:' . base_url('id'));
}
$dt = $db->query("SELECT * FROM akun_mutasi WHERE type = 'GOPAY'")->fetch_assoc();
$pt = $db->query("SELECT * FROM akun_mutasi WHERE type = 'OVO'")->fetch_assoc();
$Gopay = new GojekPay($dt['token']);
$Ovo = new Ovo($pt['token']);
$anu = json_decode($Gopay->getBalance(),true);
$Inii = json_decode($Ovo->getOvoCash(),true);
$njasn = $Inii - 2500;
$gene = json_decode($Ovo->generateTrxId($njasn),true)['trxId'];
$unlock = json_decode($Ovo->unlockAndValidateTrxId($njasn, $gene, $pt['pin']),true);
$inquiry = json_decode($Ovo->transferBankInquiry('009','1490548925',$njasn,'by LynchStore'),true);
if($anu['data'][0]['type'] == "GOPAY_WALLET"){
    $saldo = $anu['data'][0]['balance']['value'];
    $saldooo = $anu['data'][0]['balance']['value'] - 2500;
}
if(isset($_GET['tarik'])){
    $tarik = $_GET['tarik'];
    if($tarik == 'gopay'){
        if(opt_get('YmFuay1jb2Rl') == "BNI"){
            $bank_code = "bni";
        }else if(opt_get('YmFuay1jb2Rl') == "BCA"){
            $bank_code = "bca";
        }else if(opt_get('YmFuay1jb2Rl') == "PERMATA"){
            $bank_code = "bri";
        }else if(opt_get('YmFuay1jb2Rl') == "CIMB"){
            $bank_code = "cimb";
        }else if(opt_get('YmFuay1jb2Rl') == "BRI"){
            $bank_code = "bri";
        }
        $suc = $Gopay->transferBank($bank_code,opt_get('YmFuay1udW1iZXI='),$saldooo,$dt['pin']);
        if($suc == true){
            $_SESSION['alert'] = ['success','Success!','Penarikkan GOPAY Berhasil dilakukan'];
            exit(header('location: /admin/mutasi/'));
         }else{
            $_SESSION['alert'] = ['danger','Failed!','Penarikkan GOPAY Gagal dilakukan'];
            exit(header('location: /admin/mutasi/')); 
         }
    }else if($tarik == 'ovo'){
        if(opt_get('YmFuay1jb2Rl') == "BNI"){
            $bank_code = "009";
        }else if(opt_get('YmFuay1jb2Rl') == "BCA"){
            $bank_code = "014";
        }else if(opt_get('YmFuay1jb2Rl') == "PERMATA"){
            $bank_code = "013";
        }else if(opt_get('YmFuay1jb2Rl') == "CIMB"){
            $bank_code = "022";
        }else if(opt_get('YmFuay1jb2Rl') == "BRI"){
            $bank_code = "002";
        }
        $gene = json_decode($Ovo->generateTrxId($njasn),true)['trxId'];
        $unlock = json_decode($Ovo->unlockAndValidateTrxId($njasn, $gene, $pt['pin']),true);
        $inquiry = json_decode($Ovo->transferBankInquiry($bank_code,opt_get('YmFuay1udW1iZXI='),$njasn,'by Lynch Store'),true);
        if($unlock->isAuthorized == true){
         $suc = $Ovo->transferBankDirect($inquiry['bankCode'], $inquiry['accountNo'], $inquiry['bankName'], $inquiry['accountName'], $gene, $njasn, 'by LynchStore');
         if($suc == true){
            $_SESSION['alert'] = ['success','Success!','Penarikkan OVO Berhasil dilakukan'];
            exit(header('location: /admin/mutasi/'));
         }else{
            $_SESSION['alert'] = ['danger','Failed!','Penarikkan OVO Gagal dilakukan'];
            exit(header('location: /admin/mutasi/')); 
         }
        }
    }else{
        $_SESSION['alert'] = ['danger','Failed!','Penarikkan Gagal dilakukan'];
        exit(header('location: /admin/mutasi/'));
    }
}
include '../../layouts/header-admin.php';
?>
<div class="row justify-content-center">
    <div class="col-md-12 col-12">
        <ul class="nav nav-pills justify-content-left horitab-scroll" id="myTab" role="tablist">
            <li class="nav-item ms-1 nav-tab">
                <a class="nav-link btn-sm btn-category active" data-toggle="pill" style="font-size:12px;" href="#Gopay">GOPAY</a>
            </li>
            <li class="nav-item ms-1 nav-tab">
                <a class="nav-link btn-sm btn-category" data-toggle="pill" style="font-size:12px;" href="#Ovo">OVO</a>
            </li>
            <li class="nav-item ms-1 nav-tab">
                <a class="nav-link btn-sm btn-category" data-toggle="pill" style="font-size:12px;" href="#BCA">BCA</a>
            </li>
        </ul>
<div class="tab-content">
    <div id="Gopay" class="tab-pane active">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title float-left">MUTASI GOPAY</h4>
                <a class="btn btn-success float-right" href="<?=base_url('/admin/mutasi/?tarik=gopay');?>">Tarik Rp <?= $saldo;?></a>
            </div>
            <div class="card-body card-dashboard">
                <div class="table-responsive">
                    <table class="table table-striped table-dark table-bordered table-hover" id="table">
                        <thead>
                        <tr>
                            <th>No</th>
                            <th>Saldo</th>
                            <th>Keterangan</th>
                            <th>Status</th>
                            <th>Tanggal</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        $qqmutasi = mysqli_query($db, "SELECT * FROM mutasi WHERE code='GOPAY' ORDER BY id DESC");
                        $no = 0;
                        while($ffmutasi = mysqli_fetch_assoc($qqmutasi)):
                            $no++
                        ?>
                        <?php if($ffmutasi['date'] !== "" || $ffmutasi['description'] !== ""):?>
                        <tr>
                            <td><?= $no; ?></td>
                            <td>Rp. <?= $ffmutasi['amount']; ?></td>
                            <td><?= $ffmutasi['description'];?></td>
                            <td class="text-center">
                                <?php
                                if ($ffmutasi['status'] == 'Unread') {
                                    $badge_bg = 'warning';
                                } else if ($ffmutasi['status'] == 'Read') {
                                    $badge_bg = 'success';
                                } else {
                                    $badge_bg = 'secondary';
                                }
                                ?>
                                <span class="badge badge-pill bg-<?= $badge_bg; ?>" style="font-size: 11px;"><?= $ffmutasi['status']; ?></span></td>
                                <td><?= $ffmutasi['date']; ?></td>
                                <?php endif?>
                        <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div id="BCA" class="tab-pane fade">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title float-left">MUTASI BCA</h4>
            </div>
            <div class="card-body card-dashboard">
                <div class="table-responsive">
                    <table class="table table-striped table-dark table-bordered table-hover" id="tabel">
                        <thead>
                        <tr>
                            <th>No</th>
                            <th>Saldo</th>
                            <th>Keterangan</th>
                            <th>Status</th>
                            <th>Tanggal</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        $qqmutasi = mysqli_query($db, "SELECT * FROM mutasi WHERE code='BCA' ORDER BY id DESC");
                        $no = 0;
                        while($ffmutasi = mysqli_fetch_assoc($qqmutasi)):
                            $no++
                        ?>
                        <?php if($ffmutasi['date'] !== "" || $ffmutasi['description'] !== ""):?>
                        <tr>
                            <td><?= $no; ?></td>
                            <td>Rp. <?= $ffmutasi['amount']; ?></td>
                            <td><?= $ffmutasi['description'];?></td>
                            <td class="text-center">
                                <?php
                                if ($ffmutasi['status'] == 'Unread') {
                                    $badge_bg = 'warning';
                                } else if ($ffmutasi['status'] == 'Read') {
                                    $badge_bg = 'success';
                                } else {
                                    $badge_bg = 'secondary';
                                }
                                ?>
                                <span class="badge badge-pill bg-<?= $badge_bg; ?>" style="font-size: 11px;"><?= $ffmutasi['status']; ?></span></td>
                                <td><?= $ffmutasi['date']; ?></td>
                                <?php endif?>
                        <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
<div id="Ovo" class="tab-pane fade">
    <div class="card bg-dark shadow">
        <div class="card-header">
            <h4 class="card-title float-left">MUTASI OVO</h4>
            <a class="btn btn-success float-right" href="<?=base_url('/admin/mutasi/?tarik=ovo');?>">Tarik Rp <?= $Inii;?></a>
        </div>
            <div class="card-body card-dashboard">
                <div class="table-responsive">
                    <table class="table table-striped table-dark table-bordered table-hover" id="tbb">
                        <thead>
                        <tr>
                            <th>No</th>
                            <th>Saldo</th>
                            <th>Keterangan</th>
                            <th>Status</th>
                            <th>Tanggal</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php
                        $qmutasi = mysqli_query($db, "SELECT * FROM mutasi WHERE code='OVO' ORDER BY id DESC");
                        $no = 0;
                        while($fmutasi = mysqli_fetch_assoc($qmutasi)): 
                        $no++?>
                        <?php if($fmutasi['date'] == ""):?>
                        <?php else:?>
                        <tr>
                            <td><?= $no; ?></td>
                            <td>Rp. <?= $fmutasi['amount']; ?></td>
                            <td><?= $fmutasi['description'];?></td>
                            <td class="text-center">
                                <?php
                                if ($fmutasi['status'] == 'Unread') {
                                    $badge_bg = 'warning';
                                } else if ($fmutasi['status'] == 'Read') {
                                    $badge_bg = 'success';
                                } else {
                                    $badge_bg = 'secondary';
                                }
                                ?>
                                <span class="badge badge-pill bg-<?= $badge_bg; ?>" style="font-size: 11px;"><?= $fmutasi['status']; ?></span></td>
                            <td><?= $fmutasi['date']; ?></td>
                            <?php endif;?>
                        <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
</div>
<?php include '../../layouts/footer-admin.php'; ?>
<script>
    $(document).ready(function(){
        $("#tbb").dataTable();
    });
    $(document).ready(function(){
        $("#table").dataTable();
    })
    $(document).ready(function(){
        $("#tabel").dataTable();
    })
</script>