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

  console.log('app account_edit ready')

  let user=get_localstorage('member');

  let dt=user.data;

  //console.log(dt)

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

  document.querySelector('#a2_no_telp').value = dt.no_telp || '';

  document.querySelector('#a2_first_name').value = dt.first_name || '';

  document.querySelector('#a2_last_name').value = dt.last_name || '';

  document.querySelector('#a2_kecamatan').value = dt.kecamatan || '';

  document.querySelector('#a2_kabupaten').value = dt.kabupaten || '';

  document.querySelector('#a2_kelurahan').value = dt.kelurahan || '';

  document.querySelector('#a2_alamat').value = dt.alamat || '';

  document.querySelector('#a2_kodepos').value = dt.kodepos || '';

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

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

  document.querySelector('#a2_dob').value = dt.dob || '';

  let t_gelar='';

  for(let c=1;c<5;c++) {

    if(dt.gelar==c) {

      t_gelar +=`<label class="radio" style="margin-left: 5px">

      <input value="`+c+`" type="radio" checked name="a2_gelar">

      <i class="icon-radio"></i>

    </label>`+ar.gelar[c];

    } else {

      t_gelar +=`<label class="radio" style="margin-left: 5px">

      <input value="`+c+`" type="radio" name="a2_gelar">

      <i class="icon-radio"></i>

    </label>`+ar.gelar[c];

    }

  }

  $("#r_gelar").html(t_gelar);

  let t_sex='';

  for(let c=1;c<3;c++) {

    if(dt.sex==c) {

      t_sex +=`<label class="radio" style="margin-left: 5px">

      <input value="`+c+`" type="radio" checked name="a2_sex">

      <i class="icon-radio"></i>

    </label>`+ar.sex[c];

    } else {

      t_sex +=`<label class="radio" style="margin-left: 5px">

      <input value="`+c+`" type="radio" name="a2_sex">

      <i class="icon-radio"></i>

    </label>`+ar.sex[c];

    }

  }

  $("#r_sex").html(t_sex);  

})



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

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

    e.preventDefault();

    let id = $('#a2_id').val();

    let no_telp = $('#a2_no_telp').val();

    let first_name = $('#a2_first_name').val();

    let last_name = $('#a2_last_name').val();

    let kecamatan = $('#a2_kecamatan').val();

    let kabupaten = $('#a2_kabupaten').val();

    let kelurahan = $('#a2_kelurahan').val();

    let alamat = $('#a2_alamat').val();

    let kodepos = $('#a2_kodepos').val();

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

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

    let dob = $('#a2_dob').val();

    let sex = $('input[name="a2_sex"]:checked').val();

    let gelar = $('input[name="a2_gelar"]:checked').val();

    let req = {

      id,no_telp,first_name,last_name,kecamatan,kabupaten,kelurahan,alamat,kodepos,rt,rw,dob,sex,gelar

    };

    if (!sex) {

      app.dialog.alert("Sex are required", "Edit Account Errors");

      return

    }

    if (!no_telp) {

      app.dialog.alert("Phone are required", "Edit Account Errors");

      return

    }

    if (!first_name) {

      app.dialog.alert("firstname are required", "Edit Account Errors");

      return

    }

    account_edit(req);

  });

});