$(document).on('page:beforein', '.page[data-name="person_edit"]', async function (e) {

  console.log('app person_edit ready')

  var id = e.detail.route.params.id;

  console.log(id);

  let js = JSON.parse(localStorage.getItem('t01_person'));

  let dt= js.data[id];

  console.log(dt)

  document.querySelector('#p9_id').value = dt.id || '';

  document.querySelector('#p9_name').value = dt.name || '';

  document.querySelector('#p9_initial').value = dt.initial || '';

  document.querySelector('#p9_nik').value = dt.nik || '';

  document.querySelector('#p9_npwp').value = dt.npwp|| '';

  document.querySelector('#p9_address').value = dt.address || '';

  document.querySelector('#p9_city').value = dt.city  || '';

  document.querySelector('#p9_postcode').value = dt.postcode || '';

  document.querySelector('#p9_email').value = dt.email || '';

  document.querySelector('#p9_phone').value = dt.phone || '';

  //document.querySelector('#p9_company_id').value = dt.company_id || '';

  let js2 = JSON.parse(localStorage.getItem('t01_company'));

  let dt2=js2.data;

  let sel9_company =``;

  for (let i=0;i<dt2.length;i++) {

    if(dt2[i].id==dt.id) {

      sel9_company +=`<option selected value="`+dt2[i].id+`">`+dt2[i].name+`</option>`;

    } else {

      sel9_company +=`<option value="`+dt2[i].id+`">`+dt2[i].name+`</option>`;

    }

  }

  $$("#p9_company_id").html(sel9_company);





});



$$(document).on('page:init', '.page[data-name="person_edit"]', function(e) {

  $(document).off("click", "#person_edit_button").on("click", "#person_edit_button", function(e) {

    e.preventDefault();

    const id = $('#p9_id').val();

    const name = $('#p9_name').val();

    const initial= $('#p9_initial').val();

    const nik= $('#p9_nib').val();

    const npwp= $('#p9_npwp').val();

    const address= $('#p9_address').val();

    const city= $('#p9_city').val();

    const postcode= $('#p9_postcode').val();

    const email = $('#p9_email').val();

    const phone = $('#p9_phone').val();

    const company_id = $('#p9_company_id').val();



    let req = {

      id,name,initial,nik,npwp,address,city,postcode,email,phone,company_id

    }

    if (id && name) {

      person_edit(req);

    } else {

      app.dialog.alert("All fields are required", "Person Edit Errors");

    }

  });

})



$$(document)['on']('page:init', '.page[data-name="person_edit"]', function(e) {  

  $$('#person_delete_button')['on']('click', function() {

  e.preventDefault();

    const id = $('#p9_id').val();



    let req = {

      id

    }

    if (id) {

      person_delete(req);

      //console.log(req)

    } else {

      app.dialog.alert("All fields are required", "Person Edit Errors");

    }    

  })

});