<?php 
require '../../config.php';
$data_bca = $db->query("SELECT * FROM akun_mutasi WHERE type = 'BCA'") 
->fetch_assoc();
$gopay = new GojekPay();
$data_gopay = $db->query("SELECT * FROM akun_mutasi WHERE type = 'GOPAY'") 
->fetch_assoc();
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'));
}
if (isset($_POST['otp-gopay']) OR isset($_POST['phone-gopay']) OR isset($_POST['pin-gopay'])) {
    if(isset($_POST['send_otp_gopay'])){
    $post_phone = $_POST['phone-gopay'];
    $device = json_decode($gopay->loginRequest($post_phone), true)['data']['otp_token'];
    
    if($device == true){
        $update  = $db->query("UPDATE akun_mutasi SET device = '$device' , nomor = '$post_phone' WHERE type = 'GOPAY'");
        if($update == true){
            $_SESSION['alert'] = ['success', 'Success!', 'Berhasil Update Nomor GOPAY'];
            exit(header('location: /admin/mutasi/account'));
        }else {
            $_SESSION['alert'] = ['danger', 'Failed!', 'Gagal Update Nomor GOPAY'];
            exit(header('location: /admin/mutasi/account'));
        }
    } else {
        $_SESSION['alert'] = ['danger', 'Failed!', 'Gagal Mengirim OTP'];
        exit(header('location: /admin/mutasi/account'));
    }
    
    }else if(isset($_POST['confirm_otp_gopay'])){
        $post_otp = $_POST['otp-gopay'];
        $verif = json_decode($gopay->getAuthToken($data_gopay['device'],$post_otp), true)['access_token'];
    
        if($verif == true){
        $update = $db->query("UPDATE akun_mutasi SET otp = '$post_otp', token = '$verif' WHERE type = 'GOPAY'");
        if($update ==  true){
            $_SESSION['alert'] = ['success', 'Success!', 'Berhasil Update Akun Gopay'];
            exit(header('location: /admin/mutasi/account'));
        }else {
            $_SESSION['alert'] = ['danger', 'Failed!', 'Gagal Update Akun Gopay'];
            exit(header('location: /admin/mutasi/account'));
        }
        }else {
            $_SESSION['alert'] = ['danger', 'Failed!', 'Gagal Mendapatkan Token'];
            exit(header('location: /admin/mutasi/account'));
        }
    }else if(isset($_POST['auth_pin'])){
    $post_pins = $_POST['pin-gopay'];
        $db->query("UPDATE akun_mutasi SET pin = '$post_pins' WHERE type = 'GOPAY'");
            $_SESSION['alert'] = ['success', 'Success!', 'Berhasil Update Pin GOPAY'];
            exit(header('location: /admin/mutasi/account'));
            
    }else{
            $_SESSION['alert'] = ['danger', 'Failed!', 'Gagal Update Pin GOPAY'];
            exit(header('location: /admin/mutasi/account'));
    }
}
$OVO = new Ovo();
$data_ovo = $db->query("SELECT * FROM akun_mutasi WHERE type = 'OVO'") 
->fetch_assoc();
if (isset($_POST['otp']) OR isset($_POST['phone']) OR isset($_POST['pin'])) {
if(isset($_POST['send_otp'])){
    $post_phone1 = $_POST['phone'];
    $device = json_decode($OVO->sendOtp($post_phone1), true)['data']['otp']['otp_ref_id'];
    if($device == true){
        $update  = $db->query("UPDATE akun_mutasi SET device = '$device' , nomor = '$post_phone1' WHERE type = 'OVO'");
        if($update == true){
            $_SESSION['alert'] = ['success', 'Success!', 'Sukses Update Nomor OVO'];
            exit(header('location: /admin/mutasi/account'));
        }else {
            $_SESSION['alert'] = ['danger', 'Failed!', 'Gagal Update Nomor OVO'];
            exit(header('location: /admin/mutasi/account'));
        }
    } else {
            $_SESSION['alert'] = ['danger', 'Failed!', 'Gagal Mengirim OTP'];
            exit(header('location: /admin/mutasi/account'));
    }
    
}else if(isset($_POST['confirm_otp'])){
    $post_otp = $_POST['otp'];
    $verif = json_decode($OVO->OTPVerify($data_ovo['nomor'],$data_ovo['device'],$post_otp), true)['data']['otp']['otp_token'];
    
    if($verif == true){
        $update = $db->query("UPDATE akun_mutasi SET otp = '$post_otp', token = '$verif' WHERE type = 'OVO'");
        if($update ==  true){
            $_SESSION['alert'] = ['success', 'Success!', 'Sukses Update OTP'];
            exit(header('location: /admin/mutasi/account'));
        }else {
            $_SESSION['alert'] = ['danger', 'Failed!', 'Gagal Update OTP'];
            exit(header('location: /admin/mutasi/account'));
        }
    }else {
            $_SESSION['alert'] = ['danger', 'Failed!', 'Gagal Mengirim Token'];
            exit(header('location: /admin/mutasi/account'));
    }
    
}else if(isset($_POST['auth_otp'])){
    $post_pin = $_POST['pin'];
    $akses = json_decode($OVO->getAuthToken($data_ovo['nomor'],$data_ovo['device'],$data_ovo['token'],$post_pin), true)['data']['auth'];
    
    if($akses['access_token'] == true){
        $db->query("UPDATE akun_mutasi SET pin = '$post_pin', token = '".$akses['access_token']."' WHERE type = 'OVO'");
            $_SESSION['alert'] = ['success', 'Success!', 'Berhasil Update Akun OVO'];
            exit(header('location: /admin/mutasi/account'));
    }else{
            $_SESSION['alert'] = ['danger', 'Failed!', 'Gagal Update Akun OVO'];
            exit(header('location: /admin/mutasi/account'));
    }
}
}
if(isset($_POST['tombol_bca'])){
    $user = $_POST['user'];
    $pin = $_POST['pin'];
    if(!$pin || !$user){
        $_SESSION['alert'] = ['danger','Failed','Masih Ada Form Yang Kosong'];
        exit(header('location: /admin/mutasi/account'));
    }else{
        $check = $db->query("UPDATE akun_mutasi SET nomor='$user',pin='$pin' WHERE type='BCA'");
        if($check == TRUE){
            $_SESSION['alert'] = ['success','Success','Berhasil Update Akun BCA'];
            exit(header('location: /admin/mutasi/account'));
        }
    }
}
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">Akun GOPAY</h4>
            </div>
            <div class="card-body card-dashboard">
                <form method="POST">
                    <div class="col-12 col-sm-12">
                        <div class="form-group mb-3">
                            <label>Phone</label>
                            <div class="input-group search-bar">
                                <input type="text" name="phone-gopay" class="form-control" value="<?= $data_gopay['nomor'];?>" placeholder="08xxxxxxx">
                                <button type="submit" name="send_otp_gopay" class="btn btn-primary waves-effect waves-light">
                                    Kirim OTP
                                </button>
                            </div>
                        </div>
                    </div>
                    <div class="col-12 col-sm-12">
                        <div class="form-group mb-3">
                            <label>OTP</label>
                            <div class="input-group search-bar">
                                <input type="text" name="otp-gopay" class="form-control" value="<?= $data_gopay['otp'];?>" placeholder="2021">
                                <button type="submit" name="confirm_otp_gopay" class="btn btn-primary waves-effect waves-light">
                                        Verif OTP
                                </button>
                            </div>
                        </div>
                    </div>
                    <div class="col-12 col-sm-12">
                        <div class="form-group mb-3">
                            <label>PIN</label>
                            <div class="input-group search-bar">
                                <input type="text" name="pin-gopay" class="form-control" value="<?= $data_gopay['pin']; ?>" placeholder="123456">
                                <button type="submit" name="auth_pin" class="btn btn-primary waves-effect waves-light">
                                    Simpan PIN
                                </button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div id="OVO" class="tab-pane fade">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">Akun OVO</h4>
            </div>
            <div class="card-body card-dashboard">
                <form action="" method="POST">
                    <div class="col-12 col-sm-12">
                        <div class="form-group mb-3">
                            <label>Phone</label>
                            
                            <div class="input-group search-bar">
                                <input type="text" name="phone" class="form-control" value="<?= $data_ovo['nomor'];?>" placeholder="628xxxxxxx">
                                <button type="submit" name="send_otp" class="btn btn-primary waves-effect waves-light">
                                    Kirim OTP
                                </button>
                            </div>
                        </div>
                    </div><div class="col-12 col-sm-12">
                        <div class="form-group mb-3">
                            <label>OTP</label>
                            <div class="input-group search-bar">
                                <input type="text" name="otp" class="form-control" value="<?= $data_ovo['otp'];?>" placeholder="2021">
                                <button type="submit" name="confirm_otp" class="btn btn-primary waves-effect waves-light">
                                    Verif OTP
                                </button>
                            </div>
                        </div>
                    </div>
                    <div class="col-12 col-sm-12">
                        <div class="form-group mb-3">
                            <label>PIN</label>
                            <div class="input-group search-bar">
                                <input type="text" name="pin" class="form-control" value="<?= $data_ovo['pin']; ?>" placeholder="123456">
                                <button type="submit" name="auth_otp" class="btn btn-primary waves-effect waves-light">
                                    Simpan PIN
                                </button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div id="BCA" class="tab-pane fade">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title">Akun BCA</h4>
            </div>
            <div class="card-body card-dashboard">
                <form action="" method="POST">
                    <div class="col-12 col-sm-12">
                        <div class="form-group mb-3">
                            <label>Username</label>
                                <input type="text" name="user" class="form-control" value="<?= $data_bca['nomor'];?>" placeholder="LYNCHSTORE2321">
                        </div>
                    </div>
                    <div class="col-12 col-sm-12">
                        <div class="form-group mb-3">
                            <label>Pin</label>
                                <input type="text" name="pin" class="form-control" value="<?= $data_bca['pin']; ?>" placeholder="123456">
                        </div>
                    </div>
                    <div class="float-right">
                        <button type="submit" name="tombol_bca" class="btn btn-primary waves-effect waves-light">Simpan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div><br>
<?php include '../../layouts/footer-admin.php'; ?>