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

  console.log('app product_buy ready')

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

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

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

  console.log(dt);

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

});



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

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

    e.preventDefault();

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

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

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

    let due_days = $('#p1_due_days').val();

    let pay_from = $('#p1_pay_from').val();

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

    let remark= $('#p1_remark').val();



    let req = {

      product_id,action_at,qty,due_days,pay_from,invoice,remark

    }

    if (product_id && action_at && qty) {

      product_buy(req);

    } else {

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

    }

  });

})