<!DOCTYPE html>
<html lang="en">
<script type='text/javascript' >
  var myFunctionTag = false;
</script>

<head>

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta content="Back4App Inc" name="author">

  <title>DECAMP eHealth app</title>

  <script src="https://npmcdn.com/parse@1.10.0/dist/parse.min.js"></script>
  <img src="http://decamp.cs.pub.ro/wp-content/uploads/2016/09/decamp_logo.png" class="center">



  
</head>

<body>
    <header>
    <img src="  https://upload.wikimedia.org/wikipedia/it/e/ee/Logo_Universit%C3%A0_Padova.png"  width="100" height="100" class="center">
      <h2><p class="text-primary">Welcome to your e-Health app!</p></h2>
      <h4><p class="text-primary">Authenticate yourself:</p></h4>
    </header>
    <br>
    <!-- Your code here -->

    <div class="container">
      <form>
        <div class="form-group">
          <label for="usr">Name:</label>
          <input type="text" class="form-control" id="usr" placeholder="Enter your name">
        </div>
        <div class="form-group">
          <label for="pwd">Password:</label>
          <input type="password" class="form-control" id="pwd" placeholder="Enter your password">
        </div>
      </form>
      <button type="button" class="btn btn-primary" onclick="login()">Authenticate</button>
    </div>
    <br>
    <br>
    <br>



</body>

<script src="login.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>


</html>
// Code goes here

<html>
<head>
  <script src="https://npmcdn.com/parse@1.10.0/dist/parse.min.js"></script>
  <link rel="stylesheet" href="main.css">
</head>

<body>
<div>
<header>
<img src="http://decamp.cs.pub.ro/wp-content/uploads/2016/09/decamp_logo.png">
</header>
<a href="index.html" class="btn btn-default">home</a>
<hr />

<div class="panel panel-success">
<div class="panel-heading"><h3>Personal data</h3></div>
  <div class="panel-body">
    <div class="list-group">
      <h4>Name:</h4> <span id="patientNameToGet"></span></h1>
      <h4>Gender:</h4><span id="patientGenderToGet"></span></h1>
      <h4>Date of birth:</h4><span id="patientBirthToGet"></span></h1>
      <h4>Phone:</h4><span id="patientPhoneToGet"></span></h1>
      <h4>Address:</h4><span id="patientAddressToGet"></span></h1>
    </div>
  </div>
</div>



<div class="panel panel-info">
<div class="panel-heading"><h3>Medical data</h3></div>
<div class="panel-body">
<h4>Doctor's opinions:</4><hr /><span id="patientTextareaToGet"></span></h1>

</div>
</div>

<hr />

</body>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="showPatientData.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</html>
<head>
  <script src="getPatientList.js"></script>
  <script src="https://npmcdn.com/parse@1.10.0/dist/parse.min.js"></script>
  <link rel="stylesheet" href="styles/main.css">
</head>



  <div>

    <header>
      <img src="http://decamp.cs.pub.ro/wp-content/uploads/2016/09/decamp_logo.png" class="center">
    </header>
    <a href="index.html" class="btn btn-default">home</a>
    <br>
    <div class="container">
    <button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#myapp" id="okDoctor">Patients Edit</button>
      <div id="myapp" class="panel-collapse collapse" >
        <h2><p class="text-primary">Registering Patients</p></h2>
        <h3><p class="text-primary">Choose the action you want:</p></h3>
        <button type="button" name="create" id="create">
          <span class="glyphicon glyphicon-edit"></span> Create</button>
        <button type="button" name="read" id="read">
          <span class="glyphicon glyphicon-list-alt"></span> Read</button>
        <button type="button" name="update" id="update"disabled>
          <span class="glyphicon glyphicon-cloud-upload"></span> Update</button>
        <button type="button" name="delete" id="delete"disabled>
          <span class="glyphicon glyphicon-trash"></span> Delete</button>
        <br>
        <br>
        <br>
        <div id="form">
          <form id="form_patients" class="">
            <fieldset form="form_patients">
              <legend id="form_title">Create New Patient</legend>
              Name: <input type="text" id="input_name" name="input_name" placeholder="Name" size="40"><br>
              Gender: <input type="text" id="input_gender" name="input_gender" placeholder="Gender" size="20"> M / F / OTHER<br>
              Date of Birth: <input type="date" id="input_birth" name="input_birth" placeholder="Date of Birth" size="10"><br>
              Phone: <input type="tel" id="input_phone" name="input_phone" placeholder="Phone" size="20"><br>
              Address: <input type="text" id="input_address" name="input_address" placeholder="Address" size="40"><br>
              Password: <input type="password" id="input_password" name="input_password" placeholder="Password" size="40"><br>
              TextArea: <input type="text" id="input_textarea" name="input_textarea" placeholder="TextArea" size="40"><br><br>
              <input type="button" name="save" id="submit" value="Save">
            </fieldset>
          </form>
          <p id="message"></p>
        </div>
      </div>
    </div>
  </div>
  <button type="button" class="btn btn-primary"  id="seePatient" onclick="getPatientList()">Patients Refresh</button>

<body>

  <div class="table-responsive">
      <table class="table">
      <thead>
          <tr>
              <th>Name</th>
              <th>Gender</th>
              <th>Date of Birth</th>
          </tr>
      </thead>
      <tbody id="todos-list">

      </tbody>
  </table>
  </div>

</body>
  <!-- Your JS script -->
  <script src="interactiveness.js"></script>
  <script src="CRUD.js"></script>
  <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</body>

</html>
Parse.initialize("SrUxt43yc86v3SsNa5BhaERYOz9awUfurvtobQsI"); //PASTE YOUR Back4App APPLICATION ID
Parse.javaScriptKey = "kqfaNcmDZPr8sMkFGgH5palkqJZ19xasHGHVbgYf"; //PASTE YOUR Javascript KEY
Parse.serverURL = "https://parseapi.back4app.com/";

var Patient = Parse.Object.extend("Patient");

// Test with different values of name and age and see the ParseDashboard to see the changes
var textName = "myName";
var textGender = "myGender";
var textBirth = 761996;
var textPhone = 123456;
var textAddress = "myAddress";
var textPassword = "myPassword";
var textTextArea = "myTextArea";

create();

function create() {
    mypatient = new Patient();
    mypatient.set("name", textName);
    mypatient.set("gender", textGender);
    mypatient.set("birth", textBirth);
    mypatient.set("phone", textPhone);
    mypatient.set("address", textAddress);
    mypatient.set("password", textPassword);
    mypatient.set("textarea", textTextArea);

    mypatient.save(null, {
        success: function (patient) {
            console.log('Patient created successful with name: ' + patient.get("name") + ' and date of birth: ' + patient.get("birth"));
        },
        error: function (response, error) {
            console.log('Error: ' + error.message);
        }
    });
}
Parse.initialize("SrUxt43yc86v3SsNa5BhaERYOz9awUfurvtobQsI"); //PASTE YOUR Back4App APPLICATION ID
Parse.javaScriptKey = "kqfaNcmDZPr8sMkFGgH5palkqJZ19xasHGHVbgYf"; //PASTE YOUR Javascript KEY
Parse.serverURL = "https://parseapi.back4app.com/";

var Patient = Parse.Object.extend("Patient");

submitPatientBtn.onclick = function() {
    getElements();
    if (submitPatientBtn.value === "Save") {
        createPatient();
    } else if (submitPatientBtn.value === "Search") {
        readPatient();
    } else if (submitPatientBtn.value === "Update") {
        updatePatient();
    } else if (submitPatientBtn.value === "Delete") {
        deletePatient();
    }
};

function createPatient() {
    if (textName === null || textName === "") {
        alert("Please type a patient name");
    } else if(textGender === null || textGender === "") {
        alert("Please type a gender");
    } else if(textBirth === null || textBirth === "") {
        alert("Please type a date of birth");
    } else if(textPhone === null || textPhone === "") {
        alert("Please type a phone number");
    } else if(textAddress === null || textAddress === "") {
        alert("Please type an address");
    } else if(textPassword === null || textPassword === "") {
        alert("Please type a password");
    } else if(textTextArea === null || textTextArea === "") {
        alert("Please type a comment");
    } else {
        mypatient = new Patient();
        mypatient.set("name", textName);
        mypatient.set("gender", textGender);
        mypatient.set("birth", textBirth);
        mypatient.set("phone", textPhone);
        mypatient.set("address", textAddress);
        mypatient.set("password", textPassword);
        mypatient.set("textarea", textTextArea);

        mypatient.save(null, {
            success: function (mypatient) {
                inputName.value = null;
                inputGender.value = null;
                inputBirth.value = null;
                inputPhone.value = null;
                inputAddress.value = null;
                inputPassword.value = null;
                inputTextArea.value = null;
                textMessage.innerHTML = 'Patient created!' + "<br />" +
                                        'Name: ' + mypatient.get("name") + "<br />" +
                                        'Gender: ' + mypatient.get("gender") + "<br />" +
                                        'Date of birth: ' + mypatient.get("birth") + "<br />" +
                                        'Phone: ' + mypatient.get("phone") + "<br />" +
                                        'Address: ' + mypatient.get("address") + "<br />" +
                                        'Comments: ' + mypatient.get("textarea");
            },
            error: function (response, error) {
                textMessage.innerHTML = error.message;
                alert('Error: ' + error.message);
            }
        });
    }
}

function readPatient() {
    findPatient = new Patient ();
    query = new Parse.Query(Patient);

    if (textName === null || textName === "") {
        alert("Please type a patient name to search");
    } else {
        query.equalTo("name", textName);
        query.first({
            success: function(mypatient) {
                if (mypatient) {
                    findPatient = mypatient;
                    textMessage.innerHTML = 'Patient name: ' + mypatient.get("name") + "<br />" +
                                            'Gender: ' + mypatient.get("gender") + "<br />" +
                                            'Date of birth: ' + mypatient.get("birth") + "<br />" +
                                            'Phone: ' + mypatient.get("phone") + "<br />" +
                                            'Address: ' + mypatient.get("address") + "<br />" +
                                            'Comments: ' + mypatient.get("textarea");
                    updatePatientBtn.disabled = false;
                    deletePatientBtn.disabled = false;
                    inputName.value = null;
                    inputGender.value = null;
                    inputBirth.value = null;
                    inputPhone.value = null;
                    inputAddress.value = null;
                    inputPassword.value = null;
                    inputTextArea.value = null;
                } else {
                    alert("Nothing found, please try again");
                }
            },
            error: function(error) {
                alert("Error: " + error.code + " " + error.message);
            }
        });
    }
}

function updatePatient() {
  if (textName === null || textName === "") {
      alert("Please type a patient name");
  } else if(textGender === null || textGender === "") {
      alert("Please type a gender");
  } else if(textBirth === null || textBirth === "") {
      alert("Please type a date of birth");
  } else if(textPhone === null || textPhone === "") {
      alert("Please type a phone number");
  } else if(textAddress === null || textAddress === "") {
      alert("Please type an address");
  } else if(textPassword === null || textPassword === "") {
      alert("Please type a password");
  } else if(textTextArea === null || textTextArea === "") {
      alert("Please type a comment");
  } else {
        findPatient.set('name', textName);
        findPatient.set("gender", textGender);
        findPatient.set("birth", textBirth);
        findPatient.set("phone", textPhone);
        findPatient.set("address", textAddress);
        findPatient.set("password", textPassword);
        findPatient.set("textarea", textTextArea);
        findPatient.save(null, {
            success: function (response) {
                alert('Patient updated with objectId: ' + response.id);
                textMessage.innerHTML = 'Patient name: ' + mypatient.get("name") + "<br />" +
                                        'Gender: ' + mypatient.get("gender") + "<br />" +
                                        'Date of birth: ' + mypatient.get("birth") + "<br />" +
                                        'Phone: ' + mypatient.get("phone") + "<br />" +
                                        'Address: ' + mypatient.get("address") + "<br />" +
                                        'Comments: ' + mypatient.get("textarea");

            },
            error: function (response, error) {
                alert('Error: ' + error.message);
            }
        });
    }
}

function deletePatient() {
    findPatient.destroy({
        success: function(response) {
            alert('Patient erased successfully');
            findPatient = null;
            textGender = null;
            textName = null;
            textBirth = null;
            textPhone = null;
            textAddress = null;
            textPassword = null;
            textTextArea = null;
            textMessage = null;
            inputName.value = null;
            inputGender.value = null;
            inputBirth.value = null;
            inputPhone.value = null;
            inputAddress.value = null;
            inputPassword.value = null;
            inputTextArea.value = null;
            updatePatientBtn.disabled = true;
            deletePatientBtn.disabled = true;
            readBtn();
        },
        error: function(response, error) {
            alert('Error: '+ error.message);
        }
    });
}
Parse.initialize("SrUxt43yc86v3SsNa5BhaERYOz9awUfurvtobQsI"); //PASTE YOUR Back4App APPLICATION ID
Parse.javaScriptKey = "kqfaNcmDZPr8sMkFGgH5palkqJZ19xasHGHVbgYf"; //PASTE YOUR Javascript KEY
Parse.serverURL = "https://parseapi.back4app.com/";

var Patient = Parse.Object.extend("Patient");

// Pass the name of the pet you want to delete
var textName = "myName";

readThenDelete();

function readThenDelete() {
    query = new Parse.Query(Patient);
    query.equalTo("name", textName);
    query.first({
        success: function (patient) {
            if (patient) {
                console.log('Patient found with name: ' + patient.get("name") + ' and date of birth: ' + patient.get("birth"));
                update(patient);
            } else {
                console.log("Nothing found, please try again");
                return null;
            }
        },
        error: function (error) {
            console.log("Error: " + error.code + " " + error.message);
            return null;
        }
    });
}

function update(foundPatient) {
    foundPatient.destroy({
        success: function(response) {
            console.log('Patient '+ foundPatient.get("name") + ' erased successfully');
        },
        error: function(response, error) {
            console.log('Error: '+ error.message);
        }
    });
}

Parse.serverURL = 'https://parseapi.back4app.com';
Parse.initialize('SrUxt43yc86v3SsNa5BhaERYOz9awUfurvtobQsI', 'kqfaNcmDZPr8sMkFGgH5palkqJZ19xasHGHVbgYf', 'Cuj1kL9Lvi52QSZSWDtrrCecFYP6LZ96WzY9FatS');




function getPatientList() {

//document.getElementById('todos-list').replaceWith('<tbody id="todos-list"></tbody>');

const foo = document.getElementById('todos-list');

while(foo.children.length != 0)
{
  foo.children[0].remove();
}


const todosAreaEl=  document.getElementById('todos-list');


const Patient = Parse.Object.extend('Patient');
const query = new Parse.Query(Patient);

query.find().then((results) => {
  // You can use the "get" method to get the value of an attribute
  // Ex: response.get("<ATTRIBUTE_NAME>")
  if (typeof document !== 'undefined')
  {
    //document.write(`Patient found: ${JSON.stringify(results)}`);
    results.forEach(function(todo)
    {
        var obj = JSON.parse(JSON.stringify(todo));
        const todoEl = `
        <tr>
        <th>${obj.name}</th>
        <th>${obj.gender}</th>
        <th>${obj.birth}</th>
        </tr>
        `;
        todosAreaEl.insertAdjacentHTML('beforeend',todoEl);
    });
  }
  console.log('Patient found', results);
}, (error) => {
  if (typeof document !== 'undefined') document.write(`Error while fetching Patient: ${JSON.stringify(error)}`);
  console.error('Error while fetching Patient', error);
});
;


}
var mypatient, textName, textGender, textBirth, textPhone, textAddress, textPassword, textTextArea, textMessage, query, findPatient;

//buttons and form
var createPatientBtn = document.getElementById("create");
var readPatientBtn = document.getElementById("read");
var updatePatientBtn = document.getElementById("update");
var deletePatientBtn = document.getElementById("delete");
var submitPatientBtn = document.getElementById("submit");

createPatientBtn.onclick = createBtn;
readPatientBtn.onclick = readBtn;
updatePatientBtn.onclick = updateBtn;
deletePatientBtn.onclick = deleteBtn;

function createBtn () {
  getElements();
  textMessage.innerHTML = null;
  form_title.innerHTML = "Create New Patient";
  submitPatientBtn.value = "Save";
  inputName.disabled = false;
  input_gender.disabled = false;
  input_birth.disabled = false;
  input_phone.disabled = false;
  input_address.disabled = false;
  input_password.disabled = false;
  input_textarea.disabled = false;
  updatePatientBtn.disabled = true;
  deletePatientBtn.disabled = true;
}

function readBtn () {
  getElements();
  textMessage.innerHTML = null;
  form_title.innerHTML = "Search Patient";
  inputName.disabled = false;
  input_gender.value = null;
  input_gender.disabled = true;
  input_birth.value = null;
  input_birth.disabled = true;
  input_phone.value = null;
  input_phone.disabled = true;
  input_address.value = null;
  input_address.disabled = true;
  input_password.value = null;
  input_password.disabled = true;
  input_textarea.value = null;
  input_textarea.disabled = true;
  // updatePatient.disabled = true;
  // deletePatient.disabled = true;
  submitPatientBtn.value = "Search";
}

function updateBtn () {
  getElements();
  textMessage.innerHTML = "Patient id = " + findPatient.id + " and date of birth = " + findPatient.get("birth");
  form_title.innerHTML = "Update Patient";
  submitPatientBtn.value = "Update";
  inputName.disabled = true;
  input_gender.disabled = false;
  input_birth.disabled = false;
  input_phone.disabled = false;
  input_address.disabled = false;
  input_password.disabled = false;
  input_textarea.disabled = false;
  updatePatientBtn.disabled = false;
  deletePatientBtn.disabled = false;
  inputName.value = findPatient.get("name");
  inputGender.value = findPatient.get("gender");
  inputBirth.value = findPatient.get("birth");
  inputPhone.value = findPatient.get("phone");
  inputAddress.value = findPatient.get("address");
  inputPassword.value = findPatient.get("password");
  inputTextArea.value = findPatient.get("textarea");
}

function deleteBtn () {
  getElements();
  textMessage.innerHTML = null;
  form_title.innerHTML = "Delete Patient";
  submitPatientBtn.value = "Delete";
  inputName.value = findPatient.get("name");
  inputName.disabled = true;
  inputGender.value = findPatient.get("gender");
  input_gender.disabled = true;
  inputBirth.value = findPatient.get("birth");
  input_birth.disabled = true;
  inputPhone.value = findPatient.get("phone");
  input_phone.disabled = true;
  inputAddress.value = findPatient.get("address");
  input_address.disabled = true;
  inputPassword.value = findPatient.get("password");
  input_password.disabled = true;
  inputTextArea.value = findPatient.get("textarea");
  input_textarea.disabled = true;
}

function getElements () {
    inputName = document.getElementById("input_name");
    inputGender = document.getElementById("input_gender");
    inputBirth = document.getElementById("input_birth");
    inputPhone = document.getElementById("input_phone");
    inputAddress = document.getElementById("input_address");
    inputPassword = document.getElementById("input_password");
    inputTextArea = document.getElementById("input_textarea");
    textName = document.getElementById("input_name").value;
    textGender = document.getElementById("input_gender").value;
    textBirth = document.getElementById("input_birth").value;
    textPhone = document.getElementById("input_phone").value;
    textAddress = document.getElementById("input_address").value;
    textPassword = document.getElementById("input_password").value;
    textTextArea = document.getElementById("input_textarea").value;
    textMessage = document.getElementById("message");
}
Parse.initialize("SrUxt43yc86v3SsNa5BhaERYOz9awUfurvtobQsI"); //PASTE YOUR Back4App APPLICATION ID
Parse.javaScriptKey = "kqfaNcmDZPr8sMkFGgH5palkqJZ19xasHGHVbgYf"; //PASTE YOUR Javascript KEY
Parse.serverURL = "https://parseapi.back4app.com/";

function login() {

    var Patient = Parse.Object.extend("Patient");
    var Doctor = Parse.Object.extend("Doctor");

    var textName = document.getElementById("usr").value;
    var textPass = document.getElementById("pwd").value;

    queryPatient = new Parse.Query(Patient);
    queryPatient.equalTo("name", textName);
    queryPatient.first().then(function(patient){
       if(patient && patient.get("name") == textName && patient.get("password") == textPass)
       {
          alert('patient');
          localStorage.setItem('PatientToShowName', patient.get("name"));
          localStorage.setItem('PatientToShowGender', patient.get("gender"));
          localStorage.setItem('PatientToShowBirth', patient.get("birth"));
          localStorage.setItem('PatientToShowPhone', patient.get("phone"));
          localStorage.setItem('PatientToShowAddress', patient.get("address"));
          localStorage.setItem('PatientToShowTextarea', patient.get("textarea"));
          window.location='patient.html';
       }
       else {
           queryDoctor = new Parse.Query(Doctor);
           queryDoctor.equalTo("name", textName);
           queryDoctor.first().then(function(doctor){
               if(doctor && doctor.get("name") == textName && doctor.get("password") == textPass)
                 {
                   alert('doctor');
                   window.location='doctor.html';
                 }
               else {
                 alert('No user or doctor with these credentials');
               }
           }).catch(function(error){
               alert("Error: " + error.code + " " + error.message);
           });
         }
        }).catch(function(error){
            alert("Error: " + error.code + " " + error.message);
    });


}
Parse.initialize("SrUxt43yc86v3SsNa5BhaERYOz9awUfurvtobQsI"); //PASTE YOUR Back4App APPLICATION ID
Parse.javaScriptKey = "kqfaNcmDZPr8sMkFGgH5palkqJZ19xasHGHVbgYf"; //PASTE YOUR Javascript KEY
Parse.serverURL = "https://parseapi.back4app.com/";

var Patient = Parse.Object.extend("Patient");

// Pass the name you want to search on the database
var textName = "myName";
var textGender = "myGender";
var textBirth = 761996;
var textPhone = 123456;
var textAddress = "myAddress";
var textPassword = "myPassword";
var textTextArea = "myTextArea";

read();

function read() {
    query = new Parse.Query(Patient);
    query.equalTo("name", textName);
    query.first({
        success: function (patient) {
            if (patient) {
                console.log('Patient found successful with name: ' + patient.get("name") + ' and date of birth: ' + patient.get("birth"));
            } else {
                console.log("Nothing found, please try again");
            }
        },
        error: function (error) {
            console.log("Error: " + error.code + " " + error.message);
        }
    });
}
var nameTmp =localStorage.getItem('PatientToShowName');
var genderTmp = localStorage.getItem('PatientToShowGender');
var birthTmp =localStorage.getItem('PatientToShowBirth');
var phoneTmp =localStorage.getItem('PatientToShowPhone');
var addressTmp =localStorage.getItem('PatientToShowAddress');
var textTmp =localStorage.getItem('PatientToShowTextarea');

document.getElementById("patientNameToGet").innerHTML = nameTmp;
document.getElementById("patientGenderToGet").innerHTML = genderTmp;
document.getElementById("patientBirthToGet").innerHTML = birthTmp;
document.getElementById("patientPhoneToGet").innerHTML = phoneTmp;
document.getElementById("patientAddressToGet").innerHTML = addressTmp;
document.getElementById("patientTextareaToGet").innerHTML = textTmp;
Parse.initialize("SrUxt43yc86v3SsNa5BhaERYOz9awUfurvtobQsI"); //PASTE YOUR Back4App APPLICATION ID
Parse.javaScriptKey = "kqfaNcmDZPr8sMkFGgH5palkqJZ19xasHGHVbgYf"; //PASTE YOUR Javascript KEY
Parse.serverURL = "https://parseapi.back4app.com/";

var Patient = Parse.Object.extend("Patient");

// Test with different values of name and age and see the ParseDashboard to see the changes
var textName = "myName";
var textGender = "myGender";
var textBirth = 761996;
var textPhone = 123456;
var textAddress = "myAddress";
var textPassword = "myPassword";
var textTextArea = "myTextArea";

readThenUpdate();

function readThenUpdate() {
    query = new Parse.Query(Patient);
    query.equalTo("name", textName);
    query.first({
        success: function (patient) {
            if (patient) {
                console.log('Patient found with name: ' + patient.get("name") + ' and date of birth: ' + patient.get("birth"));
                update(patient);
            } else {
                console.log("Nothing found, please try again");
            }
        },
        error: function (error) {
            console.log("Error: " + error.code + " " + error.message);
        }
    });
}

function update(foundPatient) {
    foundPatient.set("name", textName);
    foundPatient.set("gender", textGender);
    foundPatient.set("birth", textBirth);
    foundPatient.set("phone", textPhone);
    foundPatient.set("address", textAddress);
    foundPatient.set("password", textPassword);
    foundPatient.set("textarea", textTextArea);

    foundPatient.save(null, {
        success: function (patient) {
            console.log('Patient updated! Name: ' + patient.get("name") + ', new gender: ' + patient.get("gender") + ', new date of birth: ' + patient.get("birth") + ', new phone: ' + patient.get("phone") + ', new address: ' + patient.get("address"));
        },
        error: function (response, error) {
            console.log('Error: ' + error.message);
        }
    });
}
header {
  background: white;
  color: #208AEC;
}

footer {
  background: #f2f2f2;
  color: black;
}

body {
  background: #eee;
  text-align: center;
  padding: 0 25px;
}

img {
  max-width: 100%;
  display: block;
  height: auto;
  margin: auto;
}

table {
    border-collapse: separate;
    border-spacing: 10px 50px;
}

.hide {
  visibility: hidden;
}
header {
  background: white;
  color: #208AEC;
}

footer {
  background: #f2f2f2;
  color: black;
}

body {
  background: #eee;
  text-align: center;
  padding: 0 25px;
}

img {
  max-width: 100%;
  display: block;
  height: auto;
  margin: auto;
}

table {
    border-collapse: separate;
    border-spacing: 10px 50px;
}

.hide {
  visibility: hidden;
}