<?php
/**
 * Created by PhpStorm.
 * User: sunusi
 * Date: 2019-05-06
 * Time: 23:14
 */


$reports = array();
foreach (getReportsDetailsByUserId($_SESSION["YUZA_ID"]) as $arr) {
//    echo $arr["patientId"];
    $reports = array_merge($reports, [$arr]);
}
//header('Content-Type: application/json');


if (!empty($reports)) {
    $curl_response = get_web_page("https://www.uduthctppp.com/api/sync/reports/", $reports);
//    print($curl_response);
    $arr_reportId = explode("|", $curl_response);
    $arr_reportId = array_filter($arr_reportId);
//    print_r($arr_reportId);
    $count = 0;
    $ack = false;
    for ($i = 0; $i < count($arr_reportId); $i++) {
        //updating patient records
        //echo $arr_reportId[$i] . "<br>";
        if (!empty(trim($arr_reportId[$i])) || $arr_reportId[$i] != "") {

            if (!$conn->query("UPDATE udut_reports SET reportSyncStatus = '1' WHERE reportId = '" . $arr_reportId[$i] . "' ")) {
                die("Error updating the local database " . $conn->error);
            } else {
                $ack = true;
            }
        }
        $count = $count + 1;
    }

    if ($ack == true) {
        print("<h5>".$count. " New Reports Records Synced successful<h1>");
    }
} else {
    print("<h5>No new reports record!<h1>");
}