$(document).ready(function () {
    $("body").on("keyup", ".isearch", getSearchData);
    $("body").on("click", ".iload", getSearchData);
    $("body").on("click", "#search", getSearchData);

    $("body").on("click", ".cBrand", showBrand);
    $(".panel-body").on("keyup", ".brandSearchFld", showBrand);
    $("body").on("click", ".removeBrand", removeBrand);
    $("body").on("click", ".brandSave", saveBrand);
    $("body").on("click", ".addNewBrand", AddBrandRows);

    $("body").on("click", ".cSize", showSize);
    $(".panel-body").on("keyup", ".sizeSearchFld", showSize);
    $("body").on("click", ".removeSize", removeSize);
    $("body").on("click", ".sizeSave", saveSize);
    $("body").on("click", ".addNewSize", AddSizeRows);

    $("body").on("click", ".cColour", showColour);
    $(".panel-body").on("keyup", ".colourSearchFld", showColour);
    $("body").on("click", ".removeColour", removeColour);
    $("body").on("click", ".colourSave", saveColour);
    $("body").on("click", ".addNewColour", AddColourRows);

    $("body").on("click", ".cUnit", showUnit);
    $(".panel-body").on("keyup", ".unitSearchFld", showUnit);
    $("body").on("click", ".removeUnit", removeUnit);
    $("body").on("click", ".unitSave", saveUnit);
    $("body").on("click", ".addNewUnit", AddUnitRows);
    
    $("body").on("change", ".itemgrp", gethsn);


    function gethsn(){
        var itemgrp = $('.itemgrp').val();
        $.post('gethsn.php',{itemgrp: itemgrp},function(data){
            $('.hsncode').val(data.hsn);
            $('.gstperc').val(data.gst);
        },'json');
    }

    $(".name").blur(function () {
        $(".brand").select2("open");
    });

    var brand = $(".brand").select2();

    brand.on("select2:close", function (e) {
        $(".size").select2("open");
    });

    var size = $(".size").select2();

    size.on("select2:close", function (e) {
        $(".colour").select2("open");
    });

    var colour = $(".colour").select2();

    colour.on("select2:close", function (e) {
        $(".company").select2("open");
    });


    var company = $(".company").select2();

    company.on("select2:close", function (e) {
        $(".itemgrp").select2("open");
    });

    var itemgrp = $(".itemgrp").select2();

    itemgrp.on("select2:close", function (e) {
        $(".unit").select2("open");
    });


    $(".createMaster").hide();
//////////////////////////////////////////////BRAND - START//////////////////////////////////////////////

    function removeBrand() {
        var tr = $(this).closest("tr"),
                name = tr.find(".name").val();

        $.post("brand_remove.php", {name: name}, function (fdata) {
            if (fdata == "success") {
                refreshBrand();
                tr.remove();
            }
        });
    }

    function showBrand() {
        $(".mSearch").removeClass("sizeSearchFld");
        $(".mSearch").removeClass("unitSearchFld");
        $(".mSearch").removeClass("colourSearchFld");
        $(".mSearch").addClass("brandSearchFld");
        $(".brandSearchFld").focus();
        var button = "brand",
                searchFld = $(".brandSearchFld").val(),
                data = {button: button, searchFld: searchFld};

        $.post("brand_show.php", data, function (fdata) {
            $(".myTable").empty();
            $(".myTable").html(fdata);
//            $(".brandDataTable").dataTable();
        });
        $(".createMaster").show();
    }


    function refreshBrand() {
        var brand = $(".brand").val();

        $.post("brand_refresh.php", {brand: brand}, function (data) {
            $(".brand").html(data);
        });
    }



    function saveBrand() {

        var brand = {},
                searchFld = $(".brandSearchFld").val(),
                myObj = [];

        $(".name").each(function () {
            var tr = $(this).closest("tr"),
                    name = tr.find(".name").val(),
                    srl = tr.find(".srl").val();

            brand = {name: name, srl: srl};
            myObj.push(brand);
        });
        
        var data = {myObj: myObj, searchFld: searchFld};

        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: "brand_save.php",
            data: data,
            success: function (response) {
                if (response.success == true) {
                    alert("Brand Saved");
                    $(".brandSearchFld").val("");
                    refreshBrand();
                    showBrand();
                }
            },
            error: function (request, textStatus, errorThrown) {

            }
        });
    }

    function AddBrandRows() {
        $.post('brand_add.php', {a: 1}, function (data) {
            $(data).appendTo('.brandDataTable');
        });
    }

//////////////////////////////////////////////BRAND - END  //////////////////////////////////////////////


//////////////////////////////////////////////SIZE - START//////////////////////////////////////////////

    function removeSize() {
        var tr = $(this).closest("tr"),
                name = tr.find(".name").val();

        $.post("size_remove.php", {name: name}, function (fdata) {
            if (fdata == "success") {
                tr.remove();
            }
        });
    }

    function showSize() {
        $(".mSearch").removeClass("unitSearchFld");
        $(".mSearch").removeClass("colourSearchFld");
        $(".mSearch").removeClass("sizeSearchFld");
        $(".mSearch").addClass("sizeSearchFld");
        $(".sizeSearchFld").focus();

        var button = "size",
                searchFld = $(".sizeSearchFld").val(),
                data = {button: button, searchFld: searchFld};

        $.post("size_show.php", data, function (fdata) {
            $(".myTable").empty();
            $(".myTable").html(fdata);
//            $(".sizeDataTable").dataTable();
        });
        $(".createMaster").show();
    }


    function refreshSize() {
        var size = $(".size").val();

        $.post("size_refresh.php", {size: size}, function (data) {
            $(".size").html(data);
        });
    }

    function saveSize() {

        var size = {},
                searchFld = $(".sizeSearchFld").val(),
                myObj = [];

        $(".name").each(function () {
            var tr = $(this).closest("tr"),
                    name = tr.find(".name").val(),
                    check = "",
                    srl = tr.find(".srl").val();
            if (tr.find('.check').is(":checked"))
            {
                check = 1;
            }

            size = {name: name, srl: srl, check: check};
            myObj.push(size);
        });

        var data = {myObj: myObj, searchFld: searchFld};

        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: "size_save.php",
            data: data,
            success: function (response) {
                if (response.success == true) {
                    alert("Size Saved");
                    $(".sizeSearchFld").val("");
                    refreshSize();
                    showSize();
                }
            },
            error: function (request, textStatus, errorThrown) {

            }
        });
    }

    function AddSizeRows() {
        $.post('size_add.php', {a: 1}, function (data) {
            $(data).appendTo('.sizeDataTable');
        });
    }

//////////////////////////////////////////////SIZE - END  //////////////////////////////////////////////


//////////////////////////////////////////////COLOUR - START//////////////////////////////////////////////

    function removeColour() {
        var tr = $(this).closest("tr"),
                name = tr.find(".name").val();

        $.post("colour_remove.php", {name: name}, function (fdata) {
            if (fdata == "success") {
                tr.remove();
            }
        });
    }

    function showColour() {
        $(".mSearch").removeClass("unitSearchFld");
        $(".mSearch").removeClass("brandSearchFld");
        $(".mSearch").removeClass("sizeSearchFld");
        $(".mSearch").addClass("colourSearchFld");
        $(".colourSearchFld").focus();

        var button = "colour",
                searchFld = $(".colourSearchFld").val(),
                data = {button: button, searchFld: searchFld};

        $.post("colour_show.php", data, function (fdata) {
            $(".myTable").empty();
            $(".myTable").html(fdata);
//            $(".colourDataTable").dataTable();
        });
        $(".createMaster").show();
    }


    function refreshColour() {
        var colour = $(".colour").val();

        $.post("colour_refresh.php", {colour: colour}, function (data) {
            $(".colour").html(data);
        });
    }

    function saveColour() {

        var colour = {},
                searchFld = $(".colourSearchFld").val(),
                myObj = [];

        $(".name").each(function () {
            var tr = $(this).closest("tr"),
                    name = tr.find(".name").val(),
                    srl = tr.find(".srl").val();

            colour = {name: name, srl: srl};
            myObj.push(colour);
        });
        // debugger;
        var data = {myObj: myObj, searchFld: searchFld};

        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: "colour_save.php",
            data: data,
            success: function (response) {
                if (response.success == true) {
                    alert("Colour Saved");
                    $(".colourSearchFld").val("");
                    refreshColour();
                    showColour();
                }
            },
            error: function (request, textStatus, errorThrown) {

            }
        });
    }

    function AddColourRows() {
        $.post('colour_add.php', {a: 1}, function (data) {
            $(data).appendTo('.colourDataTable');
        });
    }

//////////////////////////////////////////////COLOUR - END  //////////////////////////////////////////////

//////////////////////////////////////////////COLOUR - START//////////////////////////////////////////////

    function removeUnit() {
        var tr = $(this).closest("tr"),
                name = tr.find(".name").val();

        $.post("unit_remove.php", {name: name}, function (fdata) {
            if (fdata == "success") {
                tr.remove();
            }
        });
    }

    function showUnit() {
        $(".mSearch").removeClass("unitSearchFld");
        $(".mSearch").removeClass("brandSearchFld");
        $(".mSearch").removeClass("sizeSearchFld");
        $(".mSearch").addClass("unitSearchFld");
        $(".unitSearchFld").focus();

        var button = "unit",
                searchFld = $(".unitSearchFld").val(),
                data = {button: button, searchFld: searchFld};

        $.post("unit_show.php", data, function (fdata) {
            $(".myTable").empty();
            $(".myTable").html(fdata);
//            $(".unitDataTable").dataTable();
        });
        $(".createMaster").show();
    }


    function refreshUnit() {
        var unit = $(".unit").val();

        $.post("unit_refresh.php", {unit: unit}, function (data) {
            $(".unit").html(data);
        });
    }

    function saveUnit() {

        var unit = {},
                searchFld = $(".unitSearchFld").val(),
                myObj = [];

        $(".name").each(function () {
            var tr = $(this).closest("tr"),
                    name = tr.find(".name").val(),
                    srl = tr.find(".srl").val();

            unit = {name: name, srl: srl};
            myObj.push(unit);
        });
        // debugger;
        var data = {myObj: myObj, searchFld: searchFld};

        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "unit_save.php",
            data: data,
            success: function (response) {
                if (response.success == true) {
                    alert("Unit Saved");
                    $(".unitSearchFld").val("");
                    refreshUnit();
                    showUnit();
                }
            },
            error: function (request, textStatus, errorThrown) {

            }
        });
    }

    function AddUnitRows() {
        $.post('unit_add.php', {a: 1}, function (data) {
            $(data).appendTo('.unitDataTable');
        });
    }

//////////////////////////////////////////////COLOUR - END  //////////////////////////////////////////////


//////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".inoRecord").hide();
    $(".iloader").hide();

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

    var imrangeFrom = 0,
            imrangeTo = 40;

    function getSearchData(pagination) {

        $(".iloader").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(),
                isearch = $(".isearch").val(),
                ifield = $(".ifield").val(),
                sortBy = $(".sortBy").val(),
                orderAs = $(".orderAs").val(),
                ioperator = $(".ioperator").val(),
                data = {isearch: isearch, ifield: ifield, sortBy: sortBy, orderAs: orderAs, ioperator: ioperator,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
        values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "item_search.php",
            data: data,
            success: function (response) {
                $.each(response, function () {
                    values += "<tr class='clickable-row' data-href='" + this.action + "'>" +
                            "<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'>" + this.unit + "</td>" +
                            "<td align='left'><a href='" + this.action + "' ><i class='fa fa-pencil'></i></a></td>" +
                            "</tr>";
                });
                $(".iloader").hide();
                $(".iTbody").empty();
                $(".iTbody").html(values);
                if (response) {
                    $(".search-action-bar").show();
                }
            },
            error: function (request, textStatus, errorThrown) {

            }
        });
    }
    $("#isearch").click(function () {
        getSearchData();
    });
    $(".iprevious").click(function () {
        getSearchData('prev');
    });
    $(".inext").click(function () {
        getSearchData('next');
    });


    //////////////////////////////////////////////Search Part - End  //////////////////////////////////////////////


});