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

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

  optrModel = 'edit';
  optr: any;

  constructor(public bsModalRef: BsModalRef, public fb: FormBuilder) {
    super(bsModalRef);
    this.valForm = this.fb.group({
      'old_pass': [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})
    });
  }

  ngOnInit() {
    setTimeout( () => {
      if (this.bsModalRef.content.optrModel === 'admin') {
        this.optrModel = 'admin';
        this.valForm.get('old_pass').setValidators(null);
      }
      this.currentEntity = { optr_id: this.bsModalRef.content.optr.optr_id};
    }, 10);
  }


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

}
