<?php

include('config.php');
include('function.php');

date_default_timezone_set('UTC');

define('GX_PATH', realpath(__DIR__.'/'));
define('GX_LIB', GX_PATH.'/lib/');

require 'autoload.php';
new Db();
// $login = login();

// getToken();
// var_dump($login);
$today = date("Y-m-d");
// $reqid = getReqId();
// var_dump($reqid);
// $data = getData($reqid['requestId']);
// $d = $data['view'];
// print_r($data);

foreach($api as $k => $v) {
    echo $k;
    $auth = authToken($v['client_id'], $v['api_key']);
    $data = array(
        'start_date' => urlencode($today),
        'end_date' => urlencode($today),
        'timezone' => "+00:00",
        "date_segment" => "day",
        "include_archived" => 0,
        "limit" => 500
    );
    $res = APIFetch('/statistics/sm', $auth['access_token'], http_build_query($data), "GET");
    $d = $res['data'];
    $d = array_values($d);

    // print_r($d);
    // preg_match_all('/<tr>(.*)<\/tr>/Uis', $d, $m);
    // $n = count($m[1]);
    // echo $n;
    // unset($m[1][0]);
    // unset($m[1][$n-1]);
    // print_r($m);
    Db::delete(array(
        'table' => 'link_report',
        'where' => array(
            'date' => $today,
            'network' => $k
        )
    ));
    $c = count($d);
    echo $c;
    if($c > 1) {
        for ( $i = 0; $i < $c; $i++) {
            // preg_match_all('/<td class="(.*)" >(.*)<\/td>/Uis', $m[1][$i], $m2);
            print_r($m2);
            // exit;
            addPerf($d[$i], $k);

        }
    }
}
