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

  console.log('app product_sell ready')

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

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

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

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

  console.log(dt)

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

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

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

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

  //document.querySelector('#p3_due_days').value = 0 || '';

});



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

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

    e.preventDefault();

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

    let cat= $('#p3_cat').val();

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

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

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

    let buyer = $('#p3_buyer').val();

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

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

    let sales = $('#p3_sales').val();

    let req = {

      product_id,cat,action_at,qty,due_days,buyer,invoice,price_per_unit,sales

    }

    if (product_id && action_at && qty && price_per_unit) {

      product_sell(req);

    } else {

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

    }

  });

})