$(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_item.php",
            success: function (response) {
                if (response != '') {
                    $.each(response, function () {

                        fetch += "<tr>" +
                                "<td align='left'>" + this.ctr + "</td>" +
                                "<td align='left'><input type='text' class='form-control  name' value='" + this.name + "' readonly></td>" +
                                "<td align='left'>" + this.hsncode + "</td>" +
                                "<td align='left'><input type='text' class='form-control text-right gstperc' value='" + this.gstperc + "'></td>" +
                                "<td align='left'>" + this.unit + "</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>" +
                                "</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 = {};
        $(".name").each(function () {
            var tr = $(this).closest("tr"),
                    name = tr.find(".name").val(),
                    hsncode = tr.find(".hsncode").val(),
                    unit = tr.find(".unit").val(),
                    gstperc = tr.find(".gstperc").val(),
                    cost = tr.find(".cost").val(),
                    rate = tr.find(".rate").val();
                    

            myray = {name: name, hsncode: hsncode, gstperc: gstperc, unit: unit, cost: cost, rate: rate};
            chrate.push(myray);
        });
        var count = chrate.length,
                data = {chrate: chrate, count: count};
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: "saveChRate_item.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_item.php",
            data: data,
            success: function (response) {
                $.each(response, function () {
                    values += "<tr>" +
                            "<td align='left'>" + this.ctr + "</td>" +
                            "<td align='left'><input type='text' class='form-control text-right name' value='" + this.name + "' readonly></td>" +
                            "<td align='left'>" + this.hsncode + "</td>" +
                            "<td align='left'><input type='text' class='form-control text-right gstperc' value='" + this.gstperc + "'></td>" +
                            "<td align='left'>" + this.unit + "</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>" +
                            "</tr>";
                });
                $(".sloader2").hide();
                $(".sTbody2").empty();
                $(".fetchData").html(values);
                if (response) {
                    $(".search-action-bar2").show();
                }
            },
            error: function (request, textStatus, errorThrown) {

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