<?php
/**
 * Created by PhpStorm.
 * User: sunusi
 * Date: 2019-05-05
 * Time: 13:38
 */
$arr = array("uname"=>"", "pword"=>"1029384756");
//header('Content-Type: application/json');
$curl_response = get_web_page("https://www.uduthctppp.com/api/sync/sync-all/", $arr);
//print($curl_response);
print("<pre>");
$resArr = array();
$resArr = json_decode($curl_response, True);
//$users = $resArr["users"];
//$users_roles = $resArr["users_roles"];
//$users_privileges = $resArr["users_privileges"];
//$services = $resArr["services"];
//$services_types = $resArr["services_types"];
//print_r($users_roles);
try{
    $insTabArr = array("users", "users_roles", "users_privileges", "services", "services_types");
    $tabArr = array("udut_users", "udut_users_roles", "udut_users_privileges", "udut_services", "udut_services_types");
    for ($x = 0; $x < count($insTabArr); $x++) {
//    print_r($resArr[$insTabArr[$x]]);
        for ($i = 0; $i < count($tabArr); $i++) {
            //deleting users
            if (!$conn->query("delete from " . $tabArr[$i] . " ")) {
                die("Cannot delete users " . $conn->error);
            } else {
                foreach ($resArr[$insTabArr[$i]] as $insResult) {
                    if (!$conn->query(SQLStatement($tabArr[$i], $insResult))) {
                        die("Error saving " . $insTabArr[$i] . " in the " . $tabArr[$i] . " database: " . $conn->error);
                    }
                }
            }
        }
    }

    $ack = true;
}catch (Exception $e){
    $ack = false;
}


if (isset($ack) && $ack == true) {
    echo "<h1 align='center'>OTHER TABLE(S) AFFECTED</h1>";
    print("
        <table align='center' class='table'>
          <thead>
            <tr>
              <th>Table</th>
              <th>Status</th>
              <th>Time</th>
            </tr>
           </thead>
           <tbody>
    ");
    for ($y = 0; $y < count($insTabArr); $y++) {
        $time = date("d/m/Y h:i:s");
        print("<tr>
              <td>" . strtoupper(str_replace("_"," ",$insTabArr[$y])) . " [".count($resArr[$insTabArr[$y]])." records]</td>
              <td>SUCCESSFUL</td>
              <td>" . $time . "</td>
              </tr>");
    }
    print("
           </tbody>
        </table>
    ");

}
//
//if (!empty($patients)) {
//    $curl_response = get_web_page("https://www.uduthctppp.com/api/sync/patients/", $patients);
////print($curl_response);
//    $arr_patientId = explode("|", $curl_response);
////    print_r($arr_patientId);
//    $arr_patientId = array_filter($arr_patientId);
//    $count = 0;
//    $ack = false;
//    for ($i = 0; $i < count($arr_patientId); $i++) {
//        //updating patient records
////        echo $arr_patientId[$i];
//        if (!empty(trim($arr_patientId[$i]))) {
//            $count = $count + 1;
//            if (!$conn->query("UPDATE udut_patients SET patientSyncStatus = '1' WHERE patientId = '" . $arr_patientId[$i] . "' ")) {
//                die("Error updating the local database " . $conn->error);
//            } else {
//                $ack = true;
//            }
//        }
//    }
//
//    if ($ack == true) {
//        print($count . " Records Synced successful<br>");
//    }
//} else {
//    print("No new patients record<br>");
//}
?>
