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

  console.log('app action_edit ready')

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

  //console.log(aid)

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

  let dt = js.data.byid[i];  

  console.log(dt)

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

  document.querySelector('#a2_descr').value = dt.descr || '';

  document.querySelector('#a2_invoice').value = dt.invoice || '';

  document.querySelector('#a2_action_at').value = dt.act_date || '';

  //document.querySelector('#a2_bank').value = dt.bank || '';

  document.querySelector('#a2_amount').value = dt.amount || '';

  document.querySelector('#a2_pay_to').value = dt.pay_to || '';

  document.querySelector('#a2_pay_from').value = dt.pay_from || '';

  let a2_menu=`

  <a href="/image_upload/action/`+dt.id+`" data-image="`+dt.image+`" class="btn btn-xs btn-primary image_upload">Image</a>

  <!--<a href="/transaction_add/`+dt.id+`" class="btn btn-xs btn-success">Posting</a>-->  

`;

$$("#a2_topmenu").html(a2_menu);

});



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

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

    e.preventDefault();

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

    let descr = $('#a2_descr').val();

    let invoice = $('#a2_invoice').val();

    let req = {

      id,descr,invoice

    }

    if (id,descr) {

      action_edit(req);

    } else {

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

    }

  });

})



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

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

  e.preventDefault();

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



    let req = {

      id

    }

    if (id) {

      action_delete(req);

      //console.log(req)

    } else {

      app.dialog.alert("All fields are required", "Action transaction delete errors");

    }    

  })

});