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 {eNameValidator} from '../../../../shared/validator/ename.validator';
import {passswordValidator} from '../../../../shared/validator/password.validator';
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} from 'ngx-bootstrap';
import {AjaxService} from '../../../../core/ajax/ajax.service';

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

  deptArray = [];

  constructor(bsModalRef: BsModalRef, fb: FormBuilder, san: DomSanitizer) {
    super(bsModalRef, san);
    this.valForm = fb.group({
      'optr_name': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(32)])],
      'optr_sex': [null],
      'optr_status': [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_dept': [null],
      'optr_desc': [null]
    });
  }

  ngOnInit() {
    AjaxService.requestArray('frame/dept/all.htm', {}, (ResponseArray) => {
      const array = FrameDeptDetailComponent.getDeptArray(ResponseArray);
      let currentArray = [];
      for (let index = 0; index < array.length; index++) {
        // 从当前操作员的所属部门开始
        if (array[index].dept_id === FrameService.FrameOptr.optr_dept) {
          currentArray = array.slice(index);
          break;
        }
      }
      for (const s of array) {
        s.dept_note = `${s.dept_name}`;
        const dept_level = FrameService.FrameDept ? FrameService.FrameDept.dept_level : 0;
        for (let i = dept_level; i < s.dept_level; i++) {
          s.dept_note = `&nbsp;&nbsp;` + s.dept_note;
        }
        this.deptArray.push(s);
      }
    });
    this.currentEntity.optr_image = undefined;
  }


}
