<?php
require("../../inc/lib.inc");

global $OMC_server;
$sysTypeNo=$_GET['sysTypeNo'];
$pssDbConn=mysqli_connect($OMC_server[0]['host'],$OMC_server[0]['user'],$OMC_server[0]['password'], 'PPS_DB');
$objDbConn=mysqli_connect($OMC_server[0]['host'],$OMC_server[0]['user'],$OMC_server[0]['password'], 'OBJ_'.$sysTypeNo);
// $page_id=$_GET['page_id'];
// $sel_date_from=$_GET['sel_date_from'];
// $sel_date_to=$_GET['sel_date_to'];
// $show_type=$_GET['show_type'];

$query_year=$_REQUEST['query_year'];
$query_month=$_REQUEST['query_month'];
$query_day=$_REQUEST['query_day'];


if(!$query_year){
	$query_year=date("Y",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
	$query_month=date("m",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
	$query_day=date("d",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
}
$defaultDate="$query_year-$query_month-$query_day";
$selectedDate=$_REQUEST['selectedDate']?$_REQUEST['selectedDate']:$defaultDate;

    switch($sysTypeNo){      
        case '360':

            $sumSql="SELECT
                ussd_code_1,
                session_num_1,
                session_duration_1,
                error_timeout_1,
                error_system_1,
                error_message_1,
                ussd_code_2,
                session_num_2,
                session_duration_2,
                error_timeout_2,
                error_system_2,
                error_message_2,
                ussd_code_3,
                session_num_3,
                session_duration_3,
                error_timeout_3,
                error_system_3,
                error_message_3,
                ussd_code_4,
                session_num_4,
                session_duration_4,
                error_timeout_4,
                error_system_4,
                error_message_4,
                ussd_code_5,
                session_num_5,
                session_duration_5,
                error_timeout_5,
                error_system_5,
                error_message_5,
                ussd_code_6,
                session_num_6,
                session_duration_6,
                error_timeout_6,
                error_system_6,
                error_message_6,
                ussd_code_7,
                session_num_7,
                session_duration_7,
                error_timeout_7,
                error_system_7,
                error_message_7,
                ussd_code_8,
                session_num_8,
                session_duration_8,
                error_timeout_8,
                error_system_8,
                error_message_8 
             FROM
                PPS_DB.daily_report 
             WHERE date = '$selectedDate' ";
        //echo "sumSql=$sumSql";     
        $res=mysqli_query($pssDbConn,$sumSql);
        echo mysqli_error($pssDbConn);
        $num=mysqli_num_rows($res);

        // 检查是否有记录
        if ($num > 0) {
            // 读取第一条记录
            $row = mysqli_fetch_array($res, MYSQLI_ASSOC);
            
            // 处理标题
            $title = ['Index','USSD Code','Session Num','Session Duration','Error Timeout','Error System','Error Message','Average Duration','TPS']; // 获取字段名
            $file_name = 'PPS-USSD.csv';
            $file = fopen($file_name, 'w');
            fputcsv($file, $title); // 写入标题行

            for ($i = 1; $i <= 8; $i++) {
                $average=$row['session_num_' . $i]?$row['session_duration_' . $i]/$row['session_num_' . $i]:0;
                $tps=$row['session_num_' . $i]/(3600*24);
                  fputcsv($file, [$i,$row['ussd_code_' . $i],$row['session_num_' . $i],$row['session_duration_' . $i],$row['error_timeout_' . $i],$row['error_system_' . $i],$row['error_message_' . $i],$average,$tps]);
              }

            fclose($file);
             // 检查文件是否存在
        if (file_exists($file_name)) {
            header("Content-type: application/csv");
            //use for download csv in https
            header('Expires: 0'); 
            header('Pragma: public'); 
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
            header('Cache-Control: public'); 
            header("Content-Disposition: attachment; filename=$file_name");
            readfile($file_name);
        } else {
            // 文件不存在时的处理
            echo "Error: File does not exist.";
        }
        }
        break;
    default:
	    break;
    }
    
?>