$(document).ready(function () {
    $("body").on('change blur', '.company,.itemgrp', getData);
    $("body").on('click', '.proceed', fetchVal);
    $("body").on('click', '.saveChRate', saveFetch);

    $(".fetchPart").hide();

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

    function getData() {
        var company = $(".company").val();
        var data = {company: company};
        $.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 = $(".company").val(),
                itemgrp = $(".itemgrp").val(),
                fetch = "";

        var mvat = $('.vat').val(),
                mcst = $('.cst').val(),
                mcess = $('.cess').val(),
                mexcise = $('.excise').val(),
                mbell = $('.bell').val(),
                mlock = $('.lock').val(),
                mfitting = $('.fitting').val(),
                mmisc = $('.misc').val(),
                msralc = $('.sralc').val(),
                mmrpalc = $('.mrpalc').val();


        $.ajax({
            type: "POST",
            datatype: "json",
            data: {brand: brand, itemgrp: itemgrp},
            url: "openstksrch.php",
            success: function (response) {
                if (response != '') {
                    $.each(response, function () {
                        var dbdp = this.bdp,
                                dvat = dbdp * (mvat / 100),
                                dcst = dbdp * (mcst / 100),
                                dcess = dbdp * (mcess / 100),
                                dexcise = dbdp * (mexcise / 100),
                                dbell = mbell,
                                dlock = mlock,
                                dfitting = mfitting,
                                dmisc = dbdp * (mmisc / 100),
                                dnewalc = parseFloat(dbdp) + parseFloat(dvat) + parseFloat(dcst) + parseFloat(dcess) + parseFloat(dexcise) + parseFloat(dbell) + parseFloat(dlock) + parseFloat(dfitting) + parseFloat(dmisc),
                                csellrate = dnewalc * (msralc / 100),
                                cmrp = dnewalc * (mmrpalc / 100),
                                dsellrate = parseFloat(dnewalc) + parseFloat(csellrate),
                                dmrp = parseFloat(dnewalc) + parseFloat(cmrp),
                                dcost = dnewalc;

                        fetch += "<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 bdp' value='" + dbdp + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right vat' value='" + dvat.toFixed(2) + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right cst' value='" + dcst.toFixed(2) + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right cess' value='" + dcess.toFixed(2) + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right excise' value='" + dexcise.toFixed(2) + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right bell' value='" + dbell + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right lock' value='" + dlock + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right fitting' value='" + dfitting + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right misc' value='" + dmisc.toFixed(2) + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right cost' value='" + dcost.toFixed(2) + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right sellrate' value='" + dsellrate.toFixed(2) + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right mrp' value='" + dmrp.toFixed(2) + "'></td>" +
                                "</tr>";
                    });

                    $(".fetchData").html(fetch);
                    $(".fetchPart").show();
                    $(".getPart").hide();

                } else {
                    alert("No Records Found");
                }
            },
            error: function (request, errorThrown, textStatus) {
                console.log(errorThrown);
            }
        });
    }

    function saveFetch() {
        var chrate = [],
                myray = {};
        $(".opstk").each(function () {
            var tr = $(this).closest("tr"),
                    code = tr.find(".code").val(),
                    opstk = tr.find(".opstk").val(),
                    bdp = tr.find(".bdp").val(),
                    vat = tr.find(".vat").val(),
                    cess = tr.find(".cess").val(),
                    excise = tr.find(".excise").val(),
                    bell = tr.find(".bell").val(),
                    lock = tr.find(".lock").val(),
                    fitting = tr.find(".fitting").val(),
                    misc = tr.find(".misc").val(),
                    cost = tr.find(".cost").val(),
                    sellrate = tr.find(".sellrate").val(),
                    mrp = tr.find(".mrp").val();

            var percvat = $('.vat').val(),
                    perccst = $('.cst').val(),
                    perccess = $('.cess').val(),
                    percexcise = $('.excise').val(),
                    percmisc = $('.misc').val(),
                    percsralc = $('.sralc').val(),
                    percmrpalc = $('.mrpalc').val();

            myray = {code: code, opstk: opstk, bdp: bdp, vat: vat, cess: cess, excise: excise, bell: bell,
                    lock: lock, fitting: fitting, misc: misc, cost: cost, sellrate: sellrate, mrp: mrp, 
                    percvat : percvat, perccst : perccst, perccess: perccess, percexcise: percexcise, percmisc :percmisc, percsralc :percsralc,  percmrpalc :percmrpalc};
            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) {

            }
        });
    }
});
