<?php
    require_once("../models/settings.php");
    require_once("../models/uwazisms.php");

    $setting= new settings();
    $sms= new sms();

    if(isset($_GET['getsmsparameters'])){
        echo $setting->getsmsparameters();
    }

    if(isset($_POST['savesmsparameter'])){
        $clientid=$_POST['clientid'];
        $url=$_POST['url'];
        $senderid=$_POST['senderid'];
        $apikey=$_POST['apikey'];
        echo  $setting->setsmsparameters($clientid,$url,$senderid,$apikey);
    }

    if(isset($_GET['getsmsdatasources'])){
        echo $setting->getsmsdatasources();
    }

    if(isset($_POST['savesmstemplate'])){
        $id=$_POST['id'];
        $templatename=$_POST['templatename'];
        $templatedata=$_POST['templatedata'];
        $datasource=$_POST['datasource'];
        echo $setting->savesmstemplate($id,$templatename,$templatedata,$datasource);
    }
    
    if(isset($_GET['getsmstemplates'])){
        echo $setting->getsmstemplates();
    }

    if(isset($_POST['sendsms'])){
        $recipient=$_POST['mobilenumber'];
        $message=$_POST['message'];
        $response= $sms->sendSMS($recipient,$message);
        $status=$response['Data'][0]['MessageErrorDescription'];
        $messageid=$response['Data'][0]['MessageId'];
        // save the message inside sms log
        $sms->saveSMS('0',$recipient,$message,$messageid,$status);
        echo $status;
    }

    if(isset($_GET['getdatasourcecollumns'])){
        $datasourcename=$_GET['datasourcename'];
        echo $setting->getsmsdatasourcecollumns($datasourcename);
    }

    if(isset($_GET['getsmstemplatedetails'])){
        $id=$_GET['id'];
        echo $setting->getsmstemplatedetails($id);
    }

    if(isset($_GET['getallcustomers'])){
        $ownerid=$_GET['ownerid'];
        $propertyid=$_GET['propertyid'];
        echo $setting->getallcustomers($ownerid,$propertyid);
    }

    if(isset($_POST['smsmailmerge'])){
        $datasource=$_POST['datasource'];
        $customerid=$_POST['customerid'];
        $template=$_POST['template'];
        $obj=json_decode(str_replace( array('[',']') , ''  ,$setting->getmergedata($datasource,$customerid))) ;
        //print_r($obj);
        foreach($obj as $key => $value){
            $fieldname='@'.$key;
            //echo $fieldname.":".$value."<br/>";
            $template=str_replace($fieldname,$value,$template);
            //echo 'Your key is: '.$key.' and the value of the key is:'.$value;
        }
        // get the customers details
        $mobileno=$setting->getcustomerphonenumber($customerid);
        // send the sms
        $response= $sms->sendSMS($mobileno,$template);
        $status=$response['Data'][0]['MessageErrorDescription'];
        $messageid=$response['Data'][0]['MessageId'];
        // save the message inside sms log
        $sms->saveSMS($customerid,$mobileno,$template,$status,$messageid);
    }

    if(isset($_GET['getsmslogstatuses'])){
        echo $setting->getsmlogstatuses();
    }
    if(isset($_GET['filtersmslog'])){
        $startdate=$_GET['startdate'];
        $enddate=$_GET['enddate'];
        $status=$_GET['status'];
        echo $setting->filtersms($status,$startdate,$enddate);
    }
    if(isset($_GET['getpropertyowners'])){
        echo $setting->getpropertyowners();
    }
    if(isset($_GET['getproperties'])){
        $ownerid=isset($_GET['ownerid'])?$_GET['ownerid']:0;
        echo $setting->getproperties($ownerid);
    }
?>