import { Component, OnInit,ViewChild } 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';
import { FrameUploaderComponent } from '../../../../../core/uploader/uploader.component';


import * as _ from 'lodash';

@Component({
  selector: 'app-kw-special-courseroom',
  templateUrl: './kw-special-courseroom.component.html',
  styleUrls: ['./kw-special-courseroom.component.scss']
})
export class KwSpecialCourseroomComponent extends FlexTableComponent implements OnInit {
  @ViewChild('uploader')
  uploader: FrameUploaderComponent;

  public query = {
    query: "", 
    exam_time_id : ""
  };


  public impQuery = {
    exam_time_id: ""
  }

  QUERY_URL = "kw/kwSpecialCourseRoom/page.htm";
  ADD_URL = "kw/kwSpecialCourseRoom/add.htm";
  DELETE_URL= "kw/kwSpecialCourseRoom/delete.htm";
  GET_URL= "kw/kwSpecialCourseRoom/get.htm";
  EDIT_URL= "kw/kwSpecialCourseRoom/edit.htm";
  UPLOAD_URL = "kw/kwSpecialCourseRoom/upload.htm";
  SPECIAL_UNIT_URL = "kw/kwExamUnit/listUnit.htm?alone_flag=1";
  COURSE_ROOM_URL = "kw/kwSpecialCourseRoom/findRoom.htm";
  SPECIAL_COURSE_URL = "kw/kwSpecialCourseRoom/listSpecialCourse.htm";

  TIME_URL = "pl/plExamTime/listtime.htm";
 

  YEAR_CODE_URL = "pl/plExamTime/list.htm";
  LATEST_TIME_URL = "cf/cfOperateTime/getLatest.htm";

  courseList: any;
  specialUnit: any;

  filePath : String;

  constructor(public bsModalService: BsModalService, fb: FormBuilder) {
    super();
    this.formModel = fb.group({
      id: [null],
      unit_id: [null, Validators.required],
      course_id: [null, Validators.required]
    });
  }

  ngOnInit() {
    FrameRequest.requestArray(this.LATEST_TIME_URL, { oper_type: 16 }, (array) => {
      if (array && array.length > 0) {
        this.query.exam_time_id = array[0].exam_time_id;
        this.framePager.reload();
      }
    });
    FrameRequest.requestArray(this.SPECIAL_COURSE_URL, {}, (array) => {
      this.courseList = array;
    });

    FrameRequest.requestArray(this.SPECIAL_UNIT_URL, {}, (array) => {
      this.specialUnit = array;
    });
  }

    

  saveRoom() {
    if (!FrameFunction.validForm(this.formModel)) {
      return;
    }
    let url = this.item.id ? this.EDIT_URL: this.ADD_URL;
    if(_.isUndefined(this.item.id)) delete this.item.id;
    //循环判断元素是否undefined
    for (const key in this.item) {
      if(this.item[key] === undefined) {
        this.item[key] = "";
      }
    }
    FrameRequest.requestEntity(this.COURSE_ROOM_URL, {
      unit_id: this.item.unit_id
    }, (entity) => {
      if(entity) {
        AppComponent.confirm("同场次该考场已经指定了其他课程，确定要与其他课程放在同一考场考试吗？",()=>{
          FrameRequest.requestEntity(url, this.item, async () => {
            AppComponent.GobalComponent.successMsg('信息保存成功');
            this.aftSaveEvent();
            this.framePager.reload();
            if(this.bsModalWin)this.bsModalWin.hide();
            this.resetPage();
          });
        });
      } else {
        FrameRequest.requestEntity(url, this.item, async () => {
          AppComponent.GobalComponent.successMsg('信息保存成功');
          this.aftSaveEvent();
          this.framePager.reload();
          if(this.bsModalWin)this.bsModalWin.hide();
          this.resetPage();
        });
      }
    });
    
  }

  search(param) {
    if (!this.query.exam_time_id) {
      this.query.exam_time_id = "";
    }
    param.query = this.query.query;
    param.exam_time_id = this.query.exam_time_id;
  }

  exportSpecialCourse() {
    if (!this.query.exam_time_id) {
        AppComponent.alert({ title: "信息提示", text: "请选择批次!" });
        return;
    }
    let param = {
        exam_time_id: this.query.exam_time_id
    };
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("kw/kwSpecialCourseRoom/exportSpecialCourse.htm",param,()=>{
        AppComponent.GobalComponent.setLoading(false);
        AppComponent.GobalComponent.successMsg("独立编排课程导出成功!");
    });
}

//导入
importFunc() {
  if (!this.impQuery.exam_time_id) {
    AppComponent.alert({title: "信息提示", text: "请选择批次!"});
    return;
  }
  
  let param = {
    exam_time_id: this.impQuery.exam_time_id
  };
  this.uploader.choooseFileAndUpload(param, ((ret) => {
    if (ret.errorCode > 0) {
      AppComponent.alert({title:"信息提示", text: ret.errorMsg});
    }
    else if (ret.errorCode == -1) {
      this.filePath = ret.url;
      this.hideModal();
      this.editShowType = "showMainList";
      AppComponent.alert({ 'title': '提示', 'text': '有错误数据，请修正后，再上传' });
    } else if (ret.errorCode == 0) {
      this.hideModal();
      this.framePager.reload();
      this.editShowType = "showMainList";
      AppComponent.alert({title:"信息提示", text:"导入成功!"});
    }
  }));
}

 //错误数据下载
 downloadErrFile() {
  FrameRequest.download("kw/kwSpecialCourseRoom/error/download.htm",  { filePath: this.filePath });
}

}
