


$(document).ready(function () {
    $("#jqxLoader").jqxLoader({ width: 100, height: 60, imagePosition: 'top' });
    adminlogin();
});

//admin login
function adminlogin() {
    var login = (function () {
        var userbean;
        //var ServerURL = "http://localhost:64759/api/";
        function Admin(a, b) {
            this.Username = a;
            this.Password = b;

        }
        return {
            init: function () {
  

                var userid = localStorage.getItem("userid");
                //if (userid == null) {
                    //login.loadusername();
                    login.loadusername();
                    $(document).on("click", "#login-submit", login.loginsuccess);
               // }
                //else
                //{
                //    setTimeout(function () {
                //        window.location.href = "/Login/Home";
                //    }, 2000);
                   
                //    return;
                //}
                //$(document).click("#login-submit", login.loginsuccess);
            },
            getUser: function () {
                userbean = new Admin(
                 s = $('#txtUsername').val('selectedvalue'),
                $("#Password").val()


                    );
            },
            loginsuccess: function (e) {

                e.preventDefault();
                login.getUser();
                if (userbean.Username == "") {
                    document.getElementById('msg').innerHTML = "Please Fill UserName!!!";
                    document.getElementById('Password').style.border = "2px solid #0072c6";
                    document.getElementById('txtUsername').style.border = "2px solid red";
                    document.getElementById('txtUsername').value = '';
                    return;
                }
                else if (userbean.Password == "") {
                    document.getElementById('msg').innerHTML = "Please Fill Password!!!";
                    document.getElementById('Password').style.border = "2px solid red";
                    document.getElementById('txtUsername').style.border = "2px solid #0072c6";
                    document.getElementById('Password').value = '';
                    return;
                }
                else if (userbean.Username == "" && userbean.Password == "") {
                    document.getElementById('msg').innerHTML = "Type Fill username and password!!!";
                    document.getElementById('txtUsername').style.border = "2px solid red";
                    document.getElementById('Password').style.border = "2px solid red";
                    document.getElementById('txtUsername').value = '';
                    document.getElementById('Password').value = '';
                }
                $('#jqxLoader').jqxLoader('open');
                $.ajax({
                    url: "/Login/LoginSuccess",
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    data: '{"Userid":' + userbean.Username + ',"Pwd":' + userbean.Password + '}',
                    success: function (data) {
                        $('#jqxLoader').jqxLoader('close');
                        if (data.ok) {
                            var gedata = data.data;   
                            var userid = localStorage.getItem("userid");
                            if (gedata[0].Id != userid) {
                                localStorage.clear();
                            }
                            localStorage.setItem("userid", gedata[0].Id);
                            localStorage.setItem("username", gedata[0].UserName);
                            localStorage.setItem("priv", gedata[0].Priv);                     
                           // sessionStorage.setItem("Userid",  userbean.Username);
                   
                            window.location.href = "/Login/Home";
                        }
                        else {

                            document.getElementById('msg').innerHTML = "Please Check username and password!!!";

                            //$('.validateTips').addClass("ui-state-error");
                            //$('.validateTips').text("Check Your Username and Password");
                        }

                    },
                  

                });
            },
            //loadusername: function ()
            //{

            //    var objuser = {};
            //    $.ajax({
            //        url: "/Login/SelectUser",
            //        type: "POST",
            //        dataType: "json",
            //        contentType: "application/json; charset=utf-8",
            //        data: JSON.stringify(objuser),
            //        success: function (data) {
            //            if(data.length !=0)
            //            {
            //                var departmentdropDownListSource =
            //                       {
            //                           datatype: "json",
            //                           datafields: [
            //                               { name: 'UserName', type: 'string' },
            //                               { name: 'Id', type: 'number' }
            //                           ],
            //                           id: 'id'
            //                       };
            //                var Deptdropdown = new $.jqx.dataAdapter(departmentdropDownListSource, { autoBind: true, async: false });
            //                $("#Username").jqxDropDownList({  source: data, name: "UserName" ,value:"Id", height: 38, width: '100%' });
            //                $("#Username").addClass("intro");                           
            //                $("#Username").focus();
            //            }
            //        },
            //        error: function (msg, url, line) {
            //            alert('Application problem ' + msg);
            //        }


            //    });
            //},


            //jq

            loadusername: function () {

                var objuser = {};
                $.ajax({
                    url: "/Login/SelectUser",
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(objuser),
                    success: function (data) {
                        if (data.length != 0) {
                            var dataAdapter = new $.jqx.dataAdapter(data);
                            $("#txtUsername").jqxDropDownList({
                                source: dataAdapter, selectedIndex: 0, placeHolder: "UserName", displayMember: "UserName", valueMember: "Id", width: 200, height: 30,
                            });
                            $("#Password").jqxPasswordInput({ width: '200', height: 30, placeHolder: "Enter password" });

                        }
                    },
                    error: function (msg, url, line) {
                        alert('Application problem ' + msg);
                    }


                });
            },





        }
    })();
    window.login = login;
    login.init();

}

