import { __decorate, __metadata } from "tslib";
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { NzModalService } from 'ng-zorro-antd/modal';
import { TranslateService } from '@ngx-translate/core';
import { timer } from 'rxjs';
import { SettingsService } from '@delon/theme';
import { API, DEF } from '../../api/index';
import { IdentityService } from '../../service/identity.service';
var KengicChangePasswordComponent = /** @class */ (function () {
    //endregion
    //region Constructor
    function KengicChangePasswordComponent(nzModalService, translateService, identityService, settingsService) {
        this.nzModalService = nzModalService;
        this.translateService = translateService;
        this.identityService = identityService;
        this.settingsService = settingsService;
        //region Input
        this.visible = false;
        //endregion
        //region Output
        this.visibleChange = new EventEmitter();
        //endregion
        //region Field
        this.saving = false;
        this.formGroup = new FormGroup({
            oldPassword: new FormControl('', {
                validators: Validators.required,
            }),
            newPassword: new FormControl('', {
                validators: [Validators.required, Validators.minLength(6)],
            }),
            confirmNewPassword: new FormControl('', {
                validators: [Validators.required, Validators.minLength(6), this.confirmationValidator.bind(this)],
            }),
        });
        this.oldPasswordVisible = false;
        this.newPasswordVisible = false;
        this.confirmNewPasswordVisible = false;
    }
    //endregion
    //region Method
    KengicChangePasswordComponent.prototype.handleOk = function () {
        var _this = this;
        if (this.formGroup.invalid) {
            return;
        }
        this.saving = true;
        var passwordQuery = new DEF.Identity.PasswordQuery({
            oldPassword: this.formGroup.controls.oldPassword.value,
            newPassword: this.formGroup.controls.newPassword.value,
            userName: this.settingsService.user.unique_name,
        });
        API.Identity.UserController.ChangePassword(undefined, passwordQuery).subscribe(function () {
            _this.saving = false;
            _this.visibleChange.emit(false);
            _this.nzModalService.create({
                nzTitle: _this.translateService.instant('app.common.tip'),
                nzContent: _this.translateService.instant('projects/fe-ng/src/lib/component/change-password/kengic-change-password.success'),
                nzClosable: false,
                nzFooter: null,
                nzWidth: '748px',
            });
            timer(4000).subscribe(function () {
                _this.identityService.logout();
            });
        }, function () {
            _this.saving = false;
        });
    };
    KengicChangePasswordComponent.prototype.handleCancel = function () {
        this.formGroup.reset();
        this.visibleChange.emit(false);
    };
    KengicChangePasswordComponent.prototype.updateConfirmValidator = function () {
        var _this = this;
        Promise.resolve().then(function () { return _this.formGroup.controls.confirmNewPassword.updateValueAndValidity(); });
    };
    KengicChangePasswordComponent.prototype.confirmationValidator = function (formControl) {
        if (!String(formControl.value).trim()) {
            return { required: true };
        }
        else if (formControl.value !== this.formGroup.controls.newPassword.value) {
            return { confirm: true, error: true };
        }
        return {};
    };
    KengicChangePasswordComponent.ctorParameters = function () { return [
        { type: NzModalService },
        { type: TranslateService },
        { type: IdentityService },
        { type: SettingsService }
    ]; };
    __decorate([
        Input(),
        __metadata("design:type", Boolean)
    ], KengicChangePasswordComponent.prototype, "visible", void 0);
    __decorate([
        Output(),
        __metadata("design:type", EventEmitter)
    ], KengicChangePasswordComponent.prototype, "visibleChange", void 0);
    KengicChangePasswordComponent = __decorate([
        Component({
            selector: 'kengic-change-password',
            template: "<nz-modal\r\n  nzTitle=\"{{ 'projects/fe-ng/src/lib/component/change-password/kengic-change-password.change-password' | i18n }}\"\r\n  nzWidth=\"748px\"\r\n  [nzVisible]=\"visible\"\r\n  [nzMaskClosable]=\"false\"\r\n  [nzKeyboard]=\"false\"\r\n  [nzOkDisabled]=\"!formGroup.valid || saving\"\r\n  [nzOkLoading]=\"saving\"\r\n  [nzClosable]=\"!saving\"\r\n  [nzCancelDisabled]=\"saving\"\r\n  (nzOnCancel)=\"handleCancel()\"\r\n  (nzOnOk)=\"handleOk()\"\r\n>\r\n  <form nz-form [formGroup]=\"formGroup\" nzLayout=\"vertical\" [nzNoAnimation]=\"true\" (ngSubmit)=\"handleOk()\">\r\n    <div nz-row [nzGutter]=\"12\">\r\n      <div nz-col [nzSm]=\"24\" [nzMd]=\"24\">\r\n        <nz-form-item>\r\n          <nz-form-label [nzFor]=\"'oldPassword'\" [nzRequired]=\"true\">\r\n            {{ 'projects/fe-ng/src/lib/component/change-password/kengic-change-password.old-password' | i18n }}:\r\n          </nz-form-label>\r\n          <nz-form-control\r\n            nzErrorTip=\"{{\r\n              'projects/fe-ng/src/lib/component/change-password/kengic-change-password.old-password.required' | i18n\r\n            }}\"\r\n          >\r\n            <nz-input-group [nzSuffix]=\"oldPasswordSuffixTpl\">\r\n              <input\r\n                nz-input\r\n                [formControlName]=\"'oldPassword'\"\r\n                [attr.id]=\"'oldPassword'\"\r\n                [type]=\"oldPasswordVisible ? 'text' : 'password'\"\r\n              />\r\n            </nz-input-group>\r\n\r\n            <ng-template #oldPasswordSuffixTpl>\r\n              <i\r\n                nz-icon\r\n                [nzType]=\"oldPasswordVisible ? 'eye-invisible' : 'eye'\"\r\n                [ngStyle]=\"{\r\n                  cursor: 'pointer'\r\n                }\"\r\n                (click)=\"oldPasswordVisible = !oldPasswordVisible\"\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 nz-col [nzSm]=\"24\" [nzMd]=\"24\">\r\n        <nz-form-item>\r\n          <nz-form-label [nzFor]=\"'newPassword'\" [nzRequired]=\"true\">\r\n            {{ 'projects/fe-ng/src/lib/component/change-password/kengic-change-password.new-password' | i18n }}:\r\n          </nz-form-label>\r\n          <nz-form-control [nzErrorTip]=\"newPasswordconfirmNewPasswordErrorTipTpl\">\r\n            <nz-input-group [nzSuffix]=\"newPasswordSuffixTpl\">\r\n              <input\r\n                nz-input\r\n                [formControlName]=\"'newPassword'\"\r\n                [attr.id]=\"'newPassword'\"\r\n                [type]=\"newPasswordVisible ? 'text' : 'password'\"\r\n              />\r\n            </nz-input-group>\r\n\r\n            <ng-template #newPasswordconfirmNewPasswordErrorTipTpl let-control>\r\n              <ng-container *ngIf=\"control.hasError('required')\">\r\n                {{\r\n                  'projects/fe-ng/src/lib/component/change-password/kengic-change-password.new-password.required' | i18n\r\n                }}\r\n              </ng-container>\r\n              <ng-container *ngIf=\"control.hasError('minlength')\">\r\n                {{ 'projects/fe-ng/src/lib/component/change-password/kengic-change-password.min-length' | i18n }}\r\n              </ng-container>\r\n            </ng-template>\r\n\r\n            <ng-template #newPasswordSuffixTpl>\r\n              <i\r\n                nz-icon\r\n                [nzType]=\"newPasswordVisible ? 'eye-invisible' : 'eye'\"\r\n                [ngStyle]=\"{\r\n                  cursor: 'pointer'\r\n                }\"\r\n                (click)=\"newPasswordVisible = !newPasswordVisible\"\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 nz-col [nzSm]=\"24\" [nzMd]=\"24\">\r\n        <nz-form-item>\r\n          <nz-form-label [nzFor]=\"'confirmNewPassword'\" [nzRequired]=\"true\">\r\n            {{ 'projects/fe-ng/src/lib/component/change-password/kengic-change-password.confirm-new-password' | i18n }}:\r\n          </nz-form-label>\r\n          <nz-form-control [nzErrorTip]=\"confirmNewPasswordErrorTipTpl\">\r\n            <nz-input-group [nzSuffix]=\"confirmNewPasswordSuffixTpl\">\r\n              <input\r\n                nz-input\r\n                [formControlName]=\"'confirmNewPassword'\"\r\n                [attr.id]=\"'confirmNewPassword'\"\r\n                [type]=\"confirmNewPasswordVisible ? 'text' : 'password'\"\r\n                (ngModelChange)=\"updateConfirmValidator()\"\r\n              />\r\n            </nz-input-group>\r\n\r\n            <ng-template #confirmNewPasswordErrorTipTpl let-control>\r\n              <ng-container *ngIf=\"control.hasError('required')\">\r\n                {{\r\n                  'projects/fe-ng/src/lib/component/change-password/kengic-change-password.confirm-new-password.required'\r\n                    | i18n\r\n                }}\r\n              </ng-container>\r\n              <ng-container *ngIf=\"control.hasError('minlength')\">\r\n                {{ 'projects/fe-ng/src/lib/component/change-password/kengic-change-password.min-length' | i18n }}\r\n              </ng-container>\r\n              <ng-container *ngIf=\"!control.hasError('minlength') && control.hasError('confirm')\">\r\n                {{\r\n                  'projects/fe-ng/src/lib/component/change-password/kengic-change-password.confirm-new-password.confirm'\r\n                    | i18n\r\n                }}\r\n              </ng-container>\r\n            </ng-template>\r\n\r\n            <ng-template #confirmNewPasswordSuffixTpl>\r\n              <i\r\n                nz-icon\r\n                [nzType]=\"confirmNewPasswordVisible ? 'eye-invisible' : 'eye'\"\r\n                [ngStyle]=\"{\r\n                  cursor: 'pointer'\r\n                }\"\r\n                (click)=\"confirmNewPasswordVisible = !confirmNewPasswordVisible\"\r\n              ></i>\r\n            </ng-template>\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"
        }),
        __metadata("design:paramtypes", [NzModalService,
            TranslateService,
            IdentityService,
            SettingsService])
    ], KengicChangePasswordComponent);
    return KengicChangePasswordComponent;
}());
export { KengicChangePasswordComponent };
//# sourceMappingURL=data:application/json;base64, 