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 () {
    function KengicChangePasswordComponent(nzModalService, translateService, identityService, settingsService) {
        this.nzModalService = nzModalService;
        this.translateService = translateService;
        this.identityService = identityService;
        this.settingsService = settingsService;
        this.confirmNewPasswordVisible = 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.newPasswordVisible = false;
        this.oldPasswordVisible = false;
        this.saving = false;
        this.visible = false;
        this.visibleChange = new EventEmitter();
    }
    KengicChangePasswordComponent.prototype.handleCancel = function () {
        this.formGroup.reset();
        this.visibleChange.emit(false);
    };
    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__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.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\n  (nzOnCancel)=\"handleCancel()\"\n  (nzOnOk)=\"handleOk()\"\n  [nzCancelDisabled]=\"saving\"\n  [nzClosable]=\"!saving\"\n  [nzKeyboard]=\"false\"\n  [nzMaskClosable]=\"false\"\n  [nzOkDisabled]=\"!formGroup.valid || saving\"\n  [nzOkLoading]=\"saving\"\n  [nzVisible]=\"visible\"\n  nzTitle=\"{{ 'projects/fe-ng/src/lib/component/change-password/kengic-change-password.change-password' | i18n }}\"\n  nzWidth=\"748px\"\n>\n  <form (ngSubmit)=\"handleOk()\" [formGroup]=\"formGroup\" [nzNoAnimation]=\"true\" nz-form nzLayout=\"vertical\">\n    <div [nzGutter]=\"12\" nz-row>\n      <div [nzMd]=\"24\" [nzSm]=\"24\" nz-col>\n        <nz-form-item>\n          <nz-form-label [nzFor]=\"'oldPassword'\" [nzRequired]=\"true\">\n            {{ 'projects/fe-ng/src/lib/component/change-password/kengic-change-password.old-password' | i18n }}:\n          </nz-form-label>\n          <nz-form-control nzErrorTip=\"{{ 'projects/fe-ng/src/lib/component/change-password/kengic-change-password.old-password.required' | i18n }}\">\n            <nz-input-group [nzSuffix]=\"oldPasswordSuffixTpl\">\n              <input [attr.id]=\"'oldPassword'\" [formControlName]=\"'oldPassword'\" [type]=\"oldPasswordVisible ? 'text' : 'password'\" nz-input />\n            </nz-input-group>\n\n            <ng-template #oldPasswordSuffixTpl>\n              <i\n                (click)=\"oldPasswordVisible = !oldPasswordVisible\"\n                [ngStyle]=\"{\n                  cursor: 'pointer'\n                }\"\n                [nzType]=\"oldPasswordVisible ? 'eye-invisible' : 'eye'\"\n                nz-icon\n              ></i>\n            </ng-template>\n          </nz-form-control>\n        </nz-form-item>\n      </div>\n\n      <div [nzMd]=\"24\" [nzSm]=\"24\" nz-col>\n        <nz-form-item>\n          <nz-form-label [nzFor]=\"'newPassword'\" [nzRequired]=\"true\">\n            {{ 'projects/fe-ng/src/lib/component/change-password/kengic-change-password.new-password' | i18n }}:\n          </nz-form-label>\n          <nz-form-control [nzErrorTip]=\"newPasswordconfirmNewPasswordErrorTipTpl\">\n            <nz-input-group [nzSuffix]=\"newPasswordSuffixTpl\">\n              <input [attr.id]=\"'newPassword'\" [formControlName]=\"'newPassword'\" [type]=\"newPasswordVisible ? 'text' : 'password'\" nz-input />\n            </nz-input-group>\n\n            <ng-template #newPasswordconfirmNewPasswordErrorTipTpl let-control>\n              <ng-container *ngIf=\"control.hasError('required')\">\n                {{ 'projects/fe-ng/src/lib/component/change-password/kengic-change-password.new-password.required' | i18n }}\n              </ng-container>\n              <ng-container *ngIf=\"control.hasError('minlength')\">\n                {{ 'projects/fe-ng/src/lib/component/change-password/kengic-change-password.min-length' | i18n }}\n              </ng-container>\n            </ng-template>\n\n            <ng-template #newPasswordSuffixTpl>\n              <i\n                (click)=\"newPasswordVisible = !newPasswordVisible\"\n                [ngStyle]=\"{\n                  cursor: 'pointer'\n                }\"\n                [nzType]=\"newPasswordVisible ? 'eye-invisible' : 'eye'\"\n                nz-icon\n              ></i>\n            </ng-template>\n          </nz-form-control>\n        </nz-form-item>\n      </div>\n\n      <div [nzMd]=\"24\" [nzSm]=\"24\" nz-col>\n        <nz-form-item>\n          <nz-form-label [nzFor]=\"'confirmNewPassword'\" [nzRequired]=\"true\">\n            {{ 'projects/fe-ng/src/lib/component/change-password/kengic-change-password.confirm-new-password' | i18n }}:\n          </nz-form-label>\n          <nz-form-control [nzErrorTip]=\"confirmNewPasswordErrorTipTpl\">\n            <nz-input-group [nzSuffix]=\"confirmNewPasswordSuffixTpl\">\n              <input\n                (ngModelChange)=\"updateConfirmValidator()\"\n                [attr.id]=\"'confirmNewPassword'\"\n                [formControlName]=\"'confirmNewPassword'\"\n                [type]=\"confirmNewPasswordVisible ? 'text' : 'password'\"\n                nz-input\n              />\n            </nz-input-group>\n\n            <ng-template #confirmNewPasswordErrorTipTpl let-control>\n              <ng-container *ngIf=\"control.hasError('required')\">\n                {{ 'projects/fe-ng/src/lib/component/change-password/kengic-change-password.confirm-new-password.required' | i18n }}\n              </ng-container>\n              <ng-container *ngIf=\"control.hasError('minlength')\">\n                {{ 'projects/fe-ng/src/lib/component/change-password/kengic-change-password.min-length' | i18n }}\n              </ng-container>\n              <ng-container *ngIf=\"!control.hasError('minlength') && control.hasError('confirm')\">\n                {{ 'projects/fe-ng/src/lib/component/change-password/kengic-change-password.confirm-new-password.confirm' | i18n }}\n              </ng-container>\n            </ng-template>\n\n            <ng-template #confirmNewPasswordSuffixTpl>\n              <i\n                (click)=\"confirmNewPasswordVisible = !confirmNewPasswordVisible\"\n                [ngStyle]=\"{\n                  cursor: 'pointer'\n                }\"\n                [nzType]=\"confirmNewPasswordVisible ? 'eye-invisible' : 'eye'\"\n                nz-icon\n              ></i>\n            </ng-template>\n          </nz-form-control>\n        </nz-form-item>\n      </div>\n    </div>\n\n    <button [hidden]=\"true\"></button>\n  </form>\n</nz-modal>\n"
        }),
        __metadata("design:paramtypes", [NzModalService,
            TranslateService,
            IdentityService,
            SettingsService])
    ], KengicChangePasswordComponent);
    return KengicChangePasswordComponent;
}());
export { KengicChangePasswordComponent };
//# sourceMappingURL=data:application/json;base64, 