<?php
// required headers
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");

include_once '../config/database.php';
include_once '../objects/sales.php';

session_start();
$database = new Database();
$db = $database->getConnection();
 
$sales = new Sales($db);

$data = json_decode(file_get_contents("php://input"));


$sales->city = $data->city;
$sales->df = $data->df;
$sales->dt = $data->dt;
$sales->ym = $data->ym;
$stmt = $sales->searchbyMbps();
$num = $stmt->rowCount();

$arr = array();
$arr["records"] = array();
$M1MF=$M2MF=$M3MF=$M4MF=$M5MF=$M6MF=$M7MF=$M8MF=$M9MF=$M10MF=$M11MF=$M12MF=$totalF=0;
if($num>0){
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
        $detail=array( 
            "mbps" => ($mbps)?$mbps:"",
            "M1M" => ($M1M)?$M1M:"",
            "M2M" => ($M2M)?$M2M:"",
            "M3M" => ($M3M)?$M3M:"",
            "M4M" => ($M4M)?$M4M:"",
            "M5M" => ($M5M)?$M5M:"",
            "M6M" => ($M6M)?$M6M:"",
            "M7M" => ($M7M)?$M7M:"",
            "M8M" => ($M8M)?$M8M:"",
            "M9M" => ($M9M)?$M9M:"",
            "M10M" =>($M10M)?$M10M:"",
            "M11M" =>($M11M)?$M11M:"",
            "M12M" =>($M12M)?$M12M:"",
            "total" => number_format($total)
        );  
        $M1MF += (int)$M1M ;
        $M2MF += (int)$M2M ;
        $M3MF += (int)$M3M ;
        $M4MF += (int)$M4M ;
        $M5MF += (int)$M5M ;
        $M6MF += (int)$M6M ;
        $M7MF += (int)$M7M ;
        $M8MF += (int)$M8M ;
        $M9MF += (int)$M9M ;
        $M10MF += (int)$M10M ;
        $M11MF += (int)$M11M ;
        $M12MF += (int)$M12M ;
        $totalF += (int)$total ;
        array_push($arr["records"], $detail);
    }
}
$arr["M1MF"] = number_format($M1MF);
$arr["M2MF"] = number_format($M2MF);
$arr["M3MF"] = number_format($M3MF);
$arr["M4MF"] = number_format($M4MF);
$arr["M5MF"] = number_format($M5MF);
$arr["M6MF"] = number_format($M6MF);
$arr["M7MF"] = number_format($M7MF);
$arr["M8MF"] = number_format($M8MF);
$arr["M9MF"] = number_format($M9MF);
$arr["M10MF"] = number_format($M10MF);
$arr["M11MF"] = number_format($M11MF);
$arr["M12MF"] = number_format($M12MF);
$arr["totalF"] = number_format($totalF);
echo json_encode($arr);
?>