$(document).ready(function () {
    $("body").on("click", '.remove', remove);
    $("body").on("blur", '.prodcode', getProd);
//    $("body").on("blur", '.prodcode', AddRows);
    $("body").on("blur", '.prodcode', chkDuplicate);
    
    $("body").on("keyup", ".csearch", getSearchData);
    $("body").on("click", ".cload", getSearchData);
    $("body").on("click", "#search", getSearchData);
    $("body").on("change", ".state", getStateCode);

    function getStateCode() {
        var state = $(".state").val();
        $.get('get_stcode.php', {state: state}, function (data) {
            $(".statecode").val(data);
        });
    }
    
    
    
    var accode = $(".vouchno").val();
    if (accode == 49) {
        $("#hdelete").hide();
    }

    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".cnoRecord").hide();
    $(".cloader").hide();

    $(".searchModal").on('shown.bs.modal', function () {
        $(".csearch").focus();
        $(".csearch").val("");
        $(".cfield").val("1");
        getSearchData();
    });

    var imrangeFrom = 0,
            imrangeTo = 40;

    function getSearchData(pagination) {

        $(".cloader").show();

        if (pagination == 'prev') {
            if (imrangeFrom > 0) {
                imrangeFrom = (imrangeFrom - 40);
                imrangeTo = (imrangeTo - 40);
            }
        }
        if (pagination == 'next') {
            imrangeFrom = (imrangeFrom + 40);
            imrangeTo = (imrangeTo + 40);
        }

        var series = $(".series").val(),
                vouchno = $(".vouchno").val(),
                csearch = $(".csearch").val(),
                cfield = $(".cfield").val(),
                coperator = $(".coperator").val(),
                data = {csearch: csearch, cfield: cfield, coperator: coperator,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
                values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "cust_search.php",
            data: data,
            success: function (response) {
                if (response == "") {
                    $(".cnoRecord").show();
                    $(".cnext").hide();
                } else {
                    $(".cnext").show();
                    $(".cnoRecord").hide();

                    $.each(response, function () {
                        values += "<tr class='clickable-row' data-href='" + this.action + "'>" +
                                "<td align='left'>" + this.name + "</td>" +
                                "<td align='left'>" + this.add1 + "</td>" +
                                "<td align='left'>" + this.city + "</td>" +
                                "<td align='left'>" + this.mobile + " </td>" +
                                "<td align='left'>" + this.email + "</td>" +
                                "<td align='left'><a href='" + this.action + "' ><i class='fa fa-pencil'></i></a></td>" +
                                "</tr>";
                    });
                    $(".cloader").hide();
                    $(".cTbody").empty();
                    $(".cTbody").html(values);
                    if (response) {
                        $(".search-action-bar").show();
                    }
                }
            },
            error: function (request, textStatus, errorThrown) {

            }
        });
    }
    $("#search").click(function () {
        getSearchData();
    });
    $(".cprev").click(function () {
        getSearchData('prev');
    });
    $(".cnext").click(function () {
        getSearchData('next');
    });
    
    


    //////////////////////////////////////////////Search Part - End  //////////////////////////////////////////////
    
    $('#addnew').click(function () {
        AddRows();
    }); 
    
    function AddRows() {
        var ctr = $('#items').val();
        var hide_check = "";
//        if ($("#del_all").prop("checked")) {
//            hide_check = 0;
//        } else {
//            hide_check = 1;
//        }
        $.post('stock_oil_add.php', {ctr: ctr}, function (data) {
            $(data).appendTo('#tabledata');
            ctr++;
            $('#items').val(ctr);
//            $('.detailpart').select2();
        });
    }
    
    
    function getProd() {
        var tr = $(this).closest("tr"),
                prodcode = tr.find(".prodcode").val(),
                data = {prodcode: prodcode};
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "getprodname.php",
            data: data,
            success: function (response) {
                if (response != '') {
                    $.each(response, function () {
                        var success = this.success;
                        if (success == false) {
                            alert("No Product Found");
                            tr.find(".prodcode").val("");
                            tr.find(".product").val("");
                            tr.find(".unit").val("");
                            tr.find(".prodcode").focus();
                        } else {
                            tr.find(".product").val(this.name);
                            tr.find(".unit").val(this.unit);
                        }
                    });
                } else {
                    alert('No Record Found for this Prodcode');
                    $('.prodcode').focus();
                }

            }
        });
    }
    
    
function chkDuplicate() {
        var tr = $(this).closest("tr"),
                prodcode = tr.find(".prodcode").val().toUpperCase(),
                sr = tr.find(".srno").val();
        if (prodcode != '') {
            $(".myTable").find('tr').each(function () {
                var row = $(this);
                var srno = row.find('.srno').val();
                var prodchk = row.find('.prodcode').val();
//                var prodchk1 = prodchk.toUpperCase();
                if (prodcode == prodchk && srno != sr) {
                    alert('Duplicate Product Entry');
                    tr.find(".product").val("");
                    tr.find(".unit").val("");
                    tr.find(".prodcode").val("");
                    tr.find(".prodcode").focus();
                    return false;
                }
            });
        }
    }    
//    $("#select2-vessel-e4-container").click(function () {
//        GetVessel();
//    });

    function remove() {
        $(this).closest("tr").remove();
    }
   
    
    
    
});