$(document).ready(function () {
    $("body").on('change blur', '.brand,.itemgrp', getData);
    $("body").on('click', '.proceed', fetchVal);
    $("body").on('click', '.saveChRate', saveFetch);
//    $("body").on('keyup change blur', '.bdpamt,.mrp,.miscperc,.gstperc', tablcal);
//    $("body").on("keyup", ".ssearch2", getSearchData2);
//    $("body").on("click", ".sload2", getSearchData2);
//    $("body").on("change", ".orderAs2, .sortBy2", getSearchData2);
//    $("body").on('  change', '.sellrate,.mrp', checkmrp);

    $(".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 getData() {
        var brand = $(".brand").val();
        var itemgrp = $("#itemgrp").val();
        var data = {brand: brand, itemgrp: itemgrp};
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "getItemdtl.php",
            data: data,
            success: function (response) {
                var m = response;
                // $('.vat').val(m.vatperc),
//                        $('.cst').val(m.cstperc),
//                        $('.cess').val(m.cessperc),
//                        $('.excise').val(m.exciseperc),
//                        $('.bell').val(m.bell),
//                        $('.lock').val(m.lock),
//                        $('.fitting').val(m.fitting),
                // $('.misc').val(m.miscperc);
//                        $('.sralc').val(m.sellalc),
//                        $('.mrpalc').val(m.mrpalc);
            }
        });

    }

    function fetchVal() {
        var brand = $(".brand").val(),
                fetch = "";
        $.ajax({
            type: "POST",
            datatype: "json",
            data: {brand: brand},
            url: "openstksrch.php",
            success: function (response) {
                if (response != '') {
                    $.each(response, function () {
//                
                        var code = this.code,
                                name = this.name,
                                brand = this.brand,
                                hsncode = this.hsncode,
                                gstperc = this.gstperc,
                                cost = this.cost,
                                selling = this.selling;


                        fetch += "<tr>" +
                                "<td align='left'>" + this.ctr + "</td>" +
                                "<td align='left' name='code_'" + this.ctr + "><input type='hidden' class='form-control code' value='" + this.code + "' name='code_'" + this.ctr + ">" + this.code + "</td>" +
                                "<td align='left'>" + this.name + "</td>" +
                                "<td align='left'>" + this.brand + "</td>" +
                                "<td align='left'><input type='text' class='form-control hsncode' value='" + this.hsncode + "' name='hsncode_'" + this.ctr + "></td>" +
                                "<td align='left'><input type='text' class='form-control text-right gstperc' value='" + this.gstperc + "' name='gstperc_'" + this.ctr + "></td>" +
                                "<td align='left'><input type='text' class='form-control text-right cost' value='" + this.cost + "' name='cost_'" + this.ctr + "></td>" +
                                "<td align='left'><input type='text' class='form-control text-right selling' value='" + this.selling + "' name='selling_'" + this.ctr + "></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 = {};
        $(".cost").each(function () {
            var tr = $(this).closest("tr"),
                    code = tr.find(".code").val(),
                    cost = tr.find(".cost").val(),
                    selling = tr.find(".selling").val(),
                    hsncode = tr.find(".hsncode").val(),
                    gstperc = tr.find(".gstperc").val();
            myray = {code: code, gstperc: gstperc, hsncode: hsncode, cost: cost, selling: selling};
            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(),
//                brand = $(".brand").val(),
//                itemgrp = $(".itemgrp").val(),
//                data = {ssearch: ssearch, sfield: sfield, sortBy: sortBy, orderAs: orderAs, soperator: soperator, brand: brand, itemgrp: itemgrp},
//                values = "";
//        $.ajax({
//            type: 'GET',
//            dataType: 'json',
//            url: "chrate_srch.php",
//            data: data,
//            success: function (response) {
//                $.each(response, function () {
//                    var dbdp = this.bdp,
//                            mmisc = this.miscperc,
//                            miscamt = this.miscamt,
//                            gstamt = this.gstamt,
//                            gstperc = this.gstperc,
//                            mtaxval = this.taxval,
//                            dcost = this.cost,
//                            dmrp = this.mrp,
//                            margamt = this.margamt,
//                            margperc = this.margperc;
//                    values += "<tr>" +
//                            "<td align='left'>" + this.ctr + "</td>" +
//                            "<td align='left'>" + this.name + "</td>" +
//                            "<td align='left'>" + this.barcode + "</td>" +
//                            "<td align='left'>" + this.brand + "</td>" +
//                            "<td align='left'>" + this.size + "</td>" +
//                            "<td align='left'>" + this.colour + "</td>" +
////                                "<td align='left'><input type='text' class='form-control opstk' value='" + this.opstk + "'>" +
//                            "<input type='hidden' class='code' value='" + this.code + "'></td>" +
//                            "<td align='left'><input type='text' class='form-control text-right bdpamt' value='" + dbdp + "'></td>" +
//                            "<td align='left'><input type='text' class='form-control text-right miscperc' value='" + mmisc + "' ></td>" +
//                            "<td align='left'><input type='text' class='form-control text-right miscamt' value='" + miscamt + "' readonly></td>" +
//                            "<td align='left'><input type='text' class='form-control text-right taxval' value='" + mtaxval + "' readonly></td>" +
//                            "<td align='left'><input type='text' class='form-control text-right gstperc' value='" + gstperc + "' readonly></td>" +
//                            "<td align='left'><input type='text' class='form-control text-right gstamt' value='" + gstamt + "' readonly></td>" +
//                            "<td align='left'><input type='text' class='form-control text-right cost' value='" + dcost + "' readonly></td>" +
//                            "<td align='left'><input type='text' class='form-control text-right mrp' value='" + dmrp + "' ></td>" +
//                            "<td align='left'><input type='text' class='form-control text-right margamt' value='" + margamt + "' readonly></td>" +
//                            "<td align='left'><input type='text' class='form-control text-right margperc' value='" + margperc + "' readonly></td>" +
//                            "</tr>";
//                });
//                $(".sloader2").hide();
//                $(".sTbody2").empty();
//                $(".fetchData").html(values);
//                if (response) {
//                    $(".search-action-bar2").show();
//                }
//            },
//            error: function (request, textStatus, errorThrown) {
//
//            }
//        });
//    }
//    $("#search2").click(function () {
//        getSearchData2();
//    });
});
