import { Component, OnInit } from '@angular/core';
import { FlexTableComponent } from '../../../../shared/common/FlexTable.component';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FormBuilder, Validators } from '@angular/forms';
import { FrameRequest } from '../../../../shared/frame/frame-request';
import { AppComponent } from '../../../..';
import { FrameFunction } from '../../../../shared/frame/frame_function';

@Component({
  selector: 'app-pl-major-old-new',
  templateUrl: './pl-major-old-new.component.html',
  styleUrls: ['./pl-major-old-new.component.scss']
})
export class PlMajorOldNewComponent extends FlexTableComponent implements OnInit {

  public query = {
    query: "",
    new_school_id: ""
  };
  QUERY_URL = "pl/plMajorOldNew/page.htm";
  ADD_URL = "pl/plMajorOldNew/add.htm";
  DELETE_URL = "pl/plMajorOldNew/delete.htm";
  GET_URL = "pl/plMajorOldNew/get.htm";
  EDIT_URL = "pl/plMajorOldNew/edit.htm";
  MAJOR_LIST_URL = "pl/plMajor/listNewOld.htm?is_new_major=1";
  OLD_MAJOR_LIST_URL = "pl/plMajor/listNewOld.htm?is_new_major=0";
  SCHOOL_URL = "cf/cfOrganization/list.htm?org_type=1&status=1";
  FIND_MAJOR_URL = "pl/plMajor/findMajorSchool.htm";

  majorChooseFlag: boolean = true;
  oldMajorList = [];
  majorIds = [];
  majorList = []; //旧专业列表

  constructor(public bsModalService: BsModalService, fb: FormBuilder) {
    super();
    this.formModel = fb.group({
      id: [null],
      new_major_id: ['', Validators.required],
      new_school_id: ['', Validators.required],
      old_major_id: [null, Validators.required],
      old_major_status: [null, Validators.required]
    });
  }

  ngOnInit() {
    FrameRequest.requestArray(this.OLD_MAJOR_LIST_URL, {}, (array) => {
      this.majorList = array;
    });
  }

  //添加专业
  addMajor() {
    if (this.item.old_major_id === undefined) {
      AppComponent.alert({ title: "信息提示", text: "请选择专业!" });
      return;
    }
    if (this.item.old_major_status === undefined) {
      AppComponent.alert({ title: "信息提示", text: "请选择原专业状态!" });
      return;
    }
    FrameRequest.requestEntity(this.FIND_MAJOR_URL, { id: this.item.old_major_id === undefined ? "" : this.item.old_major_id }, (entity) => {
      this.majorIds = this.oldMajorList.map((major) => major.id);
      if (!this.contains(this.majorIds, entity.id)) {
        entity.old_major_status = this.item.old_major_status;

        this.oldMajorList.push(entity);
      }
      this.majorChooseFlag = false;
    });
  }

  deleteElement(id: number, flag?: string) {
    this.oldMajorList.splice(this.oldMajorList.findIndex(major => major.id === id), 1);
  }

  contains(ids: any, id: number) {
    for (let i = 0; i < ids.length; i++) {
      if (ids[i] == id) return true;
    }
    return false;
  }

  async preGoEditEvent(entity?) {
    this.majorChooseFlag = true;
    this.oldMajorList = [];
    this.majorIds = [];
    return true;
  }

  saveMajor() {
    if (!FrameFunction.validForm(this.formModel)) {
      return;
    }
    if ((this.oldMajorList === undefined || this.oldMajorList.length === 0)
     && this.item.old_major_id !== undefined && this.item.old_major_status !== undefined) {
      let temp = { id: "", old_major_status: "" };
      temp.id = this.item.old_major_id;
      temp.old_major_status = this.item.old_major_status;
      this.oldMajorList.push(temp);
    }
    if (this.oldMajorList === undefined || this.oldMajorList.length === 0) {
      AppComponent.alert({ title: "信息提示", text: "请添加专业!" });
      return;
    }
    let old_major_ids = this.oldMajorList.map(item => item.id);
    let old_major_sts = this.oldMajorList.map(item => item.old_major_status);
    FrameRequest.request(this.ADD_URL, {
      new_major_id: this.item.new_major_id,
      old_major_ids: old_major_ids,
      new_school_id: this.item.new_school_id,
      old_major_status: old_major_sts,
      id: (this.item.id === undefined) ? "" : this.item.id
    }, () => {
      this.hideModal();
      AppComponent.GobalComponent.successMsg("新旧专业衔接设置成功");
      this.framePager.reload();
    });
  }

}
