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_type': [null, Validators.compose([Validators.required])],
      'dept_addr': [null],
      'dept_tel': [null],
      'dept_fax': [null],
      'dept_pid':  [null, Validators.compose([Validators.required])],
      'dept_desc': [null],
      'teach_addr': [null],
      'status': [null, Validators.compose([Validators.required])]
    });
  }

  ngOnInit() {
    setTimeout(() => {
      debugger;
      const array = FrameDeptDetailComponent.getDeptArray(this.ResponseArray);
      debugger;
      for (const s of array) {
        if (s.dept_type == 'examArea') {//过滤掉考区
          continue;
        }
        s.dept_note = `${s.dept_code}|` + `${FrameDeptDetailComponent.getOrgType(s)}|` + `${s.dept_name}`;
        this.deptArray.push(s);
      }
    }, 10);

  }

  initDefautEntity() {
    if (this.detailMode === 'add') {
      this.currentEntity.status = 1;//默认是启用
    } else {
      debugger;
      console.log("status---" + this.currentEntity.status);
    }
  }

  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) {
    debugger;
    const topDeptName = FrameService.getParamValue('FrameDeptName');
    FrameDeptDetailComponent.findChildDept(resArray, FrameService.FrameDept);
    const array = [];
    //顶级部门--登陆返回所有部门
    if (FrameService.FrameDept.dept_id == 0) {
      FrameService.FrameDept.dept_code = '0';
      FrameService.FrameDept.dept_note = '';
      array.push(FrameService.FrameDept);
      for (const v of resArray) {
        array.push(v);
      }
      return array;
    } else {
      //其他非顶级--返回当前部门和子部门
      array.push(FrameService.FrameDept);
      FrameDeptDetailComponent.makeDeptArray(FrameService.FrameDept, 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);
    const array = 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}(` + FrameDeptDetailComponent.getOrgType(s) + `)`;
      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;
  }

  static getOrgType(org) {
    if (org.dept_type == 'Admin') {
      return "管理";
    } else if (org.dept_type == 'Admiss') {
      return "招办";
    } else if (org.dept_type == 'Profes') {
      return "专业老师";
    }else if (org.dept_type == 'School') {
      return "学校";
    }else if (org.dept_type == 'SecondLevel') {
      return "二级学院";
    }
    return "";
  }

}
