$(document).ready(function () {
  $("#showcredit").hide();
  $("#ticketselect").on("change", function () {
    $(".creditrow").hide();
    $("#emptyweightadd").val("");
    $("#loadweightadd").val("");
    $("#totalamtadd").val("");
    $("#netqtyadd").val("");
    $("#netweightadd").val("");
    $("#updatebut").prop("disabled", false);
    var ticket = $(this).val();
    // ajax for vehicle
    $.ajax({
      type: "POST",
      url: "ajaxpath/firsttransvehno.php",
      data: { ticket: ticket },
      success: function (a) {
        //  alert(a);
        // $("#showquantity-with-color").css("display","block");
        $("#vehicleno").val(a);
        //alert(a);
      },
    });
    // ajax for vehicle end
    // ajax for totaldiv
    $.ajax({
      type: "POST",
      url: "ajaxpath/secondtrans_customer.php",
      data: { ticket: ticket },
      success: function (a) {
        //  alert(a);
        // $("#showquantity-with-color").css("display","block");
        $("#customeradd").html(a);
        $("#customeraddori").html(a);
        //alert(a);
      },
    });
    // ajax for totaldiv
    // ajax for vehiclename
    $.ajax({
      type: "POST",
      url: "ajaxpath/firsttrans_vehiclename.php",
      data: { ticket: ticket },
      success: function (a) {
        //  alert(a);
        // $("#showquantity-with-color").css("display","block");
        $("#vehiclenameadd").val(a);
        //alert(a);
      },
    });
    // ajax for vehiclename
    // ajax for vehiclename
    $.ajax({
      type: "POST",
      url: "ajaxpath/secondtrans_loadtype.php",
      data: { ticket: ticket },
      success: function (a) {
        //  alert(a);
        // $("#showquantity-with-color").css("display","block");
        $("#salestype").val(a);
        //alert(a);
      },
    });
    // ajax for vehiclename
    // ajax for weiugthname
    $.ajax({
      type: "POST",
      url: "ajaxpath/firsttrans_weight.php",
      data: { ticket: ticket },
      success: function (a) {
        //  alert(a);
        // $("#showquantity-with-color").css("display","block");
        $("#weightadd").html(a);
        //alert(a);
      },
    });
    // ajax for weigthname
    // ajax for weiugthname
    $(".creditrow").hide();
    $.ajax({
      type: "POST",
      url: "ajaxpath/secondtrans_salestype.php",
      data: { ticket: ticket },
      success: function (a) {
        //  alert(a);
        // $("#showquantity-with-color").css("display","block");
        $("#customertype").val(a);
        //display
        var balance = $("#customertype").val();
        //alert(a);
        $(".creditrow").show();
        $("#showcredit").show();
        //   if (balance == 'cash')
        //   {
        //     $(".creditrow").hide();
        //     $("#showcredit").hide();
        //   }
        //   else
        //   {
        //     $(".creditrow").show();
        //     $("#showcredit").show();
        //   }
        //display
        //   creditlimit
        //    ajax for creditlimit

        var customer = $("#customeradd").val();
        //alert (customer);
        $.ajax({
          type: "POST",
          url: "ajaxpath/creditbalnce.php",
          data: { customer: customer },
          success: function (a) {
            //  alert(a);
            // $("#showquantity-with-color").css("display","block");
            $("#creditadd").val(a);

            //alert(a);
          },
        });
        //   ajax for creditlimit end
        //creditlimit
      },
    });
    // ajax for weigthname
    // ajax for product
    $.ajax({
      type: "POST",
      url: "ajaxpath/firsttrans_productname.php",
      data: { ticket: ticket },
      success: function (a) {
        //  alert(a);
        // $("#showquantity-with-color").css("display","block");
        //$("#productadd").html(a);
        // alert(a);
      },
    });
    // ajax for product
    // ajax for productcode
    $.ajax({
      type: "POST",
      url: "ajaxpath/firsttrans_productcode.php",
      data: { ticket: ticket },
      success: function (a) {
        //  alert(a);
        // $("#showquantity-with-color").css("display","block");
        $("#productadddup").val(a);
        $("#productadddori").val(a);
        //alert(a);
      },
    });
    // ajax for productcode
    // ajax for customercode
    $.ajax({
      type: "POST",
      url: "ajaxpath/firsttrans_customer.php",
      data: { ticket: ticket },
      success: function (a) {
        //  alert(a);
        // $("#showquantity-with-color").css("display","block");
        //alert(a);
        $("#customeraddori").val(a);
        //alert(a);
      },
    });
    // ajax for productcode

    // ajax for productname
    $.ajax({
      type: "POST",
      url: "ajaxpath/secondtrans_product.php",
      data: { ticket: ticket },
      success: function (a) {
        //  alert(a);
        // $("#showquantity-with-color").css("display","block");
        $("#productadd").html(a);
        var product = $("#productadd").val();
        $.ajax({
          type: "POST",
          url: "ajaxpath/secondtrans_productname.php",
          data: { product: product },
          success: function (a) {
            //  alert(a);
            // $("#showquantity-with-color").css("display","block");
            $("#productnameadd").val(a);
            //alert(a);
          },
        });
        //alert(a);
        var cus = $("#customeradd").val();
        var product = $("#productadd").val();
        //alert (product);
        $.ajax({
          type: "POST",
          url: "ajaxpath/secondtransprice.php",
          data: { cus: cus, product: product },
          success: function (b) {
            //  alert(a);
            // $("#showquantity-with-color").css("display","block");
            $("#priceadd").val(b);
            $("#priceaddori").val(b);
            //alert(b);
          },
        });
        // ajax for productname

        //    product
        // ajax unit
        // ajax for unit
        var product = $("#productadd").val();
        $.ajax({
          type: "POST",
          url: "ajaxpath/secondtrans_unit.php",
          data: { product: product },
          success: function (c) {
            //  alert(a);
            // $("#showquantity-with-color").css("display","block");
            $("#unitconversion").val(c);
            //alert(c);
            var price = $("#priceadd").val();
            var product = $("#productnameadd").val();
            $(".productdisplay").html(product);
            $(".pricedisplay").html(price);
          },
        });
        // ajax for unit
        // unit ajax
        // ajax for productnamename
      },
    });
    // ajax for productname

    // ajax for estimated quantity

    $.ajax({
      type: "POST",
      url: "ajaxpath/firsttrans_estqty.php",
      data: { ticket: ticket },
      success: function (a) {
        //  alert(a);
        // $("#showquantity-with-color").css("display","block");
        $("#estqtyadd").val(a);
        //alert(a);
      },
    });
    // ajax for estimated quantity
    // ajax for delivery

    $.ajax({
      type: "POST",
      url: "ajaxpath/secondtrans_delivery.php",
      data: { ticket: ticket },
      success: function (a) {
        //  alert(a);
        // $("#showquantity-with-color").css("display","block");
        $("#deliveryadd").val(a);
        $("#deliveryaddori").val(a);
        // alert(a);
      },
    });
    // ajax for Delivery
    // ajax for emptyweight

    $.ajax({
      type: "POST",
      url: "ajaxpath/secondtrans_weight.php",
      data: { ticket: ticket },
      success: function (a) {
        //  alert(a);
        // $("#showquantity-with-color").css("display","block");
        $("#emptyweightadd").val(a);
        // alert(a);
      },
    });

    // ajax for emptyweighht
    // ajax for tripsheet
    $.ajax({
      type: "POST",
      url: "ajaxpath/firsttrans_tripsheet.php",
      data: { ticket: ticket },
      success: function (a) {
        //  alert(a);
        // $("#showquantity-with-color").css("display","block");
        $("#tripsheetadd").val(a);
        //alert(a);
        $("#remarkadd").css("border", "1px solid red");
        $("#getweightadd").css("border", "1px solid red");
        $("#remarkadd").css(
          "box-shadow",
          "2px 1px 3px 1px rgba(241, 70, 70, 0.979)"
        );
        $("#getweightadd").css(
          "box-shadow",
          "2px 1px 3px 1px rgba(241, 70, 70, 0.979)"
        );
      },
    });
    $("#priceshowbutton").click();
    // ajax for tripsheet
  });
  //query to color
  $("#getweightadd").focus(function () {
    $(this).css("border", "1px solid skyblue");
    $(this).css("box-shadow", "2px 1px 3px 1px skyblue");
  });
  $("#remarkadd").focus(function () {
    $(this).css("border", "1px solid skyblue");
    $(this).css("box-shadow", "2px 1px 3px 1px skyblue");
  });
  //queryt to color
  //query to add weight
});
$(document).ready(function () {
  //alert('hai');
  $("#getweightbutton").click(function () {
    var query = 1;
    $.ajax({
      url: "ajaxpath/search1.php",
      method: "POST",
      data: { query: query },
      success: function (data) {
        $("#loadweightadd").val("");
        $("#loadweightadd").val(data);
        var empty = $("#emptyweightadd").val();
        var load = $("#loadweightadd").val();
        var netweight = load - empty;
        if (netweight < 0) {
          var netweightt = empty - load;
        } else {
          var netweightt = load - empty;
        }
        // alert('hai');
        $("#getweightadd").css("border", "1px solid red");
        $("#getweightadd").css("box-shadow", "2px 1px 3px 1px skyblue");
        $("#netweightadd").val(netweightt);
        var nettwe = $("#netweightadd").val();
        var unitcon = $("#unitconversion").val();
        var finaluni = nettwe / unitcon;

        $("#netqtyadd").val(finaluni.toFixed(2));
        $("#netqtyori").val(finaluni.toFixed(2));
        var unitco = $("#netqtyadd").val();
        var price = $("#priceadd").val();
        var amount = unitco * price;
        $("#totalamtadd").val(amount);
        $("#submitbut").prop("disabled", false);
      },
    });

    //
  });
  // unitkeyup
  $("#netqtyadd").keyup(function () {
    $("#updatebut").prop("disabled", false);
    var netqty = $(this).val();
    var price = $("#priceadd").val();
    var amount = netqty * price;
    $("#totalamtadd").val(amount);
    //alert(amount);
  });
  // unitkeyup
  // pricekeyup
  $("#priceadd").keyup(function () {
    $("#updatebut").prop("disabled", false);
    var netqty = $("#netqtyadd").val();
    if (netqty == "") {
      var price = 0;
    } else {
      var price = $("#priceadd").val();
    }

    var amount = netqty * price;
    $("#totalamtadd").val(amount);
    //alert(amount);
  });
  // pricekeyup
});

$(document).ready(function () {
  // ajax for product change
  $("#productadd").on("change", function () {
    var product = $(this).val();
    var procode = $("#productadddup").val();
    //alert(product);
    var product = $("#productadd").val();
    $.ajax({
      type: "POST",
      url: "ajaxpath/secondtrans_productname.php",
      data: { product: product },
      success: function (a) {
        //  alert(a);
        // $("#showquantity-with-color").css("display","block");
        //  price
        $("#productnameadd").val(a);
        //alert(a);
        var cus = $("#customeradd").val();
        var product = $("#productadd").val();
        //alert (product);
        $.ajax({
          type: "POST",
          url: "ajaxpath/secondtransprice.php",
          data: { cus: cus, product: product },
          success: function (b) {
            //  alert(a);
            // $("#showquantity-with-color").css("display","block");
            $("#priceadd").val(b);
            $("#priceaddori").val(b);
            // alert(b);
            //unit

            var product = $("#productadd").val();
            $.ajax({
              type: "POST",
              url: "ajaxpath/secondtrans_unit.php",
              data: { product: product },
              success: function (c) {
                //  alert(a);
                // $("#showquantity-with-color").css("display","block");
                $("#unitconversion").val(c);
                //alert(c);
                var price = $("#priceadd").val();
                var product = $("#productnameadd").val();
                $(".productdisplay").html(product);
                $(".pricedisplay").html(price);
                $("#priceshowbutton").click();
                //   $("#getweightbutton").click();
                var empty = $("#emptyweightadd").val();
                var load = $("#loadweightadd").val();
                //alert(empty);
                var netweight = load - empty;
                //alert(netweight);

                $("#netweightadd").val(netweight);
                var nettwe = $("#netweightadd").val();
                var unitcon = $("#unitconversion").val();
                var finaluni = nettwe / unitcon;

                $("#netqtyadd").val(finaluni.toFixed(2));
                $("#netqtyori").val(finaluni.toFixed(2));
                var unitco = $("#netqtyadd").val();
                var price = $("#priceadd").val();
                var amount = unitco * price;
                $("#totalamtadd").val(amount);
              },
            });
            //unit
          },
        });
        // price
        // ajax for productcode
        var product = $("#productadd").val();
        $.ajax({
          type: "POST",
          url: "ajaxpath/secondtrans_productcode.php",
          data: { product: product },
          success: function (a) {
            //  alert(a);
            // $("#showquantity-with-color").css("display","block");
            $("#productadddup").val(a);
            //alert(a);
          },
        });
        // ajax for productcode
        //productcode

        var product = $("#productadd").val();
        var productcode = $("#productadddup").val();
        var ticket = $("#ticketselect").val();
        if (product != productcode) {
          // alert(ticket);
          // $.ajax({
          //     type:'POST',
          //     url:'ajaxpath/secondtrans_unit.php',
          //     data:{product:product},
          //     success:function(c){
          //     //  alert(a);
          //      // $("#showquantity-with-color").css("display","block");
          //       $("#unitconversion").val(c);
          //       //alert(c);
          //       var price = $("#priceadd").val();
          //       var product = $("#productnameadd").val();
          //      $(".productdisplay").html(product);
          //      $(".pricedisplay").html(price);
          //       $("#priceshowbutton").click();
          //    }
          //                 });
        } else {
        }

        //productcode
      },
    });
    //ajax for productname
  });
});

// credit calculation
$(document).ready(function () {
  $(".totalamtadd").change(function () {
    //alert('hai');
    $("#updatebut").prop("disabled", false);
  });
  $(".alertbutton").hide();
  $("#submitbut").mouseover(function () {
    //alert('g');
    //    ajax for bill type
    var total = $("#totalamtadd").val();
    var customer = $("#customeradd").val();
    var oldtotal = $("#oldtotalamtadd").val();
    //alert(oldtotal);
    $.ajax({
      type: "POST",
      url: "ajaxpath/billtypefetch.php",
      data: { customer: customer, total: total, oldtotal: oldtotal },
      success: function (a) {
        //  alert(a);
        // $("#showquantity-with-color").css("display","block");
        $("#billtypeedit").val(a);
        //alert(a);
      },
    });
    //   ajax for billtype end
    var balance = $("#customertype").val();
    if (balance != "cash") {
      var credit = $("#creditadd").val();
      var total = $("#totalamtadd").val();
      var finalamt = credit - total;
      //alert()
      //alert(finalamt);
      if (finalamt <= 0) {
        // $(".alertbutton").click();
        //$("#updatebut").prop('disabled', true);
      } else {
        // $("#updatebut").prop('disabled', false);
      }
    }
  });
  $("#submitbut").keyup(function () {
    //    ajax for bill type
    var total = $("#totalamtadd").val();
    var customer = $("#customeradd").val();
    var oldtotal = $("#oldtotalamtadd").val();
    $.ajax({
      type: "POST",
      url: "ajaxpath/billtypefetch.php",
      data: { customer: customer, total: total, oldtotal: oldtotal },
      success: function (a) {
        //  alert(a);
        // $("#showquantity-with-color").css("display","block");
        $("#billtypeedit").val(a);
        //alert(a);
      },
    });
    //   ajax for billtype end
    if (balance != "cash") {
      var credit = $("#creditadd").val();
      var total = $(".totalamtedit").val();
      var finalamt = credit - total;
      //alert(finalamt);
      if (finalamt <= 0) {
        $(".alertbutton").click();
        $("#updatebut").prop("disabled", true);
      } else {
        //$("#updatebut").prop('disabled', false);
      }
    }
  });
});
// creditcalculation
// customer
//LOADWEIGHT CALCULATION
$(document).ready(function () {
  $("#loadweightadd").keyup(function () {
    var load = $(this).val();
    var empty = $("#emptyweightadd").val();
    // var load = $("#loadweightadd").val();
    var netweight = load - empty;
    //alert(netweight);
    if (netweight < 0) {
      var netweightt = empty - load;
    } else {
      var netweightt = load - empty;
    }
    $("#netweightadd").val(netweightt);

    var nettwe = $("#netweightadd").val();
    var unitcon = $("#unitconversion").val();
    var finaluni = nettwe / unitcon;

    $("#netqtyadd").val(finaluni.toFixed(2));
    $("#netqtyori").val(finaluni.toFixed(2));
    var unitco = $("#netqtyadd").val();
    var price = $("#priceadd").val();
    var amount = unitco * price;
    $("#totalamtadd").val(amount);
  });
});

// LOADWEIGHTCALCULATION
//EMPTYWEIGHT CALCULATION
$(document).ready(function () {
  $("#emptyweightadd").keyup(function () {
    var load = $("#loadweightadd").val();
    var empty = $("#emptyweightadd").val();
    // var load = $("#loadweightadd").val();
    var netweight = load - empty;
    //alert(netweight);
    if (netweight < 0) {
      var netweightt = empty - load;
    } else {
      var netweightt = load - empty;
    }
    $("#netweightadd").val(netweightt);
    var nettwe = $("#netweightadd").val();
    var unitcon = $("#unitconversion").val();
    var finaluni = nettwe / unitcon;

    $("#netqtyadd").val(finaluni.toFixed(2));
    $("#netqtyori").val(finaluni.toFixed(2));
    var unitco = $("#netqtyadd").val();
    var price = $("#priceadd").val();
    var amount = unitco * price;
    $("#totalamtadd").val(amount);
  });
});

// EMPTYWEIGHTCALCULATION

$(document).ready(function () {
  $("#submitbut").mouseover(function () {
    // alert('hai');
    var empty = $("#emptyweightadd").val();
    if (empty != "") {
      var empty = $("#emptyweightadd").val();
      var load = $("#loadweightadd").val();
      //alert(empty);
      var netweight = load - empty;
      //alert(netweight);
      if (netweight < 0) {
        var netweightt = empty - load;
        $("#emptyweightadd").val(load);
        $("#loadweightadd").val(empty);
      } else {
        var netweightt = load - empty;
        $("#emptyweightadd").val(empty);
        $("#loadweightadd").val(load);
      }
    }
  });
});

$(document).ready(function () {
  $("#submitbut").keyup(function () {
    // alert('hai');
    var empty = $("#emptyweightadd").val();
    if (empty != "") {
      var empty = $("#emptyweightadd").val();
      var load = $("#loadweightadd").val();
      //alert(empty);
      var netweight = load - empty;
      //alert(netweight);
      if (netweight < 0) {
        var netweightt = empty - load;
        $("#emptyweightadd").val(load);
        $("#loadweightadd").val(empty);
      } else {
        var netweightt = load - empty;
        $("#emptyweightadd").val(empty);
        $("#loadweightadd").val(load);
      }
    }
  });
});
$(document).ready(function () {
  $("#ticketFetch").keyup(function (e) {
    var tkt = $("#ticketFetch").val();
    var print = $("#print").html();
    var edit = $("#edit").html();
    var purchasedetails = "purchasedetails";
    if (e.keyCode == 13) {
      $.ajax({
        type: "POST",
        dataType: "json",
        url: "ajaxpath/tktfetch.php",
        data: { tkt: tkt },
        success: function (data) {
          console.log(typeof data);
          console.log(data);
          $("#tableTbody").html("");
          data.map((data) => {
            tablerow = `<td>${data.editdate}</td>
              <td>${data.edittime}</td>
              <td>${data.ticket}</td>
          <td>${data.cusname}</td>
          <td>${data.productname}</td>
          <td>${data.netweight}</td>
          <td>${data.totalamount}</td>
          ${
            print == 1
              ? `<td><a href="salesbillslip.php?print=${
                  data.salescode
                } && table=${
                  data.tablename
                }" target="_blank"><i class="fas fa-print"></i></a></td><?php }?>
          <?php if($per['dataprint'] =='1'){?> <td><a href="salesbillall.php?print=${
            data.salescode
          } && table=${data.tablename}" target="_blank">${
                  data.tablename != purchasedetails
                    ? '<i class="fas fa-print"></i>'
                    : ""
                }</a></td>`
              : ""
          }
          ${
            edit == 1
              ? `<td><a href="secondtransactionedit.php?edi=${data.ticket}&&table=${data.tablename}"><i class="fas fa-edit edit"></i></a></td>`
              : ""
          }    `;
          });
          $("#tableTbody").append(tablerow);
        },
      });
    }
  });
});
