import { Component, OnInit, ViewChild } from "@angular/core";
import { FormBuilder, Validators } from "@angular/forms";
import { CustomValidators } from "ng2-validation";
import { BsModalService } from "ngx-bootstrap/modal";
import { FlexTableComponent } from "../../../../shared/common/FlexTable.component";
import { FrameRequest } from "../../../../shared/frame/frame-request";
import * as _ from 'lodash';
import { FrameValidator } from "../../../../shared/frame/Frame-validator";
import { AppComponent } from "../../../..";
import { FlexSelectComponent } from "../../../../shared/components/flexSelect/FlexSelect.component";

@Component({
  selector: "app-std-break-discipline",
  templateUrl: "./std-break-discipline.component.html",
  styleUrls: ["./std-break-discipline.component.scss"]
})
export class StdBreakDisciplineComponent extends FlexTableComponent
  implements OnInit {
  public query = {
    std_name: "",
    cert_no: "",
    ticket_no: "",
    agent_id: "",
    exam_time_id:""
  };

  //TODO
  QUERY_URL = "std/stdBreakDiscipline/page.htm";
  DELETE_URL = "std/stdBreakDiscipline/deleteBatch.htm";
  GET_URL = "std/stdBreakDiscipline/get.htm";
  ADD_URL = "std/stdBreakDiscipline/add.htm";
  EDIT_URL = "std/stdBreakDiscipline/edit.htm";

  agentArray=[]
  examTimeIdArray=[]
  courseList=[]
  std_reg_id = ""
  course_id = ""

  ruleArray=[]
  itemArray=[]

  @ViewChild('course_code')
  course_code: FlexSelectComponent
  
  constructor(public bsModalService: BsModalService, fb: FormBuilder) {
    super();
    // CustomValidators.rangeLength([1, 6])
    this.formModel = fb.group({
      id: [null],
      exam_time_id: [null, Validators.compose([Validators.required])],
      std_name: [null, Validators.compose([Validators.required])],
      cert_no: [null, Validators.compose([Validators.required,Validators.pattern(FrameValidator.pattern_en_number)])],
      ticket_no: [null, Validators.compose([Validators.required,Validators.pattern(FrameValidator.pattern_en_number)])],
      agent_id: [null, Validators.compose([Validators.required])],
      course_code: [null, Validators.compose([Validators.required,Validators.pattern(FrameValidator.pattern_number)])],
      seat_no: [null, Validators.compose([Validators.required,Validators.pattern(FrameValidator.pattern_number)])],
      exam_area: [null],
      break_time: [null],
      stop_starttime: [null],
      stop_endtime: [null],
      audit_status: [null, Validators.compose([Validators.required])],
      agent_remark: [null],
      prov_agent_remark: [null],
      city_agent_remark: [null],
      item_id:[null, Validators.compose([Validators.required])],
      rule_id:[null, Validators.compose([Validators.required])]
    });

    

    FrameRequest.requestArray('pl/plExamTime/listtime.htm', {}, (array) => {
      this.examTimeIdArray = array;
    });

    FrameRequest.requestArray('std/stdBreakDisciplineRule/listAll.htm', {}, (array) => {
      this.ruleArray = array;
    });

    FrameRequest.requestArray('std/stdBreakDisciplineItem/listAll.htm', {}, (array) => {
      this.itemArray = array;
    });
  }

  changePoint(){
    if(this.query.exam_time_id){
      FrameRequest.requestArray('kw/kwExamPoint/listExamPointByExamTimeId.htm', {exam_time_id:this.query.exam_time_id}, (array) => {
        this.agentArray = array;
      });
    }
  }

  stdRegInfoModalConfirm(modal){
    let checkedArray = modal.framePager.getCheckedArray();
    if(checkedArray.length > 0){
      let checkedItem = checkedArray[0];
      this.item.std_name = checkedItem.std_name;
      this.item.cert_no = checkedItem.cert_no;
      this.item.ticket_no = checkedItem.ticket_no;
      this.std_reg_id = checkedItem.id+"";
      if(this.item.exam_time_id +""){
        this.examTimeAndStd_coursesChange();
      }
    }
    modal.hideModal();
  }

  //联动已经编排的课程
  examTimeAndStd_coursesChange(){
    if((this.item.exam_time_id +"") && (this.std_reg_id+"")){
      FrameRequest.requestArray('std/stdBreakDiscipline/stdCourseList.htm', {exam_time_id:this.item.exam_time_id,std_id:this.std_reg_id}, (array) => {
        if(array.length == 0){
          AppComponent.alert({title:"信息提示",text:"该考生无已考课程!,请重新选择"},()=>{});
          return;
        }
        this.courseList = array;
      });
    }
  }

  //联动编排信息
  examTimeAndStdAndcourses_layoutChange(){
    if((this.item.exam_time_id +"") && (this.std_reg_id+"") && (this.item.course_code+"")){
      this.courseList.forEach((item) => {
        if(item.course_code == this.item.course_code){
          let course_id = item.id;
          FrameRequest.requestArray('std/stdBreakDiscipline/findStdLayOut.htm', {exam_time_id:this.item.exam_time_id,std_id:this.std_reg_id,course_id:course_id}, (array) => {
            //加载联动信息
            if(array.length > 0){
              let entity = array[0];
              this.item.seat_no = entity.seat_no;
              this.item.agent_id = entity.exam_point_id + "";
              this.item.exam_area = entity.unit_name + "-" + entity.room_code;
              this.item.break_time = entity.exam_times;
              this.item.unit_id=entity.unit_id;
            }
           
          });
        }
     });
    }
  }

  async aftGoEditEvent(){
    if(this.item.exam_time_id && this.item.ticket_no){
    FrameRequest.requestArray('std/stdBreakDiscipline/queryStdCourseListByTicketNo.htm', {exam_time_id:this.item.exam_time_id,ticket_no:this.item.ticket_no}, (array) => {
      if(array.length == 0){
       
        return;
      }
      this.courseList = array;
    });
   }
  }

  async preSaveEvent(){debugger
    // if(!this.item.start_time){
    //     AppComponent.alert({ title: "信息提示", text: "请选择开始时间!" });
    //     return false;
    // }
    // if(!this.item.end_time){
    //     AppComponent.alert({ title: "信息提示", text: "请选择结束时间!" });
    //     return false;
    // }

    let startTime = new Date(Date.parse(this.item.stop_starttime));
    let endTime = new Date(Date.parse(this.item.stop_endtime));
    if(startTime >= endTime){
        AppComponent.alert({ title: "信息提示", text: "禁止开始时间不能大于等于禁止结束时间!" });
        return false;
    }
    return true;
}

  getAgentName(agent_id){
    if(this.agentArray.length > 0){
      agent_id = parseInt(agent_id);
      let agent = _.find(this.agentArray,{id:agent_id});
      return agent?agent.exam_name:"";
    }else{
      return "";
    }
  }



}
