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

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

            $scope.company = {
                company_id: $stateParams.id
            };
            // Do submit
            $scope.submit = false;
            $scope.userChangePassword = function (valid) {
                if (!valid)
                    return;

                $scope.submit = true;

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

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

                var objUrl = {
                    method: APIName.SUPER_ADMIN_CHANGE_COMPANY_PASSWORD.method,
                    name: APIName.SUPER_ADMIN_CHANGE_COMPANY_PASSWORD.name + $scope.company.company_id
                        + APIName.SUPER_ADMIN_CHANGE_COMPANY_PASSWORD.surfix
                };

                // Do change team admin password
                AppUtil.createRequest(objUrl, params, function (res) {
                        $scope.submit = false;
                        $state.go("companyProfile", {id: $stateParams.id});
                    }, function (error) {
                        AppUtil.showError({
                            text: util.translate("sever_errors." + error.status)
                        });
                        $scope.submit = false;
                    }
                );

            };
        }]);

