<?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: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

$response = array('status' => 'fail', 'message' => 'Something went wrong Please try again', 'list' => []);

$data = json_decode(file_get_contents("php://input"));
if (isset($data->API_KEY)) {
    $key_ = mysqli_real_escape_string($tcon, $data->API_KEY);
    $key = md5($key_);



    // check API Validity
    $api_n = $dbase->countQuery("SELECT `id` FROM `access_log` WHERE `token`='$key' ");
    if ($api_n == 1) {


        if (isset($data->filter)) {
            $filter = json_decode($data->filter);
        }

        $month_loop = $dbase->queryToWhile_2("SELECT COUNT(*) AS `count`,DATE(lead.added) AS `date`  FROM `lead` WHERE `campaign`='61bafaac3a1bc' OR `campaign`='61ea78a0ec10b' GROUP BY DATE(lead.added) ORDER BY DATE(lead.added) DESC LIMIT 14 ");
        $total = $dbase->queryto_fetch_api("SELECT COUNT(*) AS `count` FROM `lead` WHERE `campaign`='617947af72363' OR `com`='161' ");
        $open = $dbase->queryto_fetch_api("SELECT COUNT(*) AS `count` FROM `lead` WHERE `status`='O' AND (`campaign`='61bafaac3a1bc' OR `campaign`='61ea78a0ec10b' ) ");
        $failed = $dbase->queryto_fetch_api("SELECT COUNT(*) AS `count` FROM `lead` WHERE `status`='F' AND (`campaign`='61bafaac3a1bc' OR `campaign`='61ea78a0ec10b' ) ");
        $una = $dbase->queryto_fetch_api("SELECT COUNT(*) AS `count` FROM `lead` WHERE ( `status`='U' OR `status`='UR' OR `status`='S' ) AND (`campaign`='61bafaac3a1bc' OR `campaign`='61ea78a0ec10b' ) ");
        $verified = $dbase->queryto_fetch_api("SELECT COUNT(*) AS `count` FROM `lead` WHERE `campaign`='617947af72363' AND ( `verify`='POSITIVE' OR `verify`='PROS' OR `verify`='MEETING' ) OR `com`='161' ");


        $src_while_main = $dbase->querytoWhile("SELECT COUNT(*) AS `typeSum` ,_source.name,_source.short,_source.img FROM lead LEFT JOIN _source ON lead.source=_source.short WHERE `campaign`='617947af72363' OR `com`='161' GROUP BY lead.source ORDER BY `typeSum` DESC ");
        $src_whole_array = [];
        foreach (array_filter($src_while_main) as $src_ech_key => $src_ech_val) {
            $short = $src_ech_val['short'];
            $src_O_leads = $dbase->queryto_fetch_api("SELECT COUNT(*) AS `total` FROM `lead` WHERE lead.status='O' AND lead.source='$short' AND (`campaign`='61bafaac3a1bc' OR `campaign`='61ea78a0ec10b' ) ");
            $src_F_leads = $dbase->queryto_fetch_api("SELECT COUNT(*) AS `total` FROM `lead` WHERE lead.status='F' AND lead.source='$short' AND (`campaign`='61bafaac3a1bc' OR `campaign`='61ea78a0ec10b' ) ");
            $src_U_leads = $dbase->queryto_fetch_api("SELECT COUNT(*) AS `total` FROM `lead` WHERE ( lead.status='U' OR lead.status='UR' OR lead.status='S' ) AND lead.source='$short' AND (`campaign`='61bafaac3a1bc' OR `campaign`='61ea78a0ec10b' )  ");
            $src_month_each = array('typeSum' => $src_ech_val['typeSum'], 'img' => $src_ech_val['img'],'name' => $src_ech_val['name'], 'short' => $src_ech_val['short'], 'closed' => $src_clo_leads['total'],
            'open'=>$src_O_leads['total'],'failed'=>$src_F_leads['total'],'un'=>$src_U_leads['total']
            );
            array_push( $src_whole_array,$src_month_each);
        }



        $response = array('month_loop' => array_filter($month_loop), 'status' => 'success', 'src_loop' => array_filter($src_whole_array),'total'=>$total['count'],'open'=>$open['count'],'failed'=>$failed['count'],'un'=>$una['count'],'verified'=>$verified['count']);
    }
}
echo json_encode(array_filter($response));
