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


    Defaultfocus();
    GetItemGroup(0);
    $("#btnsubmit").click(function (e) {
        SaveAndUpdate(1)

    });
    $("#ItemName").keydown(function (e) {
        var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
        if (key == 13) {
            e.preventDefault();
            $('#ItemGroup').focus();
        }

    });
    $("#ItemGroup").keydown(function (e) {
        var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
        if (key == 13) {
            e.preventDefault();
            $('#SubItems_1').focus();
        }

    });
});

function additems() {
    $('#Itemsdiv').append("</br><div class='row'><div class='col-md-3'></div><div class='col-md-4'  style='width:20%;'><input type='hidden' class='form-control' id='SubItems_" + SubId + "_Id' value='0' /><input type='text' class='form-control' id='SubItems_" + SubId + "' onkeypress=AccessoriesLoad('SubItems_" + SubId + "','" + SubId + "') onkeyup=CheckID('SubItems_" + SubId + "') /></div><div class='col-md-2'><input type='text' class='form-control' id='Quantity_" + SubId + "' value='" + 1 + "' /></div><div class='col-md-2'></div></div>")
    SubId++;

}
//function AccessoriesLoad(Id)
//{

//}
function Showaccessories(result) {
    if (result.length >= 1) {
        $('#SubItems_1').val(result[0].SubItemName);
        $('#SubItems_1_Id').val(result[0].SubItemId);
        $('#Quantity_1').val(parseInt(result[0].Quantity || 0));
    }
    for (var i = 1; i < result.length; i++) {

        $('#Itemsdiv').append("</br><div class='row'><div class='col-md-3'></div><div class='col-md-4' style='width:20%;'><input type='hidden' class='form-control' id='SubItems_" + SubId + "_Id' value='" + result[i].SubItemId + "' /><input type='text' class='form-control' id='SubItems_" + SubId + "' onkeypress=AccessoriesLoad('SubItems_" + SubId + "','" + SubId + "') onkeyup=CheckID('SubItems_" + SubId + "') value='" + result[i].SubItemName + "' /></div><div class='col-md-2'><input type='text' class='form-control' id='Quantity_" + SubId + "'  value='" + parseInt(result[i].Quantity || 0) + "' /></div><div class='col-md-2'></div></div>")
        SubId++;

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

//Load Groups
function Defaultfocus() {
    $('#ItemName').focus();
}
function GetItemGroup(GroupId) {
    var data = {};
    data.GroupId = 0;
    $.ajax({
        type: "POST",
        url: "../ItemMapping/ItemGroupGetandGets",
        data: data,
        success: function (result) {
            Loadgroup(result.oList);
        }

    });
}
function Loadgroup(result) {
    $("#ItemGroup").empty();
    var group = "<option value=0> -Select- </option>";
    for (var i = 0; i < result.length; i++) {
        group += "<option value='" + result[i].GroupId + "'>" + result[i].GroupName + "</option>";
    }
    $("#ItemGroup").append(group);
}

function SaveAndUpdate(Flag) {
    if ($('#ItemName').val() == '') {
        warningshow('Please Select Product', 'ItemName');
    }
    else if ($('#ItemGroup').val() == '0') {
        warningshow('Please Select Group', 'ItemGroup');
    }
    else {
        var XArray = new Array();
        for (var i = 1; i <= SubId ; i++) {
            var ItemId = $('#ItemName_Id').val();
            var GroupId = $('#ItemGroup').val();
            var SItemsId = $('#SubItems_' + i + '_Id').val();
            console.log(SItemsId)
            var Quantity = $('#Quantity_' + i).val();
            var AutomobileItems = $('#ItemName').val();
            //if (SItemsId != undefined && SItemsId != 0 && SItemsId != '') {
            XArray.push({
                'ItemId': ItemId,
                'GroupId': GroupId,
                'SItemsId': SItemsId,
                'Quantity': Quantity,
                'AutomobileItems': AutomobileItems,
            })
            //}
        }
        console.log(XArray)
        console.log('XArray')
        if (XArray != "") {
            var data = { 'ItemMappingModel': XArray };
            $.ajax(
        {
            type: "POST",
            url: "../ItemMapping/ItemGroupMappingInsert",
            data: data,
            success: function (result) {
                var status = result.oList[0].Status;
                Showalerts(status);
            }
        });
        }
    }
}

function warningshow(message, Id) {
    $('#popupmessage').text(message);
    $('#Warningpopup').show();
    $('#' + Id).focus();
    window.setTimeout(function () {
        $('#Warningpopup').fadeOut();
    }, 3000);
}



function Showalerts(Status) {
    if (Status == 1) {
        swal('Data Saved Successfully', "", "success");
        $('.swal-button swal-button--confirm').focus();
        formrefresh();
    }
}
function formrefresh() {
    SubId = 2;
    $('#ItemName').val('');
    $('#ItemName_Id').val('');
    $('#ItemGroup').val('0');
    $('#Itemsdiv').empty();
    $('#SubItems_1').val('');
    $('#Quantity_1').val(1);
    Defaultfocus();
}