/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
'use strict';

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

                // Do back button
                $scope.doBack = function () {
                    $state.go("adminTeamProfile", {company_id: $stateParams.company_id, id: $stateParams.id});
                };

                $scope.pattern = app.EMAIL_PATTERN;

                $scope.team = {
                    team_id: $stateParams.id
                };

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

                    $scope.submit = true;

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

                    params['new_email'] = $scope.mail_address;
                    params['company_id'] = $stateParams.company_id;

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

                    // call Create Project ajax
                    AppUtil.createRequest(objUrl, params, function (data) {

                        $scope.submit = false;
                        AppUtil.createToast({
                            'text': util.translate('default_toast.change_mail_success'),
                            'type': 'success'
                        });
                        $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;
                    });
                };
            }]);
