<!-- Main Content -->
<div class="container">
  <div class="row">
    <!-- Widget Item -->
    <div class="col-md-12">
      <div class="widget-area-2 proclinic-box-shadow">
        <h3 class="widget-title">Database Synchronisation</h3>
        <div class="row justify-content-center">
          <div class="col-md-10">
            <h5 align='center'>Please wait until local database is been updated.</h5>
            <h5 align="center"><img src="<?php echo imgPATH;?>preloader.gif" alt=""> </h5>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

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


//patient details

function reArrangeArray1D($array){
    // variable declaration
    $var = "";
    $val = "";
    $arr = array();
    // loop
    foreach ($array as $var => $val) {
        $arr[$var] = $val;
    }
    return $arr;
}

$patients = $query->getAll("select * from udut_patients where patientSyncStatus = 0");

foreach($patients as $patient){
  //getting payment details of particular patient
    $payments = $query->getAll("select * from udut_payments where patientId = '".$patient['patientId']."' ");

    //updating the
    $query->execute("update udut_patients set patientSyncStatus = 1 where patientId = '".$patient['patientId']."' ");
//    print_r($payments);
//    print("<br>");
    $arr_pat = $patient;

    $arr_pay = array();
    foreach($payments as $payment){
        //getting visit details
        $arr_pay[] =  reArrangeArray1D($payment);
    }
    $patient["payments"] = $arr_pay;
//    print_r($patient);
//    print("<br>");

//extract data from the post
extract($_POST);

//set POST variables
$url = 'http://uduthctppp.com/p/sync/sync_to_online.php';

//url-ify the data for the POST
$fields_string = http_build_query($patient);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//updating the affected patient records where successful

//close connection
curl_close($ch);

}




$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);
}


?>

<script language='javascript'>
    alert('Synced successfully.');
    window.close();
</script>