<?php
require("dbconnect_mysqli.php");
require("functions.php");
$data = array();
$stmt = "SELECT val.lead_id, val.campaign_id,vc.campaign_name,full_name, CASE WHEN (vls.list_description IS NULL OR vls.list_description = '') AND vls.list_name IS NOT NULL AND vls.list_name != '' THEN NULL ELSE vls.list_name END AS list_name, vls.list_id, vls.list_description, vl.phone_number, vl.vendor_lead_code, vu.user,sub_status,vu.user_group,dispo_sec, ifnull(val.dispo_sec + val.dead_sec, 0) as PreviewTime, CASE WHEN sub_status = 'LOGIN' THEN 0 WHEN sub_status is null THEN 0 ELSE pause_sec END as pause_sec, pause_sec as pause_sec2, wait_sec + talk_sec + dispo_sec + pause_sec as loginWbrk, wait_sec + talk_sec + dispo_sec as loginWoutbrk, CASE WHEN sub_status = 'LOGIN' THEN pause_sec WHEN sub_status != 'LOGIN' or sub_status is not null or sub_status != 'LAGGED' THEN 0 ELSE pause_sec END as pause_sec_login, talk_sec, wait_sec, CASE WHEN val.status is not null THEN 1 ELSE 0 END as count, dead_sec, event_time, CASE WHEN sub_status = 'LAGGED' THEN pause_sec ELSE 0 END as lagged, val.status, CASE WHEN vcs.status_name IS NOT NULL THEN vcs.status_name ELSE vs.status_name END AS status_name, CASE WHEN (vls.list_description IS NULL OR vls.list_description = '') AND vls.list_name IS NOT NULL AND vls.list_name != '' THEN vls.list_name WHEN sub_status IS NOT NULL AND sub_status != '' THEN sub_status WHEN (vul.event IS NULL OR vul.event = '') AND vls.list_description IS NULL OR vls.list_description = '' THEN 'AGENT SCREEN CLOSED | ON PREVIEW MODE' ELSE vul.event END as sub_status2, val.uniqueid,vul.event FROM vicidial_agent_log val INNER JOIN vicidial_users vu ON vu.user = val.user INNER JOIN vicidial_campaigns vc ON vc.campaign_id = val.campaign_id LEFT JOIN vicidial_campaign_statuses vcs ON vcs.campaign_id = val.campaign_id AND vcs.status = val.status LEFT JOIN vicidial_statuses vs ON vs.status = val.status LEFT JOIN vicidial_list vl ON vl.lead_id = val.lead_id LEFT JOIN vicidial_lists vls ON vls.list_id = vl.list_id LEFT JOIN vicidial_user_log vul ON vul.campaign_id = val.campaign_id AND vul.user = val.user AND vul.user_group = val.user_group AND (vul.event_date = DATE_ADD(val.event_time, INTERVAL pause_sec second) OR vul.event_date = val.event_time) AND val.lead_id IS NULL where event_time BETWEEN '2022-11-25 00:00:00' AND '2022-11-25 23:59:59' AND pause_sec<65000 and val.campaign_id not in ('blaster') AND vu.user = '201' group by val.agent_log_id order by event_time, user,full_name,sub_status;";
$rslt = mysql_to_mysqli($stmt, $link);
$i = 0;
while($row2 = mysqli_fetch_assoc($rslt))
{
	$temp_result_arr = array(
						'id' => $i,
						'campaign_id' => $row2['campaign_id'],
						'campaign_name' => $row2['campaign_name'],
						'lead_id' => $row2['lead_id'],
						'calldate' => $row2['event_time'],
						'agent_login_time' => gmdate("H:i:s", $row2['loginWbrk']),
						'agent_talk_time' => gmdate("H:i:s", $row2['talk_sec'] - $row2['dead_sec']),
						'agent_wrap_time' => gmdate("H:i:s", $row2['dispo_sec'] + $row2['dead_sec']),
						'agent_break_time' => gmdate("H:i:s", $row2['pause_sec']),
						'agent_ring_time' => gmdate("H:i:s", $row2['wait_sec']),
						'agent_pause_time' => gmdate("H:i:s", $row2['pause_sec2'] - $row2['pause_sec']),

						'list_id' => $row2['list_id'],
						'list_description' => $row2['list_description'],
						'phone_number' => $row2['phone_number'],
						'vendor_lead_code' => $row2['vendor_lead_code'],
						'agent_id' => $row2['user'],
						'status' => $row2['status'],
						'status_name' => $row2['status_name'],
						'project_name' => $row2['list_name'],
						'agent_name' => $row2['full_name'],
					  'sub_status' => ($pause_codes[$row2['sub_status2']]) ? $row2['sub_status2'] : 'ONCALL' ,
		    	);
					array_push($data, $temp_result_arr);
	$i++;
}

echo "<pre>";
print_r($data);

?>