<?php

ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$zroot = $_SESSION["zdir"];
$zdb = $_SESSION["zdbyear_" . $dbname];
$society = $_SESSION["society_" . $dbname];
$username = $_SESSION["username_" . $dbname];
$keycond = "";


$querycomp = mysqli_query($conn, "SELECT * FROM $zroot.`company`") or die(mysqli_error($conn));
$fetcmpname = mysqli_fetch_object($querycomp)or die(mysqli_error($conn));
//$mcmpname = mysqli_real_escape_string($conn, $fetcmpname->compname);
$mcmpname = mysqli_real_escape_string($conn, $fetcmpname->compname);
$mtel = mysqli_real_escape_string($conn, $fetcmpname->htel);

mysqli_select_db($conn, $zdb);
$admin = $_SESSION["admin_" . $dbname];

//$success = false;


if (isset($_REQUEST["vouchno"])) {
    $vouchno = $_REQUEST['vouchno'];
    $querymain = mysqli_query($conn, "SELECT * FROM `todo` where vouchno = '$vouchno'") or die(mysqli_error($conn));
    $fetmain = mysqli_fetch_object($querymain)or die(mysqli_error($conn));
    $task = $fetmain->task;
    $gettran = mysqli_query($conn, "Select * from `todotran` where vouchno = '$vouchno'") or die(mysqli_error($conn));

    while ($fettran = mysqli_fetch_array($gettran)) {

        $querynum = mysqli_query($conn, "SELECT * FROM $zroot.`passmain` where code = '$fettran[staff]'") or die(mysqli_error($conn));
        $fetnum = mysqli_fetch_object($querynum)or die(mysqli_error($conn));

        $mobnum = $fetnum->mobno;
        $queryas = mysqli_query($conn, "SELECT * FROM $zroot.`passmain` where code = '$fetmain->assby'") or die(mysqli_error($conn));
        $fetass = mysqli_fetch_object($queryas)or die(mysqli_error($conn));
        $assby = $fetass->user;
//        $mobnum = $mobile1;
//    $mobnum = '8652372200,7718888446';

        if ($mobnum != '') {
//            To Do List  {#var#} task {#var#} assigned by {#var#}
//Rapid Residency 
            $msgtech = "To Do List  $vouchno task $task assigned by $assby Rapid Residency";

            $user = "RAPIDK";
            $password = "RAPIDK2022";
            $senderid = "RAPIDK";
//    $url = "144.76.39.175/api.php?";
            $url = "http://vas.mobilogi.com/api.php?";




            $mobilenumbers = $mobnum;

            $messagetech = urlencode($msgtech);

            $chtech = curl_init();
            if (!$chtech) {
                die("Couldn't initialize a cURL handle");
            }
            $rettech = curl_setopt($chtech, CURLOPT_URL, $url);
            curl_setopt($chtech, CURLOPT_POST, 1);
            curl_setopt($chtech, CURLOPT_POSTFIELDS, "username=$user&password=$password&route=1&sender=$senderid&mobile[]=$mobilenumbers&message[]=$messagetech&templateid=1207164752011149051");

            $rettech = curl_setopt($chtech, CURLOPT_RETURNTRANSFER, true);

            $curlresponsetech = curl_exec($chtech);
            if (curl_errno($chtech)) {
//        echo 'curl error : ' . curl_error($chtech);
            }
            if (empty($rettech)) {

                die(curl_error($chtech));
                curl_close($chtech);
            } else {
                $infotech = curl_getinfo($chtech);
                curl_close($chtech);

//        echo $curlresponsetech;
            }
            $succ = true;
        } else {
            $succ = false;
        }
    }
    $rows = array();
    $rows = array('success' => $succ);
    header("Content-type: application\json");
    echo json_encode($rows);
//    }
}