

var indexApp = angular.module('IndexApp', ['ngStorage', 'ngDialog', 'angularSpinner']);

indexApp.controller('IndexCtrl', ['$scope', '$http', '$sessionStorage', 'ngDialog', 'usSpinnerService', 'rememberMe',
    function ($scope, $http, $sessionStorage, ngDialog, usSpinnerService, rememberMe) {
         
        //Localization(sessionStorage.selectedLanguage, 'index', $scope, sessionStorage);
        sessionStorage.clear();
        $scope.UserName = '';
        $scope.Password = '';
        //Remeber password

        if (rememberMe('7ZXYZ@L') && rememberMe('UU@#90')) {
            var regex = new RegExp("₹", 'g');
            var uName = (Base64.decode(rememberMe('7ZXYZ@L').replace(regex, '=')));
            var pwd = (Base64.decode(rememberMe('UU@#90').replace(regex, '=')));
            $scope.UserName = uName.trim();
            $scope.Password = pwd.trim();
        }

        function remember() {

            if ($('#remember_me').prop("checked") == true) {
                var regex = new RegExp("=", 'g');
                rememberMe('7ZXYZ@L', (Base64.encode($scope.UserName.trim())).replace(regex, '₹'));
                rememberMe('UU@#90', (Base64.encode($scope.Password.trim())).replace(regex, '₹'));
            } else {
                rememberMe('7ZXYZ@L', '');
                rememberMe('UU@#90', '');
            }
        };
        $scope.Login = function (Type) {
            sessionStorage.ControlDisplayValues = null;
            if (Type == "eng") {
                sessionStorage.selectedLanguage = "en-US";
            }
            else if (Type == "arabic") {
                sessionStorage.selectedLanguage = "ar-AE";
            }
            if (validateLogin()) {
	                 $scope.loading = true;
                $.ajax({
                    url: ComonServiceUrl + "ValidateLogin/" + $scope.UserName + "/" + Base64.encode($scope.Password.toLowerCase()),
                    type: "GET",
                    jsonpCallback: "loginResponse",
                    contentType: "application/javascript",
                    dataType: "jsonp",
                    async: false,
                    success: function (result) {
                        if (result.ValidateLoginResult == "null") {
                            $scope.loading = false;
                            var dialog = ngDialog.open({
                                template:
                                    '<p>Username and Password doesnt Match</p>' +
                                    '<div class="ngdialog-buttons"><button type="button" class="ngdialog-button ngdialog-button-Success" ng-click="closeThisDialog(1)">Ok</button></div>',
                                plain: true
                            });
                            dialog.closePromise.then(function (data) {
                                console.log('ngDialog closed' + (data.value === 1 ? ' using the button' : '') + ' and notified by promise: ' + data.id);

                            });

                        }
                        else {
                            remember();
                            var data = JSON.parse(result.ValidateLoginResult);
                            sessionStorage.DistrictOID = data.districtId;

                            sessionStorage.ORGOID = data.OrgOID;
                            sessionStorage.UserOID = data.UserOID;
                            sessionStorage.RoleType = data.RoleType;
                            sessionStorage.RoleID = data.RoleId;                          
                            sessionStorage.GroupOID = 0;
                            sessionStorage.UserName = data.DisplayName;
                            sessionStorage.TokenID = data.Login;
                            sessionStorage.password = Base64.encode($scope.Password);
                            sessionStorage.OrderId = data.OrderId;
                            sessionStorage.notify = 1;
                           

                            
                           
                            if (data.OrderId >2) {
                                var tableName = "COMPANY";
                                $.ajax({
                                    cache: true,
                                    url: ComonServiceUrl + "GetDropDownListMaster/" + tableName + "/" + sessionStorage.UserOID + "/" + 0 + "/" + sessionStorage.ORGOID + "/" + sessionStorage.GroupOID,
                                    data: {

                                    },
                                    type: "GET",
                                    contentType: "application/javascript",
                                    dataType: "jsonp",
                                 
                                    success: function (result) {
                                        $scope.loading = false;
                                        if (result.length != 0) {
                                            $scope.CompanyList = result;
                                            sessionStorage.CompanyOID = $scope.CompanyList[0].OID;
                                            sessionStorage.value = $scope.CompanyList[0].Value;
                                            sessionStorage.CityID = $scope.CompanyList[0].CityOID;
                                            sessionStorage.SPEED = $scope.CompanyList[0].speed;
                                            sessionStorage.Notification = $scope.CompanyList[0].Notification;
                                            sessionStorage.Rupees = $scope.CompanyList[0].Rupees;
                                            sessionStorage.PhoneCode = $scope.CompanyList[0].PhoneCode;
                                          
                                            //sessionStorage.values = 0;
                                            $('.CompanyList').show();
                                            window.location.href = "Site/Pages/dashboard.html";
                                        }
                                        else {
                                            sessionStorage.CompanyOID = 0;
                                            $('.CompanyList').hide();
                                            window.location.href = "Site/Pages/dashboard.html";
                                        }
                                    }
                                });
                            }
                            else if (data.OrderId == 2) {
                     
                                sessionStorage.CompanyOID = data.CompanyId;
                                var tableName = "COMPANY";
                                $.ajax({
                                    cache: true,
                                    url: ComonServiceUrl + "GetDropDownListMaster/" + tableName + "/" + sessionStorage.UserOID + "/" + 0 + "/" + sessionStorage.ORGOID + "/" + sessionStorage.GroupOID,
                                    data: {

                                    },
                                    type: "GET",
                                    contentType: "application/javascript",
                                    dataType: "jsonp",

                                    success: function (result) {
                                        $scope.loading = false;
                                        if (result.length != 0) {
                                            $scope.CompanyList = result;
                                          
                                            sessionStorage.value = $scope.CompanyList[0].Value;
                                            sessionStorage.CityID = $scope.CompanyList[0].CityOID;
                                            sessionStorage.SPEED = $scope.CompanyList[0].speed;
                                         
                                            sessionStorage.Notification = $scope.CompanyList[0].Notification;
                                            sessionStorage.Rupees = $scope.CompanyList[0].Rupees;
                                            sessionStorage.PhoneCode = $scope.CompanyList[0].PhoneCode;
                                          
                                            //sessionStorage.values = 0;
                                            $('.CompanyList').show();
                                            window.location.href = "Site/Pages/dashboard.html";
                                        }
                                        else {
                                            sessionStorage.CompanyOID = 0;
                                            $('.CompanyList').hide();
                                            window.location.href = "Site/Pages/dashboard.html";
                                        }
                                    }
                                });
                                $('.CompanyList').hide();
                                window.location.href = "Site/Pages/dashboard.html";
                            }
                            else  {
                                sessionStorage.CompanyOID = data.CompanyId;
                                $('.CompanyList').hide();
                                //  $('.telecaler').hide();
                                window.location.href = "Site/Pages/dashboard.html";
                            }
                            
                          


                        }

                    },
                    
                error: function(XMLHttpRequest, textStatus, errorThrown) { 
                    alert("Status: " + textStatus); alert("Error: " + errorThrown); 
                }     
                });
            }
        };
        $scope.arabiclang = function (value) {

            sessionStorage.lang = value;

        }
    }]);

