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

if ($_SESSION['user']['level'] !== "Admin") {
    header('location:' . base_url('id'));
}
if(isset($_POST['tombol_tripay'])){
    $key = $_POST['api_key'];
    $id = $_POST['api_id'];
    $merchant = $_POST['merchant_id'];
    if(empty($merchant) OR empty($id) OR empty($key)){
        $_SESSION['alert'] = ['danger','Failed!','Masih Ada Form Yang Kosong'];
        exit(header("Location: ".base_url('/admin/setting/payment')));
    }else{
        $db->query("UPDATE payment SET merchant_id='$merchant', api_id='$id', api_key='$key' WHERE code='Tripay'");
        $_SESSION['alert'] = ['success','Success','Tripay Berhasil Diperbarui'];
        exit(header("Location: ".base_url('/admin/setting/payment')));

    }
}
if(isset($_POST['tombol_mutasi'])){
    $key = $_POST['api_key'];
    $sign = $_POST['api_sign'];
    if(empty($key)){
        $_SESSION['alert'] = ['danger','Failed!','Masih Ada Form Yang Kosong'];
        exit(header("Location: ".base_url('/admin/setting/payment')));
    }else{
        $db->query("UPDATE payment SET api_key='$key',api_id='$sign' WHERE code='Mutasi'");
        $_SESSION['alert'] = ['success','Success','Mutasi.co.id Berhasil Diperbarui'];
        exit(header("Location: ".base_url('/admin/setting/payment')));
    }
}
if(isset($_POST['tombol_xendit'])){
    $key = $_POST['api_key'];
    $callback = $_POST['callback'];
    if(empty($key)){
        $_SESSION['alert'] = ['danger','Failed!','Masih Ada Form Yang Kosong'];
        exit(header("Location: ".base_url('/admin/setting/payment')));
    }else{
        $db->query("UPDATE payment SET api_key='$key', api_id='$callback' WHERE code='Xendit'");
        $_SESSION['alert'] = ['success','Success','Xendit Berhasil Diperbarui'];
        exit(header("Location: ".base_url('/admin/setting/payment')));
    }
}
if(isset($_POST['tombol_dana'])){
    opt_update('cHVzaC1kYW5h', $_POST['push-dana']);
    opt_update('cHVzaC1kYW5hLXRleHQ=', $_POST['push-dana-text']);
    opt_update('cHVzaC1kYW5hLWtleXM=', $_POST['push-dana-keys']);
    $_SESSION['alert'] = ['success', 'Success!', 'Data berhasil di simpan.'];
    exit(header("Location: ".base_url('/admin/setting/payment')));
}
$tripay = $db->query("SELECT * FROM payment WHERE code='Tripay'")->fetch_assoc();
$mutasi = $db->query("SELECT * FROM payment WHERE code='Mutasi'")->fetch_assoc();
$Xendit = $db->query("SELECT * FROM payment WHERE code='Xendit'")->fetch_assoc();
require '../../layouts/header-admin.php';?>
<div class="row justify-content-center">
    <div class="col-12">
        <ul class="nav nav-pills horitab-scroll" id="myTab" role="tablist">
            <li class="nav-item ms-1 nav-tab">
                <a class="nav-link btn-sm btn-category  active" style="font-size:12px;" data-toggle="pill" href="#Tripay">Tripay</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="#Mutasi">CekMutasi</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="#Xendit">Xendit</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="#Dana">DANA</a>
            </li>
        </ul>
        <div class="tab-content">
            <div id="Tripay" class="tab-pane active">
                <div class="card bg-dark shadow">
                    <div class="card-header">
                        <h4 class="card-title">Tripay</h4>
                    </div>
                    <div class="card-body">
                        <form action="" method="POST" enctype="multipart/form-data">
                            <div class="form-group mb-3">
                                <label class="mb-1">Merchant ID</label>
                                    <input type="text" class="form-control" name="merchant_id" placeholder="LS91222" value="<?= $tripay['merchant_id'];?>">
                                </div>
                                <div class="form-group mb-3">
                                    <label class="mb-1">Api Key</label>
                                        <input type="text" class="form-control" name="api_key" placeholder="" value="<?= $tripay['api_key'];?>">
                                </div>
                                <div class="form-group mb-3">
                                    <label class="mb-1">Private Key</label>
                                    <input type="text" class="form-control" name="api_id" placeholder="" value="<?= $tripay['api_id'];?>">
                                </div>
                                <div class="text-right">
                                    <button class="btn btn-light" type="reset">Batal</button>
                                    <button class="btn btn-primary" type="submit" name="tombol_tripay">Tambah</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
                <div id="Mutasi" class="tab-pane fade">
                <div class="card bg-dark shadow">
                    <div class="card-header">
                        <h4 class="card-title">CekMutasi</h4>
                    </div>
                    <div class="card-body">
                        <form action="" method="POST" enctype="multipart/form-data">
                                <div class="form-group mb-3">
                                    <label class="mb-1">Api Key</label>
                                        <input type="text" class="form-control" name="api_key" placeholder="wcode.id" value="<?= $mutasi['api_key'];?>">
                                </div>
                                <div class="form-group mb-3">
                                    <label class="mb-1">Api Signature</label>
                                        <input type="text" class="form-control" name="api_sign" placeholder="wcode.id" value="<?= $mutasi['api_id'];?>">
                                </div>
                                <div class="text-right">
                                    <button class="btn btn-light" type="reset">Batal</button>
                                    <button class="btn btn-primary" type="submit" name="tombol_mutasi">Tambah</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
                <div id="Xendit" class="tab-pane fade">
                <div class="card bg-dark shadow">
                    <div class="card-header">
                        <h4 class="card-title">Xendit</h4>
                    </div>
                    <div class="card-body">
                        <form action="" method="POST" enctype="multipart/form-data">
                                <div class="form-group mb-3">
                                    <label class="mb-1">Api Key</label>
                                        <input type="text" class="form-control" name="api_key" placeholder="wcode.id" value="<?= $Xendit['api_key'];?>">
                                </div>
                                <div class="form-group mb-3">
                                    <label class="mb-1">Api Callback</label>
                                        <input type="text" class="form-control" name="callback" placeholder="wcode.id" value="<?= $Xendit['api_id'];?>">
                                </div>
                                <div class="text-right">
                                    <button class="btn btn-light" type="reset">Batal</button>
                                    <button class="btn btn-primary" type="submit" name="tombol_Xendit">Tambah</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
                <div id="Dana" class="tab-pane fade">
                <div class="card bg-dark shadow">
                    <div class="card-header">
                        <h4 class="card-title">PUSH NOTIFICATION DANA</h4>
                    </div>
                    <div class="card-body">
                        <form action="" method="POST" enctype="multipart/form-data">
                                <div class="form-group mb-3">
                                    <label class="mb-1">PKG Text</label>
                                        <input type="text" class="form-control" name="push-dana" placeholder="wcode.id" value="<?= opt_get('cHVzaC1kYW5h'); ?>">
                                </div>
                                <div class="form-group mb-3">
                                    <label class="mb-1">Text</label>
                                        <input type="text" class="form-control" name="push-dana-text" placeholder="wcode.id" value="<?= opt_get('cHVzaC1kYW5hLXRleHQ='); ?>">
                                </div>
                                <div class="form-group mb-3">
                                    <label class="mb-1">Keys Private</label>
                                        <input type="text" class="form-control" name="push-dana-keys" placeholder="wcode.id" value="<?= opt_get('cHVzaC1kYW5hLWtleXM='); ?>">
                                </div>
                                <div class="text-right">
                                    <button class="btn btn-light" type="reset">Batal</button>
                                    <button class="btn btn-primary" type="submit" name="tombol_dana">Tambah</button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php require '../../layouts/footer-admin.php';?>