import {Component, OnInit} from '@angular/core';
import {FrameDetailComponent} from "../../core/detail/frame.detail";
import {BsModalRef} from "ngx-bootstrap/modal";
import {FormBuilder, Validators} from "@angular/forms";
import {FrameService} from "../../../../core/service/frame.service";

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

  deptArray = [];

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.detailForm = fb.group({
      'dept_code': [null],
      'dept_name': [null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(128)])],
      'dept_manager': [null, Validators.compose([Validators.required, Validators.minLength(1), Validators.maxLength(32)])],
      'dept_addr': [null],
      'dept_tel': [null],
      'dept_fax': [null],
      'dept_pid': [null, Validators.required],
      'dept_desc': [null]
    });
  }

  ngOnInit() {
    setTimeout(() => {
      const array = FrameDeptDetailComponent.getDeptArray(this.ResponseArray);
      for (const s of array) {
        s.dept_note = `${s.dept_name}`;
        for (let i = 1; i <= s.dept_level; i++) {
          s.dept_note = `&nbsp;&nbsp;&nbsp;&nbsp;` + s.dept_note;
        }
        this.deptArray.push(s);
      }

    }, 10);
  }


  changeDeptPid($event) {
    for (const r of this.deptArray) {
      if (r.dept_id + '' === this.currentEntity.dept_pid + '') {
        this.currentEntity.dept_level = r.dept_level + 1;
        return;
      }
    }
  }

  public static getDeptArray(resArray) {
    const topDeptName = FrameService.getParamValue('FrameDeptName');
    const dept = {'dept_id': 0, 'dept_name': (topDeptName ? topDeptName : '最高级部门'), 'dept_level': 0};
    FrameDeptDetailComponent.findChildDept(resArray, dept);
    const array = [];
    array.push(dept);
    FrameDeptDetailComponent.makeDeptArray(dept, array);
    return array;
  }

  public static findChildDept(deptArray, dept) {
    const array = new Array();
    for (const v of deptArray) {
      if (v['dept_id'] !== dept['dept_id'] && v['dept_pid'] === dept['dept_id']) {
        FrameDeptDetailComponent.findChildDept(deptArray, v);
        array.push(v);
        dept.childArray = array;
      }
    }
  }


  public static makeDeptArray(dept, array) {
    if (dept.childArray && dept.childArray.length > 0) {
      for (const c of dept.childArray) {
        array.push(c);
        this.makeDeptArray(c, array);
      }
    }
  }

  public static makeOptrDeptArray(deptArray) {
    const array = FrameDeptDetailComponent.getDeptArray(deptArray);
    let currentArray = [];
    for (let index = 0; index < array.length; index++) {
      // 从当前操作员的所属部门开始
      if (array[index].dept_id === FrameService.FrameOptr.optr_dept) {
        currentArray = array.slice(index);
        break;
      }
    }
    const sArray = [];
    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;
      }
      sArray.push(s);
    }
    return sArray;
  }


}
