var AccId = 2;
$(document).ready(function () {


    Defaultfocus();

    $("#btnsubmit").click(function (e) {
        SaveAndUpdate(1)

    });
});


function addaccessories() {

    $('#Accessdiv').append("<div class='form-group row'><div class='col-md-7'><input type='hidden' class='form-control' id='Accsessories_" + AccId + "_Id' value='0' /><input type='text' class='form-control' id='Accsessories_" + AccId + "' onkeypress=AccessoriesLoad('Accsessories_" + AccId + "') onkeyup=CheckID('Accsessories_" + AccId + "') /></div></div>")
    AccId++;

}

function AccessoriesLoad(Id) {
    $("#" + Id).autocomplete({
        delay: 0,
        minLength: 0,
        source: function (request, response) {
            var data = {};
            data.ItemCode = $("#" + Id).val();

            $.ajax({
                url: '../Purchase/ProductSearch',
                type: "POST",
                data: data,
                dataType: "json",
                success: function (data) {
                    response($.map(data, function (item) {
                        return ({
                            ColCount: 'TWO',
                            label: item.Description,
                            label1: item.ItemCode,
                            ItemId: item.ItemId

                        })
                    }));
                }

            })

        },
        autoFocus: true,
        select: function (event, ui) {
            $('#' + Id + '_Id').val(ui.item.ItemId);

            if (Id == 'ItemName') {

                var dataX = {};
                dataX.ItemId = ui.item.ItemId;

                $.ajax({
                    type: "POST",
                    url: "../ProductMstElectroniccs/AccessoriesGetandGets",
                    data: dataX,
                    success: function (result) {
                        AccId = 2;
                        $('#Accsessories_1').val('');
                        $('#Accsessories_1_Id').val(0);
                        $('#Accessdiv').empty();
                        Showaccessories(result.oList);
                    }
                });
            }
        },
    });
}

function CheckID(Id) {
    if ($('#' + Id).val() == '') {
        $('#' + Id + '_Id').val(0);
    }
}


function SaveAndUpdate(Flag) {
    if ($('#ItemName_Id').val() == 0) {
        warningshow('Please Select Product', 'ItemName');
    }
    else {
        var XArray = new Array();


        for (var i = 1; i <= AccId ; i++) {


            var ItemId = $('#ItemName_Id').val();
            var AccessQty = 1;
            var AccessoriesId = $('#Accsessories_' + i + '_Id').val();

            if (AccessoriesId != undefined && AccessoriesId != 0 && AccessoriesId != '') {
                XArray.push({ 'AccessoriesId': AccessoriesId, 'AccessQty': AccessQty, 'ItemId': ItemId })
            }
        }

        if (XArray != "") {
            var data = { 'ProductMstModel': XArray };
            $.ajax(
        {
            type: "POST",
            url: "../ProductMstElectroniccs/AccessoriesUpdate",
            data: data,
            success: function (result) {
                var status = result.oList[0].Status;
                Showalerts(status);
            }
        });
        }
    }
}

function formrefresh() {
    AccId = 2;
    $('#Accsessories_1').val('');
    $('#Accsessories_1_Id').val(0);
    $('#ItemName_Id').val(0);
    $('#ItemName').val('')
    $('#Accessdiv').empty();
    Defaultfocus();

}
function Defaultfocus() {
    $('#ItemName').focus();
}


function Showaccessories(result) {
    if (result.length >= 1) {
        $('#Accsessories_1').val(result[0].AccessoriesName);
        $('#Accsessories_1_Id').val(result[0].AccessoriesId);
    }
    for (var i = 1; i < result.length; i++) {

        $('#Accessdiv').append("<div class='form-group row'><div class='col-md-3'><input type='hidden' class='form-control' id='Accsessories_" + AccId + "_Id' value='" + result[i].AccessoriesId + "' /><input type='text' class='form-control' id='Accsessories_" + AccId + "' onkeypress=AccessoriesLoad('Accsessories_" + AccId + "') onkeyup=CheckID('Accsessories_" + AccId + "') value='" + result[i].AccessoriesName + "' /></div></div>")
        AccId++;

    }
}
