$(document).ready(function () {
    $("body").on('change blur', '.company,.itemgrp', getData);
    $("body").on('click', '.proceed', fetchVal);
    $("body").on('click', '.saveChRate', saveFetch);
    $("body").on('keyup change', '.bdpamt,.bellamt,.lockamt,.fittingamt', tablcal);
//    $("body").on('  change', '.sellrate,.mrp', checkmrp);

    $(".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,
                        dsellrate = this.sellrate,
                        dmrp = this.mrp,
                                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(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 bdpamt' value='" + dbdp + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right vatamt' value='" + dvat.toFixed(2) + "' readonly></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 bellamt' value='" + dbell + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right lockamt' value='" + dlock + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right fittingamt' value='" + dfitting + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right miscamt' value='" + dmisc.toFixed(2) + "' readonly></td>" +
                                "<td align='left'><input type='text' class='form-control text-right cost' value='" + dcost.toFixed(2) + "' readonly></td>" +
                                "<td align='left'><input type='text' class='form-control text-right sellrate' value='" + dsellrate + "'></td>" +
                                "<td align='left'><input type='text' class='form-control text-right mrp' value='" + dmrp + "'></td>" +
                                "</tr>";
                    });

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

                } else {
                    alert("No Records Found");
                }
            },
            error: function (request, errorThrown, textStatus) {
                console.log(errorThrown);
            }
        });
    }
    
    function tablcal(){
        var gstperc = $('.vat').val(),
               tr = $(this).closest("tr"),
                bdpamt = tr.find('.bdpamt').val(),
                mmiscperc = $('.misc').val(),
                bellamt = tr.find('.bellamt').val(),
                lockamt = tr.find('.lockamt').val(),
                fittingamt = tr.find('.fittingamt').val(),
             vatamt = parseFloat(bdpamt) * parseFloat(gstperc) / 100,
             miscamt = parseFloat(bdpamt) * parseFloat(mmiscperc) / 100,
             totcost = parseFloat(bdpamt) + parseFloat(vatamt) + parseFloat(bellamt) + parseFloat(lockamt) + parseFloat(fittingamt) + parseFloat(miscamt);
     
     tr.find('.miscamt').val(miscamt);
     tr.find('.vatamt').val(vatamt);
     tr.find('.cost').val(totcost);
        
   
    }
//    function checkmrp(){
//              var tr = $(this).closest("tr"),
//                      sellrate = tr.find('.sellrate').val(),
//                      mrp = tr.find('.mrp').val(),
//                      cost = tr.find('.cost').val();
//                 
//              
//             if(sellrate < cost && mrp < cost  ){
//                 alert('SELL RATE AND MRP CANNOT BE LESS THAN ALC');
//                 tr.find('.cost').focus();
//                 tr.find('.sellrate').val(0);
//                 tr.find('.mrp').val(0);
//             }
//    }

    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(".bdpamt").val(),
                    vat = tr.find(".vatamt").val(),
//                    cess = tr.find(".cess").val(),
//                    excise = tr.find(".excise").val(),
                    bell = tr.find(".bellamt").val(),
                    lock = tr.find(".lockamt").val(),
                    fitting = tr.find(".fittingamt").val(),
                    misc = tr.find(".miscamt").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, bell: bell,
                    lock: lock, fitting: fitting, misc: misc, cost: cost, sellrate: sellrate, mrp: mrp, 
                    percvat : percvat, 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) {

            }
        });
    }
});
