<?php
    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/sms_type.php';
	include_once '../objects/sales.php';
	include_once '../objects/message.php';
	include_once '../objects/sms_log.php';
	// include_once '../objects/payment.php';

	date_default_timezone_set('Asia/Rangoon'); 
	session_start();
	$database = new Database();
	$db = $database->getConnection();
	 
	$sms_type = new SMSType($db);
	$sales = new Sales($db);
	$message = new Message($db);

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

	$arr=array();

	$arr["detail"]=array();
	if($data->sms_type=="expired"){

		$msg_detail = $message->getMsg("expired");
		$sales->d = date('Y-m-d',(strtotime ( '+3 day' , strtotime ( $data->date ) ) ));
		$stmt = $sales->getExpiredSMSList($data->city);
		$num = $stmt->rowCount();
	
		if($num>0){
			while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
				extract($row);
				$healthy = ["[ID]", "[Plan]", "[Start_Date]", "[End_Date]", "[Amount]", "[Hotline]"];
				$yummy   = [$id_no, $mbps."MB ".$days."Days", $start_date, $end_date, number_format($payment), $ph_no];

					$exp=array(
						"id" => (int)$id,
						"msg" => nl2br(str_replace($healthy, $yummy, $msg_detail)),
                        "phone" => $phone
					);
					array_push($arr["detail"], $exp);
			}
		}
	}else if($data->sms_type=="reminder"){

		$msg_detail = $message->getMsg("reminder");
		$sales->d = date('Y-m-d',(strtotime ( '0 day' , strtotime ( $data->date ) ) ));
		$stmt = $sales->getReminderSMSList($data->city);
		$num = $stmt->rowCount();

		if($num>0){
			while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
				extract($row);

				$healthy = ["[ID]", "[Plan]", "[Start_Date]", "[End_Date]", "[Amount]", "[Today_Date]","[Hotline]"];
				$yummy   = [$id_no, $mbps."MB ".$days."Days", $start_date, $end_date, number_format($payment), $data->date, $ph_no];

					$rem=array(
						"id" => (int)$id,
						"msg" => nl2br(str_replace($healthy, $yummy, $msg_detail)),
						"phone" => $phone
					);
					array_push($arr["detail"], $rem);
			}
		}
	}

	echo json_encode($arr);
?>