import {Component, OnInit} from '@angular/core';
import {FrameDetailComponent} from "../../core/detail/frame.detail";
import {FormBuilder, Validators} from "@angular/forms";
import {AjaxService} from "../../../../core/service/ajax.service";
import {passswordValidator} from "../../../../shared/validators/password.validator";
import {BsModalRef} from "ngx-bootstrap/modal";
import {MsgService} from "../../../../core/service/msg.service";

@Component({
  selector: 'app-frame-optr-password',
  templateUrl: './frame-optr-password.component.html',
  styleUrls: ['./frame-optr-password.component.scss']
})
export class FrameOptrPasswordComponent extends FrameDetailComponent implements OnInit {

  optrModel = 'edit';
  optr: any;

  constructor(bsModalRef: BsModalRef, private fb: FormBuilder) {
    super(bsModalRef);
  }

  ngOnInit() {
    this.detailForm = this.fb.group({
      'old_pass': [null, this.optrModel === 'admin' ? null : Validators.compose([Validators.required, Validators.minLength(6)])],
      'passwordGroup': this.fb.group({
        'password': [null, Validators.compose([Validators.required, Validators.minLength(6)])],
        'pconfirm': [null, Validators.compose([Validators.required, Validators.minLength(6)])]
      }, {validator: passswordValidator})
    });
  }


  savePassword() {
    this.detailForm.markAllAsTouched();
    if (!this.validForm()) {
      return;
    }
    MsgService.confirmAlert('确定要修改登录密码吗？', () => {
      AjaxService.request('frame/optr/passwd.htm', this.currentEntity, () => {
        MsgService.successAlert('操作员登录密码修改成功，请使用新密码登录！', () => {
          this.closeWindow();
        });
      }, {RequestWithLoading: true});
    });
  }

}

