import { __decorate, __metadata } from "tslib";
import { Component, Input } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { CITIES } from '../../../../../const/city.const';
import { API, DEF } from '../../../../../api';
let IdentityUserUsersUpdateComponent = class IdentityUserUsersUpdateComponent {
    //endregion
    //region Constructor
    constructor(nzModalRef) {
        this.nzModalRef = nzModalRef;
        //endregion
        //region Field
        this.saving = false;
        this.formGroup = new FormGroup({
            userName: new FormControl({ value: '', disabled: true }),
            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}$/)],
            }),
        });
        this.cities = CITIES;
    }
    //endregion
    //region LifeCycle
    ngOnInit() {
        this.formGroup.patchValue(Object.assign(Object.assign({}, this.record), { cities: !this.record.state && this.record.city
                ? [this.record.country, this.record.city]
                : [this.record.country, this.record.state, this.record.city] }));
    }
    //endregion
    //region Method
    handleOk() {
        if (this.formGroup.invalid) {
            return;
        }
        this.saving = true;
        const applicationUser = new DEF.Identity.ApplicationUser(this.formGroup.getRawValue());
        //region country / state / city
        {
            const formControl = this.formGroup.get('cities');
            if (formControl) {
                const citiesValue = 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;
            delete applicationUser.accessFailedCount;
            delete applicationUser.concurrencyStamp;
            delete applicationUser.emailConfirmed;
            delete applicationUser.lockoutEnabled;
            delete applicationUser.lockoutEnd;
            delete applicationUser.normalizedEmail;
            delete applicationUser.normalizedUserName;
            delete applicationUser.permissions;
            delete applicationUser.passwordHash;
            delete applicationUser.phoneNumberConfirmed;
            delete applicationUser.securityStamp;
            delete applicationUser.twoFactorEnabled;
            delete applicationUser.firstName;
            delete applicationUser.lastName;
            delete applicationUser.email;
            delete applicationUser.initialPassword;
        }
        //endregion
        API.Identity.UserController.UpdateUser(undefined, applicationUser).subscribe(() => {
            this.formGroup.reset();
            this.saving = false;
            this.nzModalRef.close(Object.assign(Object.assign({}, this.record), applicationUser));
        }, () => {
            this.saving = false;
        });
    }
    handleCancel() {
        this.nzModalRef.destroy();
    }
};
IdentityUserUsersUpdateComponent.ctorParameters = () => [
    { type: NzModalRef }
];
__decorate([
    Input(),
    __metadata("design:type", DEF.Identity.ApplicationUser)
], IdentityUserUsersUpdateComponent.prototype, "record", void 0);
IdentityUserUsersUpdateComponent = __decorate([
    Component({
        selector: 'kg---identity--user--users--update',
        template: "<div class=\"modal-header\">\r\n  <div class=\"modal-title\">\r\n    {{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.update-user' | i18n }}\r\n  </div>\r\n</div>\r\n\r\n<form (ngSubmit)=\"handleOk()\" [formGroup]=\"formGroup\" [nzNoAnimation]=\"true\" nz-form nzLayout=\"vertical\">\r\n  <div nz-row [nzGutter]=\"12\">\r\n    <div nz-col [nzSm]=\"24\" [nzMd]=\"12\">\r\n      <nz-form-item>\r\n        <nz-form-label [nzFor]=\"'userName'\"> {{ 'app.common.user-name' | i18n }}: </nz-form-label>\r\n        <nz-form-control>\r\n          <input nz-input [formControlName]=\"'userName'\" [attr.id]=\"'userName'\" />\r\n        </nz-form-control>\r\n      </nz-form-item>\r\n    </div>\r\n\r\n    <div nz-col [nzSm]=\"24\" [nzMd]=\"12\">\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 nz-input [formControlName]=\"'name'\" [attr.id]=\"'name'\" />\r\n        </nz-form-control>\r\n      </nz-form-item>\r\n    </div>\r\n\r\n    <div nz-col [nzSm]=\"24\" [nzMd]=\"24\">\r\n      <nz-form-item>\r\n        <nz-form-label [nzFor]=\"'country'\">\r\n          {{\r\n            'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.country-state-city' | i18n\r\n          }}:\r\n        </nz-form-label>\r\n        <nz-form-control>\r\n          <nz-cascader\r\n            [nzOptions]=\"cities\"\r\n            formControlName=\"cities\"\r\n            nzPlaceHolder=\"{{\r\n              'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.country-state-city'\r\n                | i18n\r\n            }}\"\r\n            [nzShowSearch]=\"true\"\r\n          >\r\n          </nz-cascader>\r\n        </nz-form-control>\r\n      </nz-form-item>\r\n    </div>\r\n\r\n    <div nz-col [nzSm]=\"24\" [nzMd]=\"24\">\r\n      <nz-form-item>\r\n        <nz-form-label [nzFor]=\"'street'\">\r\n          {{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.street' | i18n }}:\r\n        </nz-form-label>\r\n        <nz-form-control>\r\n          <input nz-input [formControlName]=\"'street'\" [attr.id]=\"'street'\" />\r\n        </nz-form-control>\r\n      </nz-form-item>\r\n    </div>\r\n\r\n    <div nz-col [nzSm]=\"24\" [nzMd]=\"12\">\r\n      <nz-form-item>\r\n        <nz-form-label [nzFor]=\"'zipCode'\"> {{ 'app.common.zip-code' | 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.invalid-zip-code' | i18n\r\n          }}\"\r\n        >\r\n          <input nz-input [formControlName]=\"'zipCode'\" [attr.id]=\"'zipCode'\" />\r\n        </nz-form-control>\r\n      </nz-form-item>\r\n    </div>\r\n\r\n    <div nz-col [nzSm]=\"24\" [nzMd]=\"12\">\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 nz-input [formControlName]=\"'phoneNumber'\" [attr.id]=\"'phoneNumber'\" />\r\n        </nz-form-control>\r\n      </nz-form-item>\r\n    </div>\r\n\r\n    <div nz-col [nzSm]=\"24\" [nzMd]=\"12\">\r\n      <nz-form-item>\r\n        <nz-form-label [nzFor]=\"'cardNumber'\">\r\n          {{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.card-number' | i18n }}:\r\n        </nz-form-label>\r\n        <nz-form-control>\r\n          <input nz-input [formControlName]=\"'cardNumber'\" [attr.id]=\"'cardNumber'\" />\r\n        </nz-form-control>\r\n      </nz-form-item>\r\n    </div>\r\n\r\n    <div nz-col [nzSm]=\"24\" [nzMd]=\"12\">\r\n      <nz-form-item>\r\n        <nz-form-label [nzFor]=\"'cardHolderName'\">\r\n          {{\r\n            'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.card-holder-name' | i18n\r\n          }}:\r\n        </nz-form-label>\r\n        <nz-form-control>\r\n          <input nz-input [formControlName]=\"'cardHolderName'\" [attr.id]=\"'cardHolderName'\" />\r\n        </nz-form-control>\r\n      </nz-form-item>\r\n    </div>\r\n\r\n    <div nz-col [nzSm]=\"24\" [nzMd]=\"12\">\r\n      <nz-form-item>\r\n        <nz-form-label [nzFor]=\"'expiration'\">\r\n          {{ 'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.expiration' | 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.invalid-expiration' | i18n\r\n          }}\"\r\n        >\r\n          <input nz-input [formControlName]=\"'expiration'\" [attr.id]=\"'expiration'\" placeholder=\"MM/YY\" />\r\n        </nz-form-control>\r\n      </nz-form-item>\r\n    </div>\r\n\r\n    <div nz-col [nzSm]=\"24\" [nzMd]=\"12\">\r\n      <nz-form-item>\r\n        <nz-form-label [nzFor]=\"'securityNumber'\">\r\n          {{\r\n            'projects/fe-ng/src/lib/page/identity/user/users/create/user-users-create.security-number' | i18n\r\n          }}:\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.invalid-security-number'\r\n              | i18n\r\n          }}\"\r\n        >\r\n          <input nz-input [formControlName]=\"'securityNumber'\" [attr.id]=\"'securityNumber'\" placeholder=\"XXX\" />\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\r\n<div class=\"modal-footer\">\r\n  <button (click)=\"handleCancel()\" [disabled]=\"saving\" [nzType]=\"'default'\" nz-button>\r\n    {{ 'app.common.cancel' | i18n }}\r\n  </button>\r\n\r\n  <button\r\n    (click)=\"handleOk()\"\r\n    [disabled]=\"!formGroup.valid || saving\"\r\n    [nzLoading]=\"saving\"\r\n    [nzType]=\"'primary'\"\r\n    nz-button\r\n  >\r\n    {{ 'app.common.save' | i18n }}\r\n  </button>\r\n</div>\r\n"
    }),
    __metadata("design:paramtypes", [NzModalRef])
], IdentityUserUsersUpdateComponent);
export { IdentityUserUsersUpdateComponent };
//# sourceMappingURL=data:application/json;base64, 