'use strict';

angular.module('admin-change-email', [])
        .controller('ChangeEmailMemberCtrl',
            ['$scope', 'AppUtil', 'APIName', 'APIStatus', '$stateParams', 'app', 'util', '$window',
            function ($scope, AppUtil, APIName, APIStatus, $stateParams, app, util, $window) {

                // Do back button
                $scope.doBack = function () {
                    $window.history.back();
                };

                $scope.pattern = app.EMAIL_PATTERN;

                $scope.member = {
                    user_id: $stateParams.id
                };

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

                    $scope.submit = true;

                    // Prevent submit when no change
                    var params = {};
                    params['team_id'] =  $stateParams.team_id;
                    params['new_email'] = $scope.mail_address;
                    params['company_id'] = $stateParams.company_id;
                    //params['confirm_email'] = $scope.confirm_mail_address;

                    var objUrl = {
                        method: APIName.CHANGE_MEMBER_EMAIL.method,
                        name: APIName.CHANGE_MEMBER_EMAIL.name + $scope.member.user_id + APIName.CHANGE_MEMBER_EMAIL.surfix
                    };
                    // call Create Project ajax
                    AppUtil.createRequest(objUrl, params, function (data) {

                        $scope.submit = false;
                        $scope.doBack();
                        AppUtil.showSuccess({
                            text: util.translate(APIName.CHANGE_MEMBER_EMAIL.success_msg),
                            type: 'success'
                        });

                    }, function (error) {
                        AppUtil.showError({
                            text: util.translate("sever_errors." + error.status)
                        });
                        $scope.submit = false;
                    });
                };
            }]);
