var num = 1;
var valid = false;
var data = {};
var checkDataEmpty = false;
var time = 60;
var jumping = 0;
var btnValue = true;
var accountSide;
$(document).ready(function () {
    $("[data-event='true']").click(function () {
        btnValue = $(this).attr("data-state");
        if (btnValue == "true") {
            $("#btnHagigi").attr("class", "btn btn-primary isactive");
            $("#btnHogogi").attr("class", "btn btn-primary dontisactive");
        }
        else if (btnValue == "false") {
            $("#btnHagigi").attr("class", "btn btn-primary dontisactive");
            $("#btnHogogi").attr("class", "btn btn-primary isactive");
        }
    });
    $('[data-mask="date"]').attr('placeholder', '__/__/__');
    $('[data-mask="date"]').attr('maxlength', '8');
});
function processStep(isNext) {
    operation(isNext)
}
function operation(isNext) {
    valid = false;
    if (!isNext) {
        nextPage(isNext);
        if (num == jumping)
            nextPage(isNext);
    }
    if (isNext) {
        switch (num) {
            case 1:
                if ($('#chkCustomer').is(':checked')) {
                    valid = true;
                    $('#errorDiv').hide();
                }
                else {
                    $('#errorDiv').show();
                }
                data = null;
                break;
            case 2:
                data = null;
                if ($('#txtSerial').val() != '') {
                    valid = true;
                }
                else
                    showMsgModal(0, serial);
                break;
            case 3:
                data = getJsonValue();
                var exceprionText = checkValidation("#registerDiv");
                if (exceprionText == '') {
                    valid = true;
                }
                else
                    showMsgModal(0, exceprionText);
                break;
            case 4:
                data = getJsonValue();
                data['Code_Th'] = data.Code_Th;
                valid = true;
                break;
            case 5:
                if ($('#txtCodeRandom').val() != '') {
                    valid = true;
                }
                else
                    showMsgModal(0, activeCode);
                break;
            case 6:
                window.location.href = "Login"
                valid = false;
                break;
        }
        if (valid && isNext && num === 1) {
            nextPage(isNext);
        }
        else if (valid && isNext && (num === 2 || num === 4)) {
            ajax({
                loading: true,
                url: "GeneralApi/IsValidLockSerial",
                data: { type: num, lockSerial: $('#txtSerial').val(), mobileNo: $('#txtMobileNo').val() },
                success: function (result) {
                    if (num == 2) {
                        if (result.TarafHesab.MobileNo != "") {
                            nextPage(isNext);
                            nextPage(isNext);
                            jumping = 3;

                            $('#IsAman').val(result.IsAmani);
                            $('#ParkLock').val(result.ParkLock);
                            $('#NasbMojadad').val(result.NasbMojadad);
                            $('#ValidLock').val(result.ValidLock);
                            $('#serial').text($('#txtSerial').val());
                            $('#txtMobileNo').val(result.TarafHesab.MobileNo);
                            $('#mobail').text(" " + result.TarafHesab.MobileNo.substr(8, 3) + "****" + result.TarafHesab.MobileNo.substr(0, 4));
                        }
                        else {
                            nextPage(isNext);
                        }

                        accountSide = result.TarafHesab;
                        setValueInController(result.TarafHesab);

                        if (result.TarafHesab.MobileNo != '')
                            $('#txtMobileNo').val(result.TarafHesab.MobileNo);
                        else if (result.TarafHesab.MoBileNo2 != '')
                            $('#txtMobileNo').val(result.TarafHesab.MoBileNo2);
                        else if (result.TarafHesab.MoBileNo3 != '')
                            $('#txtMobileNo').val(result.TarafHesab.MoBileNo3);

                        if (result.TarafHesab.Email != '')
                            $('#txtEmail').val(result.TarafHesab.Email);
                        else if (result.TarafHesab.Email2 != '')
                            $('#txtEmail').val(result.TarafHesab.Email2);
                        else if (result.TarafHesab.Email3 != '')
                            $('#txtEmail').val(result.TarafHesab.Email3);
                    }
                    else if (num == 4) {
                        nextPage(isNext);
                        setInterval(function () {
                            if (time != 0) {
                                time--;
                                $('#time').empty();
                                $('#time').text(waitingTime + ":" + time + " " + second);
                            }
                            else {
                                $('#time').empty();
                                $('#time').append("<label  class='label-control' style='cursor: pointer;' onclick='repeatSMS()' >" + resend + "</label>");
                            }
                        }, 1000);
                        valid = true;
                    }
                }
            });
        }
        else if (valid && isNext && accountSide != null) {
            showLoading();
            ajax({
                loading: true,
                url: "GeneralApi/GetSerialLock",
                data: {
                    type: num,
                    lockSerial: $('#txtSerial').val(),
                    tarafHesab: accountSide,
                    mobileNo: $('#txtMobileNo').val(),
                    randomCode: $('#txtCodeRandom').val(),
                    senfName: $("#cboCodeGuild").lookupBuilder().getSelectedText()
                },
                success: function (result) {
                    closeLoading();
                    if (num == 1) {
                        nextPage(isNext);
                    }
                    else if (num == 2) {
                        if (result.TarafHesab.MobileNo != "") {
                            nextPage(isNext);
                            nextPage(isNext);
                            jumping = 3;

                            $('#IsAman').val(result.IsAmani);
                            $('#ParkLock').val(result.ParkLock);
                            $('#NasbMojadad').val(result.NasbMojadad);
                            $('#ValidLock').val(result.ValidLock);
                            $('#serial').text($('#txtSerial').val());
                            $('#txtMobileNo').val(result.TarafHesab.MobileNo);
                            $('#mobail').text(" " + result.TarafHesab.MobileNo.substr(8, 3) + "****" + result.TarafHesab.MobileNo.substr(0, 4));
                        }
                        else {
                            nextPage(isNext);
                        }

                        accountSide = result.TarafHesab;
                        setValueInController(result.TarafHesab);

                        if (result.TarafHesab.MobileNo != '')
                            $('#txtMobileNo').val(result.TarafHesab.MobileNo);
                        else if (result.TarafHesab.MoBileNo2 != '')
                            $('#txtMobileNo').val(result.TarafHesab.MoBileNo2);
                        else if (result.TarafHesab.MoBileNo3 != '')
                            $('#txtMobileNo').val(result.TarafHesab.MoBileNo3);

                        if (result.TarafHesab.Email != '')
                            $('#txtEmail').val(result.TarafHesab.Email);
                        else if (result.TarafHesab.Email2 != '')
                            $('#txtEmail').val(result.TarafHesab.Email2);
                        else if (result.TarafHesab.Email3 != '')
                            $('#txtEmail').val(result.TarafHesab.Email3);
                    }
                    else if (num == 3) {
                        $('#IsAman').val(Result.IsAmani);
                        $('#ParkLock').val(Result.ParkLock);
                        $('#NasbMojadad').val(Result.NasbMojadad);
                        $('#ValidLock').val(Result.ValidLock);
                        nextPage(isNext);
                        nextPage(isNext);
                        jumping = 4;
                    }
                    else if (num == 4) {
                        nextPage(isNext);
                        setInterval(function () {
                            if (time != 0) {
                                time--;
                                $('#time').empty();
                                $('#time').text(waitingTime + ":" + time + " " + second);
                            }
                            else {
                                $('#time').empty();
                                $('#time').append("<label  class='label-control' style='cursor: pointer;' onclick='repeatSMS()' >" + resend + "</label>");
                            }
                        }, 1000);
                        valid = true;
                    }
                    else if (num == 5) {
                        if (result) {
                            $("#div1").remove();
                            $("#div2").remove();
                            $("#div3").remove();
                            $("#div4").remove();
                            $("#div5").remove();
                            nextPage(isNext);
                        }
                    }
                }
            });
        }
    }
}
function repeatSMS() {
    data = getJsonValue();
    data["CodeGuild"] = data["CodeGuild"] ? data["CodeGuild"] : -1;
    num = 4;
    showLoading();
    ajax({
        loading: true,
        url: "GeneralApi/GetSerialLock",
        data: {
            type: num,
            lockSerial: $('#txtSerial').val(),
            tarafHesab: data,
            mobileNo: $('#txtMobileNo').val()
        },
        async: false,
        success: function () {
            closeLoading();
            time = 60;
            num = 5;
        }
    });
}
function nextPage(isNext) {
    if (isNext) {
        if (num == 6)
            return;
        $('.control-label[data-label="div' + num + '"]').removeClass('label-selected').addClass('label-accepted');
        $('.cirlce-style[data-id="div' + num + '"]').html('<img src="../img/pages/setup/tick.svg" class="ico-circle svg" />');
        imgToSvg();
        $('.cirlce-style[data-id="div' + num + '"]').removeClass('selected').addClass('accepted');
    }
    else {
        $('.control-label[data-label="div' + num + '"]').removeClass('label-selected').addClass('label-default');
        $('.cirlce-style[data-id="div' + num + '"]').removeClass('selected');
        $('.cirlce-style[data-id="div' + num + '"] svg').css('fill', '#a0a0a0');
    }

    $('#div' + num + '').hide();
    if (isNext) num++; else num--;
    $('#div' + num + '').css('display', 'flex');

    if (num == 6) {
        $('#back').css('visibility', 'hidden');
        $('.label-cancel').css('visibility', 'hidden');
        $('#next').val(end);
    }
    else {
        if (num < 6) $('#next').css('visibility', 'visible'); else $('#next').css('visibility', 'hidden');
        if (num > 1) $('#back').css('visibility', 'visible'); else $('#back').css('visibility', 'hidden');
    }
    if (isNext) {
        $('.control-label[data-label="div' + num + '"]').removeClass('label-default').addClass('label-selected');
        $('.cirlce-style[data-id="div' + num + '"]').addClass('selected');
        $('.cirlce-style[data-id="div' + num + '"] svg').css('fill', '#fff');
    }
    else {
        $('.control-label[data-label="div' + num + '"]').removeClass('label-accepted').addClass('label-selected');
        $('.cirlce-style[data-id="div' + num + '"]').html('<img src="../img/pages/setup/step' + num + '.svg" class="ico-circle svg" />');
        imgToSvg();
        $('.cirlce-style[data-id="div' + num + '"]').removeClass('accepted').addClass('selected');
    }
}
function setValueInController(model) {
    $.each(model, function (k, v) {
        if ($('[data-id=' + k + ']').is('input[type="text"]') ||
            $('[data-id=' + k + ']').is('input[type="hidden"]') ||
            $('[data-id=' + k + ']').is('input[type="password"]') ||
            $('[data-id=' + k + ']').is('textarea')) {
            $('[data-id=' + k + ']').attr("value", v);
        }
        else if ($('[data-id=' + k + ']').is('input[type="checkbox"]') ||
            $('[data-id=' + k + ']').is('input[type="radio"]')) {
            if (v) {
                $('[data-id=' + k + ']').prop("checked", true);
            }
            else
                $('[data-id=' + k + ']').prop("checked", false);
        }
        else if ($('[data-id=' + k + ']').is('select')) {
            $('[data-id=' + k + ']').attr("value", v);
        }
        else if ($('[data-id=' + k + ']').is("table")) {
            eval('cbo' + k).SetValue(v);
        }
    });
}

async function getEventLookupCombo($combo, search, code) {
    await ajax({
        url: "GeneralApi/GetGuildList",
        success: function (result) {
            $combo.lookupBuilder().bindData(result);
        }
    });
}

