<?php

//error_reporting(E_ALL);

include("../db.php");
$ID = $LinksDetails["sms_id"];
$Pwd = $LinksDetails["sms_pwd"];
$senderID = trim($LinksDetails["senderID"]);

$TemplateID = $_REQUEST["templateid"];
$EntityID = $_REQUEST["entityid"];

if ($TemplateID == "") {
    $TemplateID = $LinksDetails["templateid"];
}
if ($EntityID == "") {
    $EntityID = $LinksDetails["entityid"];
}

$jsondata = $_POST['json'];
$idata = json_decode($jsondata, true);

$msg = $idata['str'];
$mobile = $idata['mobile'];
$session = $idata['session'];
$page = $idata['page'];
$school_id = $idata['school_id'];
$Added_by = $idata['Added_by'];

//Prepare you post parameters
$postData = array('Id' => $ID, 'Pwd' => $Pwd, 'PhNo' => $mobile, 'text' => $msg);

//$encodedMsg = urlencode($msg);
$encodedMsg = urlencode(makeSmsString($idata['template_param']));

$url = "http://sms.uphargroup.com/submitsms.jsp?user=$ID&key=$Pwd&mobile=91$mobile&message=$encodedMsg&senderid=$senderID&accusage=1&entityid=$EntityID&tempid=$TemplateID";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
//curl_close($ch);

if ($response) {

    $aryData = array("mobile" => $mobile, "msg" => $msg, "status" => $response, "session" => $session, "dtime" => $date, "page" => $page, "school_id" => $school_id, "added_by" => $Added_by);

    $insert_complain = $db->insertAry("sms", $aryData);

    if ($insert_complain > 0) echo json_encode(array('status' => 1, 'msg' => 'message send..!', 'url' => $url));

} else {
    echo json_encode(array('status' => 0, 'msg' => 'message not send..!', 'url' => $url, 'curl_res' => $response));
}

/////////////////////

function makeSmsString($templateParam){
    if ($templateParam['type'] == 'absent'){
        return "Dear Parents, Your Child $templateParam[name] of class- $templateParam[class]($templateParam[section]) is absent in school on $templateParam[date] Regards Principal";
    }elseif ($templateParam['type'] == 'present'){
        return "Dear Parents, Your Child $templateParam[name] is present in school on $templateParam[date] Regards Principal";
    }elseif ($templateParam['type'] == 'late'){
        return "Dear Parents, Your Child $templateParam[name] of class- $templateParam[class]($templateParam[section]) is late in school on $templateParam[date] Regards Principal SCHOOL";
    }else{
        return "";
    }
}