$(document).ready(function () {
  $("body").off("click").on("click", ".remove", remove);
  $("body").on(
    "keyup blur focus",
    ".qty, .rate, .amount, .discperc, .discount, .vatperc, .vatamt, .total",
    calDetail
  );
  $(".tbody").on("keyup blur focus", ".amtoth", calDetailOth);
  // $("body").on("keyup blur focus", ".subtotal, .vattot, .totdisc, .billamt", calMain);
  $("body").on("keyup blur focus", ".ldiscperc", discount);
  $("body").on(
    "keyup blur focus",
    ".grossamt, .totdisc, .ldiscperc, .addpack, .addgst",
    finalCal
  );
  $("body").on("keyup", ".ssearch", getSearchData);
  $("body").on("click", ".sload", getSearchData);

  function calDetail() {
    var tr = $(this).closest("tr"),
      cartqty = tr.find(".cartqty").val(),
      qty = tr.find(".qty").val(),
      sqft = tr.find(".sqft").val(),
      calby = tr.find(".calby").val(),
      totqty = cartqty * qty,
      rate = tr.find(".rate").val(),
      discperc = tr.find(".discperc").val(),
      discamt = 0,
      vatperc = tr.find(".vatperc").val(),
      subtot = 0,
      total = 0,
      amount = 0,
      vat = 0;

    if (calby == "S") {
      amount = (rate * sqft).toFixed(2);
    } else {
      amount = (rate * qty).toFixed(2);
    }

    if (discperc > 0) {
      discamt = ((discperc * amount) / 100).toFixed(2);
    } else {
      discamt = tr.find(".discamt").val();
    }
    subtot = amount - discamt;
    if (vatperc > 0) {
      vat = ((vatperc * subtot) / 100).toFixed(2);
    } else {
      vat = 0;
    }
    total = (parseFloat(subtot) + parseFloat(vat)).toFixed(2);
    tr.find(".totqty").val(totqty);
    tr.find(".amount").val(amount);
    tr.find(".discamt").val(discamt);
    tr.find(".vat").val(vat);
    tr.find(".total").val(total);
    var totamt = 0,
      totdisc = 0,
      totvat = 0,
      tottotal = 0,
      totbal = 0,
      totalqty = 0,
      totcart = 0,
      totexp = 0,
      totadv = 0;
    $(".qty").each(function () {
      if (!isNaN(this.value) && this.value.length != 0) {
        totalqty += parseFloat(this.value);
      }
    });
    $(".totalqty").val(totalqty);

    $(".amount").each(function () {
      if (!isNaN(this.value) && this.value.lenght != 0) {
        tottotal += parseFloat(this.value);
      }
    });
    $(".grossamt").val(totamt);
  }
  function discount() {
    var tdiscperc = $(".ldiscperc").val();
    var grossamt = $(".grossamt").val();
    if (tdiscperc > 0) {
      discamt = ((tdiscperc * grossamt) / 100).toFixed(2);
    }
    $(".totdisc").val(discamt);
  }

  function finalCal() {
    var grossamt = $(".grossamt").val(),
      ldiscperc = $(".ldiscperc").val(),
      totdisc = $(".totdisc").val(),
      addpack = $(".addpack").val(),
      addgst = $(".addgst").val();
      totamt = parseInt(grossamt) - parseInt(totdisc) + parseInt(addpack) + parseInt(addgst);
    $(".tottotal").val(totamt);
  }

  function calDetailOth() {
    var tr = $(this).closest("tr"),
      amtoth = tr.find(".amtoth").val(),
      obillamt = 0;

    $(".amtoth").each(function () {
      if (!isNaN(this.value) && this.value.lenght != 0) {
        obillamt += parseFloat(this.value);
      }
    });
    $(".obillamt").val(obillamt);
  }

  $("#addnew").click(function () {
    AddRows();
  });
  $("#addnewoth").click(function () {
    AddRowsOth();
  });
  function remove() {
    $(this).closest("tr").remove();
  }

  function AddRows() {
    var ctr = $("#items").val();
    var hide_check = "";
    if ($("#del_all").prop("checked")) {
      hide_check = 0;
    } else {
      hide_check = 1;
    }
    var detail = ".fetchpart_" + ctr;
    $.post("goods_add.php", { ctr: ctr, hide: hide_check }, function (data) {
      $(data).appendTo(".tbody");
      ctr++;
      $("#items").val(ctr);
      $(detail).select2();
    });
  }

  function AddRowsOth() {
    var ctr = $("#items").val();
    var hide_check = "";
    if ($("#del_all").prop("checked")) {
      hide_check = 0;
    } else {
      hide_check = 1;
    }
    var detail = ".fetchpart_" + ctr;
    $.post("other_add.php", { ctr: ctr, hide: hide_check }, function (data) {
      $(data).appendTo(".tbody");
      ctr++;
      $("#items").val(ctr);
      $(detail).select2();
    });
  }

  //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
  $(".snoRecord").hide();

  $(".searchModal").on("shown.bs.modal", function () {
    $(".ssearch").focus();
    $(".ssearch").val("");
    $(".sfield").val("1");
    getSearchData();
  });

  var smrangeFrom = 0,
    smrangeTo = 40;

  function getSearchData(pagination) {
    $(".sloader").show();

    if (pagination == "prev") {
      if (smrangeFrom > 0) {
        smrangeFrom = smrangeFrom - 40;
        smrangeTo = smrangeTo - 40;
      }
    }
    if (pagination == "next") {
      smrangeFrom = smrangeFrom + 40;
      smrangeTo = smrangeTo + 40;
    }

    var ssearch = $(".ssearch").val(),
      sfield = $(".sfield").val(),
      ioperator = $(".soperator").val(),
      data = {
        ssearch: ssearch,
        sfield: sfield,
        ioperator: ioperator,
        rangeFrom: smrangeFrom,
        rangeTo: smrangeTo,
      },
      values = "";
    $.ajax({
      type: "GET",
      dataType: "json",
      url: "debnote_search.php",
      data: data,
      success: function (response) {
        $.each(response, function () {
          values +=
            "<tr class='searchTr'>" +
            "<td align='left'>" +
            this.vouchno +
            "</td>" +
            "<td align='left'>" +
            this.name +
            "</td>" +
            "<td align='left'>" +
            this.billno +
            "</td>" +
            "<td align='left'>" +
            this.vouchdt +
            "</td>" +
            "<td align='left'>" +
            this.nettotal +
            "</td>" +
            "<td align='left'>" +
            this.rem1 +
            "</td>" +
            "<td align='left'><a href='" +
            this.action +
            "'><i class='fa fa-pencil' style='font-size:1.3em;cursor:pointer;'></i></a></td>" +
            "</tr>";
        });
        $(".sloader").hide();
        $(".sTbody").empty();
        $(".sTbody").html(values);
        if (response) {
          $(".search-action-bar").show();
        }
      },
      error: function (request, textStatus, errorThrown) {},
    });
  }
  $("#search").click(function () {
    getSearchData();
  });
  //    $(".ssearch").change(function () {
  //        getSearchData();
  //    });
  $(".sprevious").click(function () {
    getSearchData("prev");
  });
  $(".snext").click(function () {
    getSearchData("next");
  });

  //////////////////////////////////////////////Search Part - End  //////////////////////////////////////////////
});
