'use strict';
angular.module('admin-change-password', [])
    .controller('AdminChangePassword', ['$scope', 'AppUtil', 'APIName', 'APIStatus', '$stateParams', 'util', '$window', '$state',
        function ($scope, AppUtil, APIName, APIStatus, $stateParams, util, $window, $state) {

            $scope.getPasswordPolicySetting = function () {
                var params = {
                    team_id: $stateParams.team_id,
                    type: 3,
                };
                var objUrlPasswordPolicy = {
                    method: APIName.GET_TEAM_SETTING_BY_TYPE.method,
                    url: APIName.GET_TEAM_SETTING_BY_TYPE.name
                };
                AppUtil.createRequest(objUrlPasswordPolicy, params, function (data) {
                    var passwordPolicy = JSON.parse(data.data.password_setting);
                    $scope.password_pattern = passwordPolicy.regex;
                    $scope.pw_max_length = passwordPolicy.max;
                    $scope.password_error = AppUtil.getPasswordPolicyError(passwordPolicy);
                }, function (error) {
                    AppUtil.showError({text: util.translate("sever_errors." + error.status)});
                    $window.history.back();
                });
            };
            $scope.getPasswordPolicySetting();
            // Do back button
            $scope.doBack = function () {
                $window.history.back();
            };
            $scope.team = {
                team_id: $stateParams.team_id
            };
            // Do submit
            $scope.submit = false;
            $scope.adminChangePassword = function (valid) {

                if (!valid)
                    return;

                $scope.submit = true;

                // Prevent submit when no change
                var params = {};

                params['new_password'] = AESEncrypter.encode($scope.password);
                params['confirm_password'] = AESEncrypter.encode($scope.confirm_password);

                var objUrl = {
                    method: APIName.ADMIN_CHANGE_PASSWORD.method,
                    name: APIName.ADMIN_CHANGE_PASSWORD.name + $scope.team.team_id + APIName.ADMIN_CHANGE_PASSWORD.surfix
                };

                // Do change team admin password
                AppUtil.createRequest(objUrl, params, function (data) {
                    AppUtil.createToast({
                        'text': util.translate('default_toast.save_success'),
                        'type': 'success'
                    });
                    $scope.submit = false;
                    $state.go("adminTeamProfile", {company_id: $stateParams.company_id, id: $scope.team.team_id});
                }, function (error) {
                    AppUtil.showError({
                        text: util.translate("sever_errors." + error.status)
                    });
                    $scope.submit = false;
                });

            };
            $('#link_password').on('keypress', function(e) {if (e.which === 32) return false;});
        }]);

