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

  console.log('app company_edit ready')

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

  console.log(id);

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

  let dt= js.data[id];

  console.log(dt)

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

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

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

  document.querySelector('#c1_nib').value = dt.nib || '';

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

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

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

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

  let dt2=ar['relation'];

  let sel1_relation =``;

  for (let i=8;i<11;i++) {

    if(i==dt.relation) {

      sel1_relation +=`<option selected value="`+i+`">`+dt2[i]+`</option>`;

    } else {

      sel1_relation +=`<option value="`+i+`">`+dt2[i]+`</option>`;

    }

  }

  $$("#c1_relation").html(sel1_relation );

  

});



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

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

    e.preventDefault();

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

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

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

    const nib= $('#c1_nib').val();

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

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

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

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

    const relation= $('#c1_relation').val();

    const img_url= $('#c1_img_url').val();



    let req = {

      id,name,initial,nib,npwp,address,city,postcode,relation,img_url

    }

    if (id && name) {

      company_edit(req);

    } else {

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

    }

  });

})



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

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

  e.preventDefault();

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



    let req = {

      id

    }

    if (id) {

      company_delete(req);

      //console.log(req)

    } else {

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

    }    

  })

});