$(document).ready(function () {
    $("body").on('click', '.proceed', fetchVal);
    $("body").on('click', '.saveChRate', saveFetch);
    $("body").on("keyup", ".ssearch2", getSearchData2);
    $("body").on("click", ".sload2", getSearchData2);
    $("body").on("change", ".orderAs2, .sortBy2", getSearchData2);

    $(".fetchPart").hide();
    $(".searchtab").hide();
    $(".exitChRate").click(function () {
        $(".getPart").show();
        $(".fetchPart").hide();
        $(".searchtab").hide();
    });

//    $('.excel').click(function () {
//        var company = $(".company").val();
//        var itemgrp = $("#itemgrp").val();
//        $(this).attr('href','chrate_excel.php?company='+company+'&itemgrp='+itemgrp);
//    });



    function fetchVal() {
        var itemgrp = $(".itemgrp").val(),
                fetch = "";

        $.ajax({
            type: "POST",
            datatype: "json",
            data: {itemgrp: itemgrp},
            url: "openstksrch.php",
            success: function (response) {
                if (response != '') {
                    $.each(response, function () {

                        fetch += "<tr>" +
                                "<td align='left'>" + this.ctr + "</td>" +
                                "<td align='left'>" + this.name + "</td>" +
                                "<td align='left'><input type='text' class='form-control  barcode' value='" + this.barcode + "' readonly></td>" +
                                "<td align='left'>" + this.gram + "</td>" +
                                "<td align='left'>" + this.packing + "</td>" +
//                                "<td align='left'>" + this.colour + "</td>" +
//                                "<input type='hidden' class='code' value='" + this.unit + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right gstperc' value='" + this.gstperc + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right cost' value='" + this.cost + "' ></td>" +
                                "<td align='left'><input type='text' class='form-control text-right rate' value='" + this.rate + "' ></td>" +
                                "<td align='left'><input type='text' class='form-control text-right mrp' value='" + this.mrp + "' ></td>" +
                                "<td align='left'><input type='text' class='form-control text-right msp' value='" + this.msp + "' ></td>" +
                                "</tr>";
                    });
                    $(".fetchData").html(fetch);
                    $(".searchmod").modal('show');
                    $(".fetchPart").show();
                    $('.search').show();
                    $(".getPart").hide();
                } else {
                    alert("No Records Found");
                }
            },
            error: function (request, errorThrown, textStatus) {
                console.log(errorThrown);
            }
        });
    }


    function saveFetch() {
        var chrate = [],
                myray = {};
        $(".barcode").each(function () {
            var tr = $(this).closest("tr"),
                    barcode = tr.find(".barcode").val(),
                    gstperc = tr.find(".gstperc").val(),
                    cost = tr.find(".cost").val(),
                    rate = tr.find(".rate").val(),
                    mrp = tr.find(".mrp").val(),
                    msp = tr.find(".msp").val();

            myray = {barcode: barcode, gstperc: gstperc, cost: cost, rate: rate, mrp: mrp, msp: msp};
            chrate.push(myray);
        });
        var count = chrate.length,
                data = {chrate: chrate, count: count};
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: "saveChRate.php",
            data: data,
            success: function (response) {
                if (response.success == true) {
                    alert("Records Updated");
                } else {
                    alert("Error");
                }
            },
            error: function (textStatus, request, errorThrown) {

            }
        });
    }

    $('.search').click(function () {
        $('.searchtab').show();
        $('.search').hide();

    });

    function getSearchData2() {
        var ssearch = $(".ssearch2").val(),
                sfield = $(".sfield2").val(),
                sortBy = $(".sortBy2").val(),
                orderAs = $(".orderAs2").val(),
                soperator = $(".soperator2").val(),
                itemgrp = $(".itemgrp").val(),
                data = {ssearch: ssearch, sfield: sfield, sortBy: sortBy, orderAs: orderAs, soperator: soperator, itemgrp: itemgrp},
                values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "chrate_srch.php",
            data: data,
            success: function (response) {
                $.each(response, function () {

                    values += "<tr>" +
                            "<td align='left'>" + this.ctr + "</td>" +
                            "<td align='left'>" + this.name + "</td>" +
                            "<td align='left'><input type='text' class='form-control text-right barcode' value='" + this.barcode + "' readonly></td>" +
                            "<td align='left'>" + this.gram + "</td>" +
                            "<td align='left'>" + this.packing + "</td>" +
//                                "<td align='left'>" + this.colour + "</td>" +
//                            "<input type='hidden' class='code' value='" + this.unit + "'></td>" +
                            "<td align='left'><input type='text' class='form-control text-right gstperc' value='" + this.gstperc + "'></td>" +
                            "<td align='left'><input type='text' class='form-control text-right cost' value='" + this.cost + "' ></td>" +
                            "<td align='left'><input type='text' class='form-control text-right rate' value='" + this.rate + "' ></td>" +
                            "<td align='left'><input type='text' class='form-control text-right mrp' value='" + this.mrp + "' ></td>" +
                            "<td align='left'><input type='text' class='form-control text-right msp' value='" + this.msp + "' ></td>" +
                            "</tr>";
                });
                $(".sloader2").hide();
                $(".sTbody2").empty();
                $(".fetchData").html(values);
                if (response) {
                    $(".search-action-bar2").show();
                }
            },
            error: function (request, textStatus, errorThrown) {

            }
        });
    }
    $("#search2").click(function () {
        getSearchData2();
    });
});
