'use strict';

angular
    .module('list-email', [])
    .controller('ListEmailModal', [
        '$scope', 'AppUtil', '$uibModalInstance', 'data', 'util', 'userConfig', 'APIName', 'configServer',
        'session', 'company', '_', '$state', '$uibModal',
        function ($scope, AppUtil, $uibModalInstance, data, util, userConfig, APIName, configServer,
                  session, company, _, $state, $uibModal) {
            $scope.currentUser = session.getUser();
            $scope.companyId = company;
            $scope.onClose = function () {
                $uibModalInstance.close();
            };
            $scope.listEmailImport = data.list_email_import;
            $scope.listEmailExists = data.list_email_exists;
            $scope.listEmailError = data.list_email_error;

            $scope.getError = function (item) {
                if (item.error_type !== 12) {
                    return util.translate("error_type_CSV." + item.error_type);
                } else {
                    return AppUtil.getPasswordPolicyError(JSON.parse(item.data_attached));
                }
            };

            $scope.contentEmail =  util.translate("label.team.payment_online", {
                count: $scope.listEmailExists.length
            });
            $scope.contentError = $scope.listEmailError.length > 1 ? util.translate("label.team.error_lists", {
                count: $scope.listEmailError.length
            }) : util.translate("label.team.error_list", {
                count: $scope.listEmailError.length
            });

            $scope.importTeams = function (override) {
                var params = {};
                params['company_id'] = $scope.currentUser.role_id === userConfig.SUPER_ADMIN.role ? $scope.companyId : $scope.currentUser.company_id;
                params['list_team'] = $scope.listEmailImport;
                if (override === true) {
                    //add list exists in list email import
                    _.forEach($scope.listEmailExists, function (item) {
                        $scope.listEmailImport.push(item);
                    });
                    //import team
                    importTeam(params);
                } else {
                    // remove list exists and continue import
                    if ($scope.listEmailImport.length > 0) {
                        importTeam(params);
                    } else {
                        $uibModalInstance.close();
                        AppUtil.createToast({
                            text: util.translate("label.team.no_data"),
                            type: 'warning'
                        });
                        if ($state.current.name === "teamCreate") {
                            $state.go("teamList");
                        }
                        if ($state.current.name === "teamCreateBySuperAdmin") {
                            $state.go("companyProfile", {id: $scope.companyId});
                        }
                    }
                }
            };

            function importTeam(param) {
                var objUrl = {
                    url: APIName.CREATE_TEAM_FROM_CSV.name,
                    method: APIName.CREATE_TEAM_FROM_CSV.method
                };

                AppUtil.createRequest(objUrl, param, function (response) {
                    $uibModalInstance.close();
                    if (response.data.error_list.length > 0) {
                        return $uibModal.open({
                                windowClass: 'bootstrap-dialog type-primary',
                                backdrop: false,
                                templateUrl: 'js/components/import_user/error_modal.html',
                                controller: 'ErrorModal',
                                resolve: {
                                    data: response.data
                                }
                            }
                        );
                    } else {
                        $uibModalInstance.close();
                        AppUtil.showSuccess({
                            text: util.translate("label.team.import_success")
                        });
                        if ($state.current.name === "teamCreate") {
                            $state.go("teamList");
                        }
                        if ($state.current.name === "teamCreateBySuperAdmin") {
                            $state.go("companyProfile", {id: $scope.companyId});
                        }
                    }
                }, function (error) {
                    AppUtil.showError({
                        text: util.translate("sever_errors." + error.status)
                    });
                });
            }
        }]
    );
