import {Component, OnInit} from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import {DomSanitizer} from '@angular/platform-browser';
import {mobileValidator} from '../../../../shared/validator/mobile.validator';
import {AjaxService} from '../../../../core/ajax/ajax.service';
import {FrameDeptDetailComponent} from '../../dept/frame-dept-detail/frame-dept-detail.component';
import {FrameService} from '../../../../core/frame/frame.service';
import {FrameEditComponent} from '../../core/FrameEditComponent';
import {BsModalRef, BsModalService} from 'ngx-bootstrap';
import {MsgService} from '../../../../core/msg/msg.service';
import {FrameOptrPasswordComponent} from '../frame-optr-password/frame-optr-password.component';

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

  constructor(public modalService: BsModalService, bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.valForm = fb.group({
      'optr_name': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(32)])],
      'optr_sex': [null],
      'optr_email': [null],
      'optr_mobile': [null, Validators.compose([Validators.required, mobileValidator])],
      'optr_tel': [null],
      'optr_fax': [null],
      'optr_addr': [null],
      'optr_post': [null],
      'optr_desc': [null]
    });
  }

  ngOnInit() {
    this.currentEntity = Object.assign({}, FrameService.FrameOptr);
    this.currentEntity.optr_image = undefined;
    this.currentEntity.dept_name = FrameService.FrameDept.dept_name;
    this.currentEntity.resMap = undefined;
  }

  modifyOptr() {
    this.submitForm('frame/optr/edit.htm', {successMsg: '操作员信息已修改!'},  () => {
      MsgService.successAlert('操作员信息已完善，系统将重新加载！', undefined, () => {
        location.reload();
      });
    });
  }

  openPassword() {
    const modal = Object.assign({},  {
      animated: true,
      keyboard: false,
      backdrop: true,
      ignoreBackdropClick: true,
      initialState: {optrModel: 'edit', optr: FrameService.FrameOptr},
      class: 'modal-5'
    });
    this.modalService.show(FrameOptrPasswordComponent, modal);
    this.bsModalRef.hide();
  }


  uploadOptrImage() {
    MsgService.successAlert('操作员照片信息已保存，系统将重新加载！', undefined, () => {
      location.reload();
    });
  }
}
