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

  console.log('app product_retur ready')

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

  var cat = e.detail.route.params.c;

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

  let dt= js.data.by_id[id];

  console.log(dt)

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

  document.querySelector('#p4_retur_cat').value = cat || '';

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

  document.querySelector('#p4_item_code').value = dt.item_code || '';

});



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

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

    e.preventDefault();

    let product_id = $('#p4_product_id').val();

    let retur_cat= $('#p4_retur_cat').val();

    let action_at = $('#p4_action_at').val();

    let qty = $('#p4_qty').val();

    let from_to = $('#p4_from_to').val();

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

    let price_per_unit = $('#p4_price_per_unit').val();

    let returid = $('#p4_returid').val();



    let req = {

      product_id,retur_cat,action_at,qty,from_to,invoice,price_per_unit,returid

    }

    if (product_id && qty && price_per_unit) {

      product_retur(req);

    } else {

      app.dialog.alert("All fields are required", "Product Retur Errors");

    }

  });

})