<?php
header('Content-Type: application/json');
require '../config.php';
if(isset($_POST['key']) && isset($_POST['username']) && isset($_POST['password']) && isset($_POST['from']) && isset($_POST['to']) && isset($_POST['sign'])) {
    $key = $_POST['key'];
    $us = $_POST['username'];
    $pass = $_POST['password'];
    $from = $_POST['from'];
    $to = $_POST['to'];
    $sign = $_POST['sign'];
    $dates = date('Y-m-d H:i:s');
    $s_api = $db->query("SELECT * FROM users WHERE api_key = '$key'");
    if($s_api->num_rows == 0) die(json_encode(['result' => false,'message' => 'API Key not registered.'], JSON_PRETTY_PRINT));
    $r_api = $s_api->fetch_assoc();
    $username = $r_api['username'];
    $data_user = $username;
    if($sign <> md5($r_api['api_id'].$r_api['api_key'])) die(json_encode(['result' => false,'message' => 'Signature Not Valid.']));
    if($data_user == false) die(json_encode(['result' => false,'message' => 'User not found.']));
    if(check_ip($client_ip, $r_api['ip_static']) == false) die(json_encode(['result' => false,'message' => 'IP Anda '.$client_ip.' Tidak Dikenali'], JSON_PRETTY_PRINT));
    $exp = $r_api['expired'];
    if($dates > $exp) die(json_encode(['result' => false,'message' => 'Sudah Expired Silahkan Hubungi Admin']));
        $bca = new BCA($us,$pass);
        $mbca = $bca->getListTransaksi($from,$to);
        for($i = 0; $i <= count($mbca['data']); $i++){
        $data = $mbca['data'][$i];
        $date = $data['date'];
        $desk = $data['desk'];
        $amount = $data['amount'];
        $p = str_replace(',','',$amount);
        $saldo = (int)$p;
        $flow = $data['flows'];
            if($date !== null || $desk  !== null || !$amount !== "0" || !$flow !== null){
                $dat[] = [
                    'date' => $date,
                    'description' => $desk,
                    'amount' => $saldo,
                    'type' => $flow
                 ];
            }
        }
    die(json_encode(['result' => true,'data' => $dat,'message' => 'Mutasi BCA Berhasil Didapatkan'], JSON_PRETTY_PRINT));
} else {
    die(json_encode(['result' => false,'message' => 'Request not detected.'], JSON_PRETTY_PRINT));
}