/**
 * @fileoverview added by tsickle
 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
 */
import * as tslib_1 from "tslib";
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { CITIES } from '../../../../../const/city.const';
import { API, DEF } from '../../../../../api';
import { SettingsService } from '@delon/theme';
import { finalize, tap } from 'rxjs/operators';
var IdentityUserUsersCreateComponent = /** @class */ (function () {
    function IdentityUserUsersCreateComponent(settingService) {
        this.settingService = settingService;
        this.visible = false;
        this.visibleChange = new EventEmitter();
        this.ok = new EventEmitter();
        this.saving = false;
        this.bindWarehouses = [];
        this.warehouses = [];
        this.formGroup = new FormGroup({
            userName: new FormControl('', {
                validators: Validators.required,
            }),
            name: new FormControl('', {
                validators: Validators.required,
            }),
            cities: new FormControl([]),
            street: new FormControl(''),
            zipCode: new FormControl('', {
                validators: [Validators.pattern(/^[0-9]{5,6}$/)],
            }),
            phoneNumber: new FormControl(''),
            cardNumber: new FormControl(''),
            cardHolderName: new FormControl(''),
            expiration: new FormControl('', {
                validators: [Validators.pattern(/^[0-9]{2}\/[0-9]{2}$/)],
            }),
            securityNumber: new FormControl('', {
                validators: [Validators.pattern(/^[0-9]{3}$/)],
            }),
            email: new FormControl('', {
                validators: [Validators.required, Validators.email],
            }),
            initialPassword: new FormControl('', {
                validators: Validators.required,
            }),
        });
        this.initialPasswordVisible = false;
        this.cities = CITIES;
    }
    /**
     * @return {?}
     */
    IdentityUserUsersCreateComponent.prototype.handleOk = /**
     * @return {?}
     */
    function () {
        var _this = this;
        if (this.formGroup.invalid) {
            return;
        }
        this.saving = true;
        /** @type {?} */
        var applicationUser = new DEF.Iwms.ApplicationUser(tslib_1.__assign({}, this.formGroup.getRawValue(), { warehouse: this.settingService.user.sub }));
        //region country / state / city
        {
            /** @type {?} */
            var formControl = this.formGroup.get('cities');
            if (formControl) {
                /** @type {?} */
                var citiesValue = (/** @type {?} */ (formControl.value));
                if (citiesValue.length === 3) {
                    applicationUser.country = citiesValue[0];
                    applicationUser.state = citiesValue[1];
                    applicationUser.city = citiesValue[2];
                }
                else if (citiesValue.length === 2) {
                    applicationUser.country = citiesValue[0];
                    applicationUser.city = citiesValue[1];
                }
            }
        }
        //endregion
        //region delete redundant fields
        {
            delete applicationUser.id;
        }
        //endregion
        API.Iwms.UserController.AddUser(undefined, applicationUser).subscribe((/**
         * @return {?}
         */
        function () {
            API.Iwms.UserWarehouseController.BindWarehouse(undefined, new DEF.Iwms.UserWarehouseQuery({
                // tslint:disable-next-line:no-non-null-assertion
                userName: (/** @type {?} */ (_this.formGroup.get('userName'))).value,
                warehouseCodes: _this.bindWarehouses,
            }))
                .pipe(tap((/**
             * @return {?}
             */
            function () {
                _this.formGroup.reset({
                    cities: [],
                });
                _this.saving = false;
                _this.visibleChange.emit(false);
                _this.ok.emit();
            })), finalize((/**
             * @return {?}
             */
            function () {
                _this.saving = false;
            })))
                .subscribe();
        }), (/**
         * @return {?}
         */
        function () {
            _this.saving = false;
        }));
    };
    /**
     * @return {?}
     */
    IdentityUserUsersCreateComponent.prototype.handleCancel = /**
     * @return {?}
     */
    function () {
        this.formGroup.reset({
            cities: [],
        });
        this.visibleChange.emit(false);
    };
    /**
     * @return {?}
     */
    IdentityUserUsersCreateComponent.prototype.onAfterOpen = /**
     * @return {?}
     */
    function () {
        var _this = this;
        API.Iwms.UserController.GetAllWarehouse()
            .pipe(tap((/**
         * @param {?} data
         * @return {?}
         */
        function (data) {
            _this.warehouses = data.map((/**
             * @param {?} i
             * @return {?}
             */
            function (i) { return ({
                value: i.value,
                label: i.label,
            }); }));
        })))
            .subscribe();
    };
    IdentityUserUsersCreateComponent.decorators = [
        { type: Component, args: [{
                    selector: 'kg---identity--user--users--create',
                    template: "<nz-modal\r\n  (nzAfterOpen)=\"onAfterOpen()\"\r\n  (nzOnCancel)=\"handleCancel()\"\r\n  (nzOnOk)=\"handleOk()\"\r\n  [nzCancelDisabled]=\"saving\"\r\n  [nzClosable]=\"!saving\"\r\n  [nzKeyboard]=\"false\"\r\n  [nzMaskClosable]=\"false\"\r\n  [nzOkDisabled]=\"!formGroup.valid || saving\"\r\n  [nzOkLoading]=\"saving\"\r\n  [nzVisible]=\"visible\"\r\n  nzTitle=\"{{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.add-user' | i18n }}\"\r\n  nzWidth=\"748px\"\r\n>\r\n  <form (ngSubmit)=\"handleOk()\" [formGroup]=\"formGroup\" [nzNoAnimation]=\"true\" nz-form nzLayout=\"vertical\">\r\n    <div [nzGutter]=\"12\" nz-row>\r\n      <div [nzMd]=\"12\" [nzSm]=\"24\" nz-col>\r\n        <nz-form-item>\r\n          <nz-form-label [nzFor]=\"'userName'\" [nzRequired]=\"true\"> {{ 'app.common.user-name' | i18n }}: </nz-form-label>\r\n          <nz-form-control\r\n            nzErrorTip=\"{{\r\n              'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.username-is-required' | i18n\r\n            }}\"\r\n          >\r\n            <input [attr.id]=\"'userName'\" [formControlName]=\"'userName'\" nz-input />\r\n          </nz-form-control>\r\n        </nz-form-item>\r\n      </div>\r\n\r\n      <div [nzMd]=\"12\" [nzSm]=\"24\" nz-col>\r\n        <nz-form-item>\r\n          <nz-form-label [nzFor]=\"'name'\" [nzRequired]=\"true\"> {{ 'app.common.name' | i18n }}: </nz-form-label>\r\n          <nz-form-control nzErrorTip=\"{{ 'app.common.form.name.required' | i18n }}\">\r\n            <input [attr.id]=\"'name'\" [formControlName]=\"'name'\" nz-input />\r\n          </nz-form-control>\r\n        </nz-form-item>\r\n      </div>\r\n\r\n      <div [nzMd]=\"12\" [nzSm]=\"24\" nz-col>\r\n        <nz-form-item>\r\n          <nz-form-label [nzFor]=\"'email'\" [nzRequired]=\"true\"> {{ 'app.common.email' | i18n }}: </nz-form-label>\r\n          <nz-form-control [nzErrorTip]=\"emailErrorTipTpl\">\r\n            <ng-template #emailErrorTipTpl let-control>\r\n              <ng-container *ngIf=\"control.hasError('email')\">\r\n                {{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.invalid-email' | i18n }}\r\n              </ng-container>\r\n              <ng-container *ngIf=\"control.hasError('required')\">\r\n                {{\r\n                  'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.email-is-required' | i18n\r\n                }}\r\n              </ng-container>\r\n            </ng-template>\r\n\r\n            <input [attr.id]=\"'email'\" [formControlName]=\"'email'\" nz-input />\r\n          </nz-form-control>\r\n        </nz-form-item>\r\n      </div>\r\n\r\n      <div [nzMd]=\"12\" [nzSm]=\"24\" nz-col>\r\n        <nz-form-item>\r\n          <nz-form-label [nzFor]=\"'initialPassword'\" [nzRequired]=\"true\">\r\n            {{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.initial-password' | i18n }}:\r\n          </nz-form-label>\r\n          <nz-form-control\r\n            nzErrorTip=\"{{\r\n              'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.initial-password-is-required'\r\n                | i18n\r\n            }}\"\r\n          >\r\n            <nz-input-group [nzSuffix]=\"nzSuffixTpl\">\r\n              <input\r\n                [attr.id]=\"'initialPassword'\"\r\n                [formControlName]=\"'initialPassword'\"\r\n                [type]=\"initialPasswordVisible ? 'text' : 'password'\"\r\n                nz-input\r\n              />\r\n            </nz-input-group>\r\n\r\n            <ng-template #nzSuffixTpl>\r\n              <i\r\n                (click)=\"initialPasswordVisible = !initialPasswordVisible\"\r\n                [ngStyle]=\"{\r\n                  cursor: 'pointer'\r\n                }\"\r\n                [nzType]=\"initialPasswordVisible ? 'eye-invisible' : 'eye'\"\r\n                nz-icon\r\n              ></i>\r\n            </ng-template>\r\n          </nz-form-control>\r\n        </nz-form-item>\r\n      </div>\r\n\r\n      <div [nzMd]=\"24\" [nzSm]=\"24\" nz-col>\r\n        <nz-form-item>\r\n          <nz-form-label [nzFor]=\"'phoneNumber'\"> {{ 'app.common.phone-number' | i18n }}: </nz-form-label>\r\n          <nz-form-control>\r\n            <input [attr.id]=\"'phoneNumber'\" [formControlName]=\"'phoneNumber'\" nz-input />\r\n          </nz-form-control>\r\n        </nz-form-item>\r\n      </div>\r\n\r\n      <div [nzMd]=\"24\" [nzSm]=\"24\" nz-col>\r\n        <nz-form-item>\r\n          <nz-form-label [nzFor]=\"'bind_warehouse'\">\r\n            {{ 'src_lib_page_identity_user_zb_users_create_identity_user_users_create__warehouse' | i18n }}:\r\n          </nz-form-label>\r\n          <nz-form-control>\r\n            <nz-select\r\n              [(ngModel)]=\"bindWarehouses\"\r\n              [attr.id]=\"'bind_warehouse'\"\r\n              [ngModelOptions]=\"{ standalone: true }\"\r\n              nzMode=\"multiple\"\r\n            >\r\n              <nz-option\r\n                *ngFor=\"let option of warehouses\"\r\n                [nzLabel]=\"option.label\"\r\n                [nzValue]=\"option.value\"\r\n              ></nz-option>\r\n            </nz-select>\r\n          </nz-form-control>\r\n        </nz-form-item>\r\n      </div>\r\n    </div>\r\n\r\n    <button [hidden]=\"true\"></button>\r\n  </form>\r\n</nz-modal>\r\n"
                }] }
    ];
    /** @nocollapse */
    IdentityUserUsersCreateComponent.ctorParameters = function () { return [
        { type: SettingsService }
    ]; };
    IdentityUserUsersCreateComponent.propDecorators = {
        visible: [{ type: Input }],
        visibleChange: [{ type: Output }],
        ok: [{ type: Output }]
    };
    return IdentityUserUsersCreateComponent;
}());
export { IdentityUserUsersCreateComponent };
if (false) {
    /** @type {?} */
    IdentityUserUsersCreateComponent.prototype.visible;
    /** @type {?} */
    IdentityUserUsersCreateComponent.prototype.visibleChange;
    /** @type {?} */
    IdentityUserUsersCreateComponent.prototype.ok;
    /** @type {?} */
    IdentityUserUsersCreateComponent.prototype.saving;
    /** @type {?} */
    IdentityUserUsersCreateComponent.prototype.bindWarehouses;
    /** @type {?} */
    IdentityUserUsersCreateComponent.prototype.warehouses;
    /** @type {?} */
    IdentityUserUsersCreateComponent.prototype.formGroup;
    /** @type {?} */
    IdentityUserUsersCreateComponent.prototype.initialPasswordVisible;
    /** @type {?} */
    IdentityUserUsersCreateComponent.prototype.cities;
    /**
     * @type {?}
     * @private
     */
    IdentityUserUsersCreateComponent.prototype.settingService;
}
//# sourceMappingURL=data:application/json;base64, 