'use strict';

angular.module('create-company', [])

    .controller('CreateCompanyController', [
        '$scope', 'AppUtil', 'APIName', 'app', '$state', 'util', 'session', '$window',
        function ($scope, AppUtil, APIName, app, $state, util, session, $window) {
            $scope.script_pattern = app.SCRIPT_PATTERN;

            $scope.isJapaneseLanguage = session.isJapanese();

            $scope.pattern = app.EMAIL_PATTERN;

            $scope.listCurrency = [
                {id: 1, name: "USD"},
                {id: 2, name: "VND"},
                {id: 3, name: "YEN"}
            ];

            $scope.listCycleBilling = [
                {id: 1, value: 30},
                {id: 2, value: 60},
                {id: 3, value: 90}
            ];

            $scope.listLanguage = [
                {id: 1, name: util.translate("language.en"), value: 'en'},
                {id: 2, name: util.translate("language.ja"), value: 'ja'},
                {id: 3, name: util.translate("language.vi"), value: 'vi'}
            ];

            $scope.newCompany = {
                company_name: "",
                mail_address: "",
                password: "",
                billing_cycle: null,
                currency: null,
                language: null
            };
            $scope.newCompany.billing_cycle = $scope.listCycleBilling[0];
            $scope.newCompany.currency = $scope.listCurrency[0];
            $scope.newCompany.language = $scope.listLanguage[0];
            // Do submit signup new user
            $scope.submit = false;
            $scope.createCompany = function (valid) {
                if (!valid)
                    return;

                $scope.submit = true;
                // Prevent submit when no change
                var params = APIName.ADMIN_CREATE_COMPANY.params || {};

                params['company_name'] = $scope.newCompany.company_name;
                params['first_name'] = $scope.newCompany.first_name;
                params['middle_name'] = $scope.newCompany.middle_name;
                params['last_name'] = $scope.newCompany.last_name;
                params['mail_address'] = $scope.newCompany.mail_address;
                params['hash_password'] = AESEncrypter.encode($scope.newCompany.password);
                params['trial'] = ($scope.isTrial === 1);

                if (typeof $scope.newCompany.billing_cycle !== 'undefined') {
                    params['billing_cycle'] = $scope.newCompany.billing_cycle.value;
                } else {
                    params['billing_cycle'] = $scope.listCycleBilling[0].value;
                }

                if (typeof $scope.newCompany.currency !== 'undefined') {
                    params['currency'] = $scope.newCompany.currency.name;
                } else {
                    params['currency'] = $scope.listCurrency[0].name;
                }

                if (typeof $scope.newCompany.language !== 'undefined') {
                    params['lang'] = $scope.newCompany.language.value;
                } else {
                    params['lang'] = $scope.listLanguage[0].value;
                }

                // call Create Project ajax
                AppUtil.createRequest(APIName.ADMIN_CREATE_COMPANY, params, function (data) {
                        AppUtil.createToast({
                            'text': util.translate("default_toast.create_success"),
                            'type': 'success'
                        });
                        $state.go("companyList");
                        $scope.submit = false;
                    }, function (error) {
                        AppUtil.showError({
                            text: util.translate("sever_errors." + error.status)
                        });
                        $scope.submit = false;
                    }
                );
            };
            $scope.initDefaultPlan = function () {
                $scope.itemPriceClick(1);

            };

            $scope.doBack = function () {
                $window.history.back();
            }
        }])

    // Create directive to watch list change
    .directive('planitemdirective', function () {
        return {
            restrict: 'A',
            link: function ($scope, element, attrs) {
                $scope.$watch(attrs.value, function (v) {
                    if (attrs.value === '20')
                        $scope.initDefaultPlan();
                });
            }
        };
    });
