///define(['app'], function (app) {
(function () {
  var app = angular.module('myApp');
  var cId = 'login';
  var cVer = 1;
  ///app.register.controller(cId, [
  app.controller(cId, [
    '$scope', '$rootScope', '$http', '$window', '$location', 'VS4Library', 'myFactory', '$routeParams',
    function ($scope, $rootScope, $http, $window, $location, VS4Library, myFactory, $routeParams) {
      var self = this;
      $rootScope.controllerMsg = cId + ' : ' + myFactory.AppVersion() + '.' + cVer;
      //...
      $scope.loginForm = {
        UserName: '',
        Password: '',
        RememberMe: true,
        returnUrl: $routeParams.returnUrl,
        loginFailure: false
      };

      $window.sessionStorage.ActiveCompany = "001";
      $rootScope.ActiveCompany = "001";

      jQuery("#userName").focus();

      $scope.login = function () {

        var opts = {
          lines: 11, // The number of lines to draw
          length: 11, // The length of each line
          width: 3, // The line thickness
          radius: 15, // The radius of the inner circle
          corners: 1, // Corner roundness (0..1)
          rotate: 0, // The rotation offset
          direction: 1, // 1: clockwise, -1: counterclockwise
          color: '#000', // #rgb or #rrggbb or array of colors
          speed: 1, // Rounds per second
          trail: 60, // Afterglow percentage
          shadow: false, // Whether to render a shadow
          hwaccel: false, // Whether to use hardware acceleration
          className: 'spinner', // The CSS class to assign to the spinner
          zIndex: 2e9, // The z-index (defaults to 2000000000)
          top: '50%', // Top position relative to parent
          left: '50%' // Left position relative to parent
        };
        var target = document.getElementById('progress');
        var spinner = new Spinner(opts).spin(target);

        VS4Library.Login($scope.loginForm)
          .then(function (response) {
            if (response.data.Token !== "") {

              $window.sessionStorage.token = response.data.Token;
              $window.sessionStorage.authenticated = true;
              $window.sessionStorage.ActiveUser = $scope.loginForm.UserName;
              $window.sessionStorage.userID = response.data.UserID;
              $window.sessionStorage.EmpCode = response.data.EmpCode;
              $window.sessionStorage.EmpName = response.data.EmpName;
              $window.sessionStorage.userType = response.data.Type;
              $window.sessionStorage.isLogin = true;
              $window.sessionStorage.isStaff = false;

              $window.sessionStorage.ActiveCompany = "001";
              $rootScope.ActiveCompany = $window.sessionStorage.ActiveCompany;

              var b = response.data.BranchCode;
              if (b == "" || b == undefined) { b = "00000"; }
              $window.sessionStorage.ActiveBranch = b;
              $rootScope.ActiveBranch = $window.sessionStorage.ActiveBranch;

              $rootScope.ActiveUser = $window.sessionStorage.ActiveUser;
              $rootScope.isLogin = true;

              if ($window.sessionStorage.userType === "0") {
                $window.sessionStorage.isStaff = false;
                $rootScope.isStaff = false;
                $location.path('/dashboard');
              } else {
                $window.sessionStorage.isStaff = true;
                $rootScope.isStaff = true;
                $location.path('/dashboard');
              }
            } else {
              resetUser();
            }
          }, function (error) {
            resetUser();
          });

        function resetUser() {
          $window.sessionStorage.token = "";
          $window.sessionStorage.authenticated = false;
          $window.sessionStorage.userID = -9;
          $window.sessionStorage.ActiveUser = "";
          $window.sessionStorage.EmpCode = "";
          $window.sessionStorage.EmpName = "";
          $window.sessionStorage.userType = 0;
          $window.sessionStorage.isLogin = false;
          $window.sessionStorage.isStaff = false;

          // $window.sessionStorage.ActiveCompany = "";
          // $rootScope.ActiveCompany = "";

          $window.sessionStorage.ActiveBranch = "";
          $rootScope.ActiveBranch = "";

          $rootScope.ActiveUser = "";
          $rootScope.isLogin = false;
          $rootScope.isStaff = false;

          $scope.loginForm.loginFailure = true;

          spinner.stop();
        }

      };
    }
  ]);
  ///});
})();



(function () {
  var app = angular.module('myApp');
  var cId = 'rootmenu';
  var cVer = 1;
  ///app.register.controller(cId, [
  app.controller(cId, [
    '$scope', '$rootScope', '$window', 'VS4Library',
    function ($scope, $rootScope, $window, VS4Library) {

      $scope.init = function () {
        VS4Library.init();
      }

      //            $scope.exitFromApp = function () {
      //                if (navigator && navigator.app) {
      //                    navigator.app.exitApp();
      //                } else {
      //                    if (navigator && navigator.device) {
      //                        navigator.device.exitApp();
      //                    }
      //                }
      //            }

    }
  ]);
  ///});
})();