<?php
/**
 * Created by PhpStorm.
 * User: sunusi
 * Date: 2019-02-20
 * Time: 09:42
 */

session_start();

require_once getcwd() . "/../functions/db.class.php";
require_once getcwd() . "/../functions/form.data.php";
require_once getcwd() . "/../functions/robot.php";


// header('Content-Type: application/json');

// ################ fetching online database
//$chi = curl_init();



$users = file_get_contents('http://uduthctppp.com/p/sync/sync_to_offline_users.php');
$user_roles = file_get_contents('http://uduthctppp.com/p/sync/sync_to_offline_user_roles.php');
$user_privileges = file_get_contents('http://uduthctppp.com/p/sync/sync_to_offline_user_privileges.php');
$scan_types = file_get_contents('http://uduthctppp.com/p/sync/sync_to_offline_scan_types.php');


//users
$users = json_decode($users, true);
$query->execute("DELETE FROM udut_users WHERE  userId != '' ");
foreach($users as $arr_users){
    $stmt = SQLStatement("udut_users", $arr_users);
    $query->execute($stmt);
}


//user roles goes here
$user_roles = json_decode($user_roles, true);
$query->execute("DELETE FROM udut_users_roles WHERE  roleId != '' ");
foreach($user_roles as $arr_user_roles){
    $stmt = SQLStatement("udut_users_roles", $arr_user_roles);
    $query->execute($stmt);
}

//user privileges
$user_privileges = json_decode($user_privileges, true);
$query->execute("DELETE FROM udut_users_privileges WHERE  privilegeId != '' ");
foreach($user_privileges as $arr_user_privileges){
    $stmt = SQLStatement("udut_users_privileges", $arr_user_privileges);
    $query->execute($stmt);
}

//scan types
$scan_types = json_decode($scan_types, true);
$query->execute("DELETE FROM udut_scan_types WHERE  typeId != '' ");
foreach($scan_types as $arr_scan_types){
    $stmt = SQLStatement("udut_scan_types", $arr_scan_types);
    $query->execute($stmt);
}

//$results = array($results);

//$results = json_encode($results);

//print_r($results);
//$results = array($results);
//echo sizeof($results,1);


//foreach($results as $arr_result){
//    $arr_result = array($arr_result);
//    foreach($arr_result as $result){
//        $patient = array($result);
//        echo sizeof($patient, true);
//    }
//}

//if(is_array($results)){
//    echo "is array";
//}else{
//    echo "is not an array";
//}

//foreach($results as $arr_result){
//    foreach(array($arr_result) as $result){
////        var_dump($result);
//        print("<pre>");
//        foreach ($result as $arr_re){}
//        print_r($result);
//    }
//}
//





//$payments = $_POST['payments'];
//$returnId = $_POST['patientId'];
//unset($_POST['payments']);
//unset($_POST['patientId']);
//
//$stmt = SQLStatement("udut_patients", $_POST);
//// echo $stmt."<br>";
//$query->execute($stmt);
//
//$patient = $query->getOne("SELECT * FROM udut_patients ORDER BY patientId DESC LIMIT 1");
//$patientId = $patient['patientId'];
//
//
//foreach($payments as $arr_data){
//    unset($arr_data['paymentId']);
//    $arr_data['patientId'] = $patientId;
//    $stmtp = SQLStatement("udut_payments", $arr_data);
//    $query->execute($stmtp);
//}
?>
<script language='javascript'>
    alert('Synced successfully.');
    window.close();
</script>