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

  console.log('app address_edit ready')

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

  //console.log(aid)

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

  let dt = js.data.address;  

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

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

      document.querySelector('#a2_label').value = dt[i].label || '';

      document.querySelector('#a2_name').value = dt[i].name || '';

      document.querySelector('#a2_phone').value = dt[i].phone || '';

      document.querySelector('#a2_address').value = dt[i].address || '';

      document.querySelector('#a2_rt').value = dt[i].rt || '';

      document.querySelector('#a2_rw').value = dt[i].rw || '';

      document.querySelector('#a2_no').value = dt[i].no || '';

      document.querySelector('#a2_postcode').value = dt[i].postcode || '';

      document.querySelector('#a2_kel').value = dt[i].kel || '';

      document.querySelector('#a2_kec').value = dt[i].kec || '';

      document.querySelector('#a2_city').value = dt[i].city || '';

      document.querySelector('#a2_gmap').value = dt[i].gmap || '';

      document.querySelector('#a2_remark').value = dt[i].remark || '';

      document.querySelector('#a2_id').value = dt[i].id || '';

    }

  }

});



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

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

    e.preventDefault();

    let user = JSON.parse(localStorage.getItem('t01_user'));

    const label = $('#a2_label').val();

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

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

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

    const rt = $('#a2_rt').val();

    const rw = $('#a2_rw').val();

    const no = $('#a2_no').val();

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

    const kel = $('#a2_kel').val();

    const kec = $('#a2_kec').val();

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

    const gmap = $('#a2_gmap').val();

    const remark = $('#a2_remark').val();

    const aid = $('#a2_id').val();

    const token=user.sess_token;

    const uuid=user.uuid;

    let req = {

      uuid,token,label,name,phone,address,rt,rw,no,postcode,kel,kec,city,gmap,remark,aid

    }

    if (name && phone && address && no && gmap) {

      address_edit(req);

    } else {

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

    }

  });

})