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

                $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.user = {
                    user_id: $stateParams.id
                };

                // Do submit
                $scope.submit = false;
                $scope.adminChangePassword = function (valid) {

                    if (!valid)
                        return;

                    $scope.submit = true;

                    // Prevent submit when no change
                    var params = APIName.CHANGE_MEMBER_PASSWORD.params || {};

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

                    var urlObj = {
                        name: APIName.CHANGE_MEMBER_PASSWORD.name + $scope.user.user_id + APIName.CHANGE_MEMBER_PASSWORD.surfix,
                        method: APIName.CHANGE_MEMBER_PASSWORD.method
                    };

                    // call Create Project ajax
                    AppUtil.createRequest(urlObj, params, function (data) {
                        $scope.submit = false;
                        $scope.doBack();
                        AppUtil.showSuccess({
                            type: 'success',
                            text: util.translate(APIName.CHANGE_MEMBER_PASSWORD.success_msg)
                        });

                    }, 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;});
            }]);
