$(document).ready(function () {
    $("body").on("click", ".clickable-row", clickRow);
    $(".searchModal").on("click", ".clickable-row", clickRow);
    $("body").on("keyup", ".ssearch", getSearchData);
    $("body").on("change", ".soperator", getSearchData);
    $("body").on("click", ".sload", getSearchData);
    $("body").on("click", "#search", getSearchData);
    $("body").on("change", "#origin", getMainData);

    getSearchData();
    getMainData();

    function getMainData() {
        var origin = $(".origin").val();

        if (origin != "") {
            $.post("get_shipmentstatus.php", { origin: origin },
                function (data) {
                    if (data != "") {
                        $(".origin").focus();
                        $(".dataDiv").html(data);
                        getSearchData();
                    }
                }
            );
        } else {
            $(".dataDiv").html("");
        }

    }

    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    function clickRow() {
        window.document.location = $(this).data("href");
    }
    $(".snoRecord").hide();
    $(".sloader").hide();

    var imrangeFrom = 0,
        imrangeTo = 15;

    $(".ssearch").focus();
    //    $(".ssearch").val("");
    //    $(".sfield").val("1");
    getSearchData();

    function getSearchData(pagination) {

        $(".sloader").show();

        if (pagination == 'prev') {
            if (imrangeFrom > 0) {
                imrangeFrom = (imrangeFrom - 15);
                imrangeTo = (imrangeTo - 15);
            }
        }
        if (pagination == 'next') {
            imrangeFrom = (imrangeFrom + 15);
            imrangeTo = (imrangeTo + 15);
        }
 

        var origin = $(".origin").val(),
            type = $(".type").val(),
            ssearch = $(".ssearch").val(),
            sfield = $(".sfield").val(),
            sortBy = $(".sortBy").val(),
            orderAs = $(".orderAs").val(),
            soperator = $(".soperator").val(),
            data = {
                ssearch: ssearch, sfield: sfield, sortBy: sortBy, orderAs: orderAs, soperator: soperator,
                rangeFrom: imrangeFrom, rangeTo: imrangeTo, type: type, origin: origin
            },
            values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "shipmentstatus_search.php",
            data: data,
            success: function (response) {

                if (response == "") {
                    $(".snoRecord").show();
                    $(".snext").hide();
                    $(".ssload").hide();
                } else {
                    $(".snext").show();
                    $(".snoRecord").hide();
                    $(".ssload").show();
       
                    let totalCBM = 0;
                    let totalWeight = 0;
                    let totalPCS = 0;

                    $.each(response, function () {

                        totalCBM += parseFloat(this.totcbm) || 0;
                        totalWeight += parseFloat(this.weight) || 0;
                        totalPCS += parseFloat(this.pcs) || 0;
                        
                        values += "<tr class='clickable-row2' data-href='" + this.action + "' style='" + this.bgcolr + "'>";

                        values += "<td align='center'><b>" + this.vouchno + "</b></td>" +

                            "<td align='center'><b>" + this.vouchdt + "</b></td>" +
                            "<td align='left'><b>" + this.status + "</b></td>" +
                            "<td align='left'>" + 
                            "<a id='whats' class='btn btn-pad btn-square' style='background-color:green;margin-top: 5px;' " + 
                            "href='" + this.whatsapppdf + "&para=whats' target='_blank'>" +
                            "<i class='fa fa-whatsapp' style='color:#ffffff'></i></a></td>" +
                            "<td align='left'>" + this.customer + "</td>" +
                            "<td align='center'>" + this.nocart + "</td>" +
                            "<td align='left'>" + this.supplier + "</td>" +
                            "<td align='left'>" + this.description + "</td>" +
                            "<td align='center'>" + this.pcs + "</td>" +
                            "<td align='center'>" + this.unit + "</td>" +
                            "<td align='center'><b>" + this.marka + "</b></td>" +
                            "<td align='right'>" + this.totcbm + "</td>" +
                            "<td align='right'>" + this.weight + "</td>" +
                            "<td align='left'><b>" + this.containerno + "</b></td>" +
                            "<td align='center'>" + this.saildate + "</td>" +
                            "<td align='left'>" + this.destination + "</td>" +
                            "<td align='center'>" + this.estimdate + "</td>" +
                            "<td align='center'>" + this.delvdate + "</td>" +
                            "<td align='left'>" + this.mobile + "</td>" +
                            "</tr>";

                        });

                        // Add total row
                        let totalRow = `
                        <tr style="background:#e8e8e8; font-weight:bold;">
                        <td colspan="8" align="right">TOTAL</td>
                        <td align="center">${totalPCS}</td>
                        <td></td>
                        <td></td>
                        <td align="right">${totalCBM.toFixed(2)}</td>
                        <td align="right">${totalWeight.toFixed(2)}</td>
                        <td colspan="6"></td>
                        </tr>`;
                 

                    $(".sloader").hide();
                    $(".sTbody").empty();
                    $(".sTbody").html(values);
                    $(".sTotalFoot").html(totalRow);
                    if (response) {
                        $(".search-action-bar").show();
                    }
                }
            },
            error: function (request, textStatus, errorThrown) {

            }
        });
    }
    $("#search").click(function () {
        $(".searchModal").on('shown.bs.modal', function () {
            $(".ssearch").focus();
            $(".ssearch").val("");
            $(".sfield").val("8");
            getSearchData();
        });

        getSearchData();
    });
    $(".sprev").click(function () {
        getSearchData('prev');
    });
    $(".snext").click(function () {
        getSearchData('next');
    });


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