import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { FlexTableComponent } from '../../../../shared/common/FlexTable.component';
import { FrameRequest } from '../../../../shared/frame/frame-request';
import { AppComponent } from '../../../../app.component';
import * as _ from 'lodash';
@Component({
  selector: 'app-group-enrol',
  templateUrl: './group-enrol.component.html',
  styleUrls: ['./group-enrol.component.scss']
})
export class GroupEnrolComponent extends FlexTableComponent implements OnInit {

  query = {
    major_id: '',
    std_name: '',
    cert_no: '',
    ticket_no: '',
    course_id: '',
    course_code: '',
    course_name: ''
  };
  MAJOR_LIST_URL = "pl/plMajor/list.htm";
  MAJOR_URL = "pl/plMajor/findMajor.htm";
  COURSE_PLAN_URL = "std/stdEnrol/choose.htm";
  STD_LIST_URL = "std/stdEnrol/page.htm";
  ADD_ENROL_URL = "std/stdEnrol/addOne.htm";
  EXAM_AREA_URL = "pl/plExamAreaCourse/list.htm";
  FIND_STD_URL = "/std/stdRegInfo/findStd.htm";
  FIND_COURSE_URL = "pl/plCourse/findByCourse.htm";
  FIND_OPTR_URL = "frame/optr/getFrameOptr.htm";
  ENROL_CONFIRM_URL = "std/stdEnrol/printEnrolConfirm.htm";
  IN_TIME_URL = "cf/cfOperateTime/getLatest.htm";

  courseFlag = true;
  inTimeFlag = true;
  coursePlan = [];
  allPlans = [];
  timeArr = []; //报考起止时间
  major = {};
  org_id: any;
  examArea1 = [];//考区
  examArea2 = [];//考区
  examArea3 = [];//考区
  examArea4 = [];//考区
  examArea5 = [];//考区
  examArea6 = [];//考区
  items = {
    course_id_1: '',
    course_id_2: '',
    course_id_3: '',
    course_id_4: '',
    course_id_5: '',
    course_id_6: '',
    org_id_1: '',
    org_id_2: '',
    org_id_3: '',
    org_id_4: '',
    org_id_5: '',
    org_id_6: ''
  };
  course1 = {
    time: '',
    courses: ''
  };
  course2 = {
    time: '',
    courses: ''
  };
  course3 = {
    time: '',
    courses: ''
  };
  course4 = {
    time: '',
    courses: ''
  };
  course5 = {
    time: '',
    courses: ''
  };
  course6 = {
    time: '',
    courses: ''
  };
  @ViewChild('ecourseCode')
  ecourseCode: ElementRef;
  @ViewChild('eticketNo')
  eticketNo: ElementRef;

  constructor() {
    super();
    FrameRequest.requestArray(this.IN_TIME_URL, {oper_type : 2}, (array) => {
      if(array && array.length > 0) {
        this.inTimeFlag = true;
        this.timeArr = array[0];
      } else {
        this.inTimeFlag = false;
      }
    });
  }

  ngOnInit() {
    FrameRequest.requestArray(this.COURSE_PLAN_URL, {}, (arrary) => {
      this.allPlans = arrary;
      
      if (this.allPlans.length === 4) {
        this.course1 = this.allPlans[0];
        this.course2 = this.allPlans[1];
        this.course3 = this.allPlans[2];
        this.course4 = this.allPlans[3];
      } else if (this.allPlans.length === 5) {
        this.course1 = this.allPlans[0];
        this.course2 = this.allPlans[1];
        this.course3 = this.allPlans[2];
        this.course4 = this.allPlans[3];
        this.course5 = this.allPlans[4];
      }else if (this.allPlans.length === 6) {
        this.course1 = this.allPlans[0];
        this.course2 = this.allPlans[1];
        this.course3 = this.allPlans[2];
        this.course4 = this.allPlans[3];
        this.course5 = this.allPlans[4];
        this.course6 = this.allPlans[5];
      }

    });
   

    FrameRequest.requestEntity("frame/optr/getUserExamAreaId.htm", {}, (entity) => {
      if (entity) {
        this.org_id = entity;
      }
    });

    
  }

  loadMajorPlan(major_id: number) {
    if (major_id) {
      FrameRequest.requestArray(this.COURSE_PLAN_URL, { major_id: major_id }, (arrary) => {
        this.coursePlan = arrary;
        this.courseFlag = false;
      });
      FrameRequest.requestEntity(this.MAJOR_URL, { id: major_id }, (entity) => {
        this.major = entity;
      });
    } else {
      this.courseFlag = true;
    }
  }

  findStd(ticket_no: any) {
    if (!ticket_no) {
      AppComponent.alert({ "title": "提示", "text": "请输入准考证号！" });
    } else {
      FrameRequest.requestEntity(this.FIND_STD_URL, { ticket_no: ticket_no }, (entity) => {
        if (entity) {
          this.query.std_name = entity.std_name;
          this.query.cert_no = entity.cert_no;
          this.query.ticket_no = ticket_no;
          this.ecourseCode.nativeElement.focus();
        } else {
          AppComponent.alert({ "title": "提示", "text": "准考证输入有误！" });
        }
      });
    }
  }

  findCourse(course_code: any) {
    if (!course_code) {
      AppComponent.alert({ "title": "提示", "text": "请输入课程代码！" });
    } else {
      FrameRequest.requestEntity(this.FIND_COURSE_URL, { course_code: course_code }, (entity) => {
        if (entity) {
          this.query.course_name = entity.course_name;
          this.autoChooseCourse(course_code, entity);
          this.query.course_code = "";
          this.query.course_name = "";
        } else {
          AppComponent.alert({ "title": "提示", "text": "课程代码输入有误！" });
        }
      });
    }
  }

  //根据录入课程自动匹配
  autoChooseCourse(course_code: any, entity: any) {
    //重复判断
    if(entity.id === this.items.course_id_1 || entity.id === this.items.course_id_2 
      || entity.id === this.items.course_id_3 || entity.id === this.items.course_id_4) {
        AppComponent.alert({ "title": "提示", "text": "该课程已经录入，是否需要修改？" });
        return ;
      }
    for (let i = 0; i < this.course1.courses.length; i++) {
      if (course_code === this.course1.courses[i]['course_code']) {
        this.items.course_id_1 = entity.id;
        this.examChoose(entity.id, 1);
        return;
      }
    }
    for (let i = 0; i < this.course2.courses.length; i++) {
      if (course_code === this.course2.courses[i]['course_code']) {
        this.items.course_id_2 = entity.id;
        this.items.org_id_2 = this.org_id;
        this.examChoose(entity.id, 2);
        return;
      }
    }
    for (let i = 0; i < this.course3.courses.length; i++) {
      if (course_code === this.course3.courses[i]['course_code']) {
        this.items.course_id_3 = entity.id;
        this.items.org_id_3 = this.org_id;
        this.examChoose(entity.id, 3);
        return;
      }
    }
    for (let i = 0; i < this.course4.courses.length; i++) {
      if (course_code === this.course4.courses[i]['course_code']) {
        this.items.course_id_4 = entity.id;
        this.items.org_id_4 = this.org_id;
        this.examChoose(entity.id, 4);
        return;
      }
    }
    for (let i = 0; i < this.course5.courses.length; i++) {
      if (course_code === this.course5.courses[i]['course_code']) {
        this.items.course_id_5 = entity.id;
        this.items.org_id_5 = this.org_id;
        this.examChoose(entity.id, 5);
        return;
      }
    }
    for (let i = 0; i < this.course6.courses.length; i++) {
      if (course_code === this.course6.courses[i]['course_code']) {
        this.items.course_id_6 = entity.id;
        this.items.org_id_6 = this.org_id;
        this.examChoose(entity.id, 6);
        return;
      }
    }
  }

  saveEnrol() {
    let courseIds = this.getIds("course_id");
    let orgIds = this.getIds("org_id");
    if (!this.query.ticket_no) {
      AppComponent.alert({ "title": "提示", "text": "请输入考生的准考证号" });
      return;
    }
    // let ids = this.framePager.getCheckedArray((item) => item.id);
    // if (!ids || ids.length === 0) {
    //   AppComponent.alert({ "title": "提示", "text": "请选择考生" });
    //   return;
    // }
    if (courseIds.length === 0) {
      AppComponent.alert({ "title": "提示", "text": "请选择报考课程" });
      return;
    }
    if (orgIds.length === 0) {
      AppComponent.alert({ "title": "提示", "text": "请选择报考考区" });
      return;
    }
    if (courseIds.length !== orgIds.length) {
      AppComponent.alert({ "title": "提示", "text": "必须同时选择课程和对应的考区" });
      return;
    }
    FrameRequest.request(this.ADD_ENROL_URL, {
      ticket_no: this.query.ticket_no, courseIds: courseIds,
      examIds: orgIds, RequestLoading: true
    }, () => {
      AppComponent.GobalComponent.successMsg("报考成功");
      this.initData();
      this.framePager.reload();
      this.eticketNo.nativeElement.focus();
    });
  }

  initData() {
    this.query = {
      major_id: '',
      std_name: '',
      cert_no: '',
      ticket_no: '',
      course_id: '',
      course_code: '',
      course_name: ''
    }
    this.items = {
      course_id_1: '',
      course_id_2: '',
      course_id_3: '',
      course_id_4: '',
      course_id_5: '',
      course_id_6: '',
      org_id_1: '',
      org_id_2: '',
      org_id_3: '',
      org_id_4: '',
      org_id_5: '',
      org_id_6: ''
    };
    // this.course1 = {
    //   time: '',
    //   courses: ''
    // };
    // this.course2 = {
    //   time: '',
    //   courses: ''
    // };
    // this.course3 = {
    //   time: '',
    //   courses: ''
    // };
    // this.course4 = {
    //   time: '',
    //   courses: ''
    // };
  }


  getIds(element: any) {
    let array = [];
    for (const key in this.items) {
      if (this.items.hasOwnProperty(key) && key.indexOf(element) != -1 && this.items[key]) {
        array.push(this.items[key]);
      }
    }
    return array;
  }

  //解决下拉框控件，在删除元素，设置为值为null的问题
  // search(param?: any) {
  //   let searchCondition = this.query;
  //   if (searchCondition) {
  //     if (!searchCondition['major_id']) {
  //       searchCondition['major_id'] = '';
  //     }
  //   }
  //   _.assignIn(param, searchCondition);
  // }

  //根据课程选择考区
  examChoose(course_id: any, flag: any) {
    if (course_id !== null) {
      FrameRequest.requestArray(this.EXAM_AREA_URL, { course_id: course_id }, (array) => {
        if (flag === 1) {
          this.examArea1 = array;
          for (let i = 0; i < this.examArea1.length; i++) {
            if (this.examArea1[i].org_id === this.org_id) {
              this.items.org_id_1 = this.org_id;
            }
          }
          if (!this.items.org_id_1) {
            AppComponent.alert({ "title": "提示", "text": "课程没在该考区下开考" });
            return;
          }
        } else if (flag === 2) {
          this.examArea2 = array;
          for (let i = 0; i < this.examArea2.length; i++) {
            if (this.examArea2[i].org_id === this.org_id) {
              this.items.org_id_2 = this.org_id;
            }
          }
          if (!this.items.org_id_2) {
            AppComponent.alert({ "title": "提示", "text": "课程没在该考区下开考" });
            return;
          }
        } else if (flag === 3) {
          this.examArea3 = array;
          for (let i = 0; i < this.examArea3.length; i++) {
            if (this.examArea3[i].org_id === this.org_id) {
              this.items.org_id_3 = this.org_id;
            }
          }
          if (!this.items.org_id_3) {
            AppComponent.alert({ "title": "提示", "text": "课程没在该考区下开考" });
            return;
          }
        } else if (flag === 4) {
          this.examArea4 = array;
          for (let i = 0; i < this.examArea4.length; i++) {
            if (this.examArea4[i].org_id === this.org_id) {
              this.items.org_id_4 = this.org_id;
            }
          }
          if (!this.items.org_id_4) {
            AppComponent.alert({ "title": "提示", "text": "课程没在该考区下开考" });
            return;
          }
        } else if (flag === 5) {
          this.examArea5 = array;
          for (let i = 0; i < this.examArea5.length; i++) {
            if (this.examArea5[i].org_id === this.org_id) {
              this.items.org_id_5 = this.org_id;
            }
          }
          if (!this.items.org_id_5) {
            AppComponent.alert({ "title": "提示", "text": "课程没在该考区下开考" });
            return;
          }
        } else if (flag === 6) {
          this.examArea6 = array;
          for (let i = 0; i < this.examArea6.length; i++) {
            if (this.examArea6[i].org_id === this.org_id) {
              this.items.org_id_6 = this.org_id;
            }
          }
          if (!this.items.org_id_6) {
            AppComponent.alert({ "title": "提示", "text": "课程没在该考区下开考" });
            return;
          }
        }
      });
    } else {
      if(flag === 1) {
        this.examArea1 = [];
      } else if(flag === 2) {
        this.examArea2 = [];
      } else if(flag === 3) {
        this.examArea3 = [];
      } else if(flag === 4) {
        this.examArea4 = [];
      }else if(flag === 5) {
        this.examArea5 = [];
      }else if(flag === 6) {
        this.examArea6 = [];
      }
    }

  }

  //打印报考单
  printEnrol() {
    if (!this.query.ticket_no) {
      AppComponent.alert({ "title": "提示", "text": "请输入考生的准考证号码" });
      return;
    }
    FrameRequest.requestEntity(this.FIND_STD_URL, { ticket_no: this.query.ticket_no }, (entity) => {
      if(!entity) {
        AppComponent.alert({ "title": "提示", "text": "考生的准考证号码输入有误" });
        return ;
      }
    });
    FrameRequest.download(this.ENROL_CONFIRM_URL, {
      ticket_no: this.query.ticket_no
    }, () => {
      AppComponent.GobalComponent.successMsg("报考校对单生成成功");
    });
  }


}
