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

  console.log('app inventory_edit ready')

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

  console.log(i);

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

  let dt= js.data.result.inventory.data[i];

  console.log(dt)

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

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

  document.querySelector('#i1_brand').value = dt.brand || '';

  document.querySelector('#i1_unit').value = dt.unit || '';

  document.querySelector('#i1_qty').value = dt.qty|| '';

  document.querySelector('#i1_remark').value = dt.remark || '';

  document.querySelector('#i1_will_age').value = dt.will_age|| '';

  document.querySelector('#i1_price_per_unit').value = dt.price_per_unit || '';

});



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

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

    e.preventDefault();

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

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

    const brand= $('#i1_brand').val();

    const unit= $('#i1_unit').val();

    const qty= $('#i1_qty').val();

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

    const will_age= $('#i1_will_age').val();

    const price_per_unit= $('#i1_price_per_unit').val();



    let req = {

      id,name,brand,unit,qty,remark,will_age,price_per_unit

    }

    if (id && price_per_unit) {

      inventory_edit(req);

    } else {

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

    }

  });

})