import {Component, OnInit} from '@angular/core';
import {NzMessageService, NzModalService} from "ng-zorro-antd";
import {FormBuilder} from "@angular/forms";
import {HttpClient} from "../../../../../node_modules/@angular/common/http";
import {SystemService} from "../../../services/system.service";
import {Router} from "@angular/router";

@Component({
  selector: 'app-position-rel',
  templateUrl: './position-rel.component.html',
  styleUrls: ['./position-rel.component.scss']
})
export class PositionRelComponent implements OnInit {
  // 专业搜索
  subjectSearch = '';
  // 专业集合
  subjectList = [];
  // 已选择的专业
  selectSubjects = [];
  //职位类型树
  jobTypeTree = [];
  // 选择的职位类型
  selectTree = [];
  // 职位类型搜索
  searchValue = '';

  // 专业名称 搜索关联关系列表
  searchSubjectName = '';
  // 分页
  pageIndex = 1;
  pageSize = 3;
  total = 0;
  loading = true;
  isLoadingSearch = false;
  saveLoading = false;
  requestData = [];
  isLoadingDelete = false;
  constructor(
    private fb: FormBuilder,
    private router: Router,  // 路由
    private message: NzMessageService,
    private http: HttpClient,
    private systemService: SystemService,
    private modalService: NzModalService,
  ) { }

  ngOnInit() {
    this.getSubjectList(this.subjectSearch);
    this.getJobTypeTree();
    // 调用请求方法
    this.goRequest();
  }

  // 查询专业列表
  getSubjectList(subjectName) {
    this.systemService.subjectList(subjectName, data => {
      // console.log(data);
      this.subjectList = data['return_data'];
    });
  }
  // 获取职位类别树
  getJobTypeTree() {
    this.systemService.jobTypeTree(data => {
      console.log(data);
      this.jobTypeTree = data['return_data'];
    });
  }
  // 勾选专业
  selectSubject(value: string[]): void {
    // console.log(value);
    this.selectSubjects = value;
  }
  // 搜索专业，按名称
  searchSubject(value: string): void {
    // console.log(value);
    this.getSubjectList(value);
  }
  // 勾选职位类别
  nzCheck(e): void {
    this.selectTree = [];
    for (const i of e['nodes']) {
      console.log(i['origin']);
      if (i['origin']['checked'] == true) {
        this.selectTree.push(i['origin']['title']);
      }
      if (i['origin']['children'] != null) {
        this.getArrayList(i['origin']['children']);
      }
    }
  }

  // 保存关联关系
  addSubjectJob(){
    if (this.selectSubjects.length == 0) {
      this.modalService.warning({
        nzTitle: '提示',
        nzContent: '请选择专业'
      });
      return;
    }
    if (this.selectTree.length == 0) {
      this.modalService.warning({
        nzTitle: '提示',
        nzContent: '请选择职位类型'
      });
      return;
    }
    this.saveLoading = true;
    // 定义请求数据
    const params = {
      subjectNames: this.selectSubjects,
      jobTypeNames: this.selectTree
    };
    this.systemService.addSubjectJob(params, data => {
      this.saveLoading = false;
      if (data['return_code'] === '000000') {
        this.refreshWeb();
      } else {
        this.modalService.warning({
          nzTitle: '提示',
          nzContent: data['return_msg']
        });
      }
    });
  }

// 查询
  goRequest(reset: boolean = false) {
    this.loading = false;
    if (reset) {
      this.pageIndex = 1;
    }
    this.systemService.subjectJobTypeList(this.pageIndex,this.pageSize,this.searchSubjectName, data => {
      if (data['return_code'] === '000000') {
        this.requestData = data['return_data'].list;
        this.total = data['return_data'].total;
        this.isLoadingSearch = false;
      } else {
        this.modalService.warning({
          nzTitle: '提示',
          nzContent: data['return_msg']
        });
      }
    });
  }

  // 搜索
  goSearch() {
    this.isLoadingSearch = true;
    this.goRequest();
  }
  // 删除提示
  showDeleteConfirm(id): void {
    this.modalService.confirm({
      nzTitle: '您确定要删除吗?',
      nzOkText: '是',
      nzOkType: 'danger',
      nzOnOk: () => this.goDelete(id),
      nzCancelText: '否',
    });
  }

  // 删除
  goDelete(id) {
    this.isLoadingDelete = true;
    this.systemService.deleteSubjectJobType(id, data => {
      if (data['return_code'] === '000000') {
        this.isLoadingDelete = false;
        this.goRequest();
      } else {
        this.modalService.warning({
          nzTitle: '提示',
          nzContent: data['return_msg']
        });
      }
    });
  }

  cancelButton() {
    this.subjectSearch = '';
    // 已选择的专业
    this.selectSubjects = [];
    // 选择的职位类型
    this.selectTree = [];
    // 职位类型搜索
    this.searchValue = '';
    this.saveLoading = false;
    this.getSubjectList(this.subjectSearch);
    this.getJobTypeTree();
  }
  // 刷新页面
  refreshWeb(){
    this.cancelButton();
    // 专业名称 搜索关联关系列表
    this.searchSubjectName = '';
    // 分页
    this.pageIndex = 1;
    this.pageSize = 3;
    this.total = 0;
    // 调用请求方法
    this.goRequest();
  }

  public getArrayList(children) {
    for (const data of children) {
      if (data['checked'] == true) {
        this.selectTree.push(data['title']);
      }
      if (data['children'] != null) {
        this.getArrayList(data['children']);
      }
    }
  }
}
