'use strict';

angular.module('admin-login', [])

    .controller('AdminLoginCtrl', ['$scope', 'session', 'util', '$location', 'logo', 'AppUtil', 'APIName',
        function ($scope, session, util, $location, logo, AppUtil, APIName) {

            angular.element(document).find('#email').focus();
            // Note:
            // We need a dropdown team at here
            $scope.logo = logo;
            $scope.submit = false;

            // default show email field, after validate email will show password field and select company list
            $scope.emailChecked = false;
            $scope.email = '';
            $scope.users = [];
            $scope.selectedUser = {};

            // The way to use alert
            $scope.alerts = [];

            $scope.closeAlert = function (index) {
                $scope.alerts.splice(index, 1);
            };

            // Do login
            $scope.adminLogin = function (valid) {
                if (!valid)
                    return;

                if (!$scope.emailChecked) {
                    // validate email address
                    AppUtil.createRequest(APIName.GET_ADMIN_COMPANIES, {email: $scope.email}, function (data) {

                        $scope.users = data.data;
                        $scope.emailChecked = true;
                        setTimeout(function () {
                            angular.element(document).find('#password').focus();
                        }, 100);

                    }, function (error) {
                        if (error.status === 1400) {
                            AppUtil.showError({
                                text: util.translate("errors.invalid_login_credential")
                            });
                        } else {
                            AppUtil.showError({
                                text: util.translate("sever_errors." + error.status)
                            });
                        }
                        $scope.submit = false;
                    });
                } else {
                    // choose company & login with password
                    // Set as submit
                    $scope.submit = true;

                    // Prevent submit when no change
                    var params = {
                        email: $scope.email,
                        password_hash: $scope.password,
                        keep_login: $scope.keep_me_login
                    };

                    if ($scope.users.length !== 0) {
                        // validate company id
                        if (typeof $scope.selectedUser.id !== 'undefined') {
                            params.company_id = $scope.selectedUser.id;
                        } else {
                            if ($scope.users.length === 1) {
                                params.company_id = $scope.users[0].company_id;
                            } else {
                                $scope.alerts.push({
                                    type: 'danger',
                                    msg: util.translate("message.login.choose_company")
                                });
                                $scope.submit = false;
                                return;
                            }
                        }
                    }

                    session.login(params, function (data, locationPath) {
                        $scope.alerts = [];
                        $location.path(locationPath);
                    }, function (error) {
                        if (error.status === 100) {
                            AppUtil.showError({
                                text: util.translate("errors.invalid_login_credential")
                            });
                        } else {
                            AppUtil.showError({
                                text: util.translate("sever_errors." + error.status)
                            });
                        }
                        $scope.submit = false;
                    }, true);
                }
            };
        }]);
