<?php
include "../db/dBase.php";
$dbase = new dBase();
$tcon = $dbase->con;

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");


$split = array('result'=>'FAIL');

if (isset($_GET['apikey'])) {
    $key_ = mysqli_real_escape_string($tcon, $_GET['apikey']);
    $key = md5($key_);
    // check API Validity
    $api_n = $dbase->countQuery("SELECT `id` FROM `access_log` WHERE `token`='$key' ");
    if ($api_n == 1) {

        $api_row = $dbase->queryto_fetch_api("SELECT access_log.userID,user.name,user.dp FROM `access_log` LEFT JOIN `user` ON access_log.userID= user.userID WHERE `token`='$key' ");
        $cUser=$api_row['userID'];
       
        // Notification Count
       
        $not = $dbase->queryto_fetch_api("SELECT COUNT(*) AS `count` FROM `notification` WHERE ( `not_to`='agent' OR `not_to`= '".$cUser."' ) AND `status`=0 ");
        $crone_while=$dbase->querytoWhile_2("SELECT DATE_FORMAT(`dated`,'%d-%M-%y %h:%i: %p') AS `dated`,`name` FROM `crone_log` ");


        $user_det=array('dp'=>$api_row['dp'],'name'=>$api_row['name']);

        $split = array(
            'result'=>'DONE',
            'USER_DET' =>  $user_det,
            'nc'=>$not['count'],
            'crones'=>array_filter($crone_while)
        );
    }
}
echo $json = json_encode(array_filter($split));