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

@Component({
  selector: 'app-kw-special-examplan',
  templateUrl: './kw-special-examplan.component.html',
  styleUrls: ['./kw-special-examplan.component.scss']
})
export class KwSpecialExamplanComponent extends FlexTableComponent implements OnInit {

  //specialFlag = 2;//特殊课程标志
  query = {
    query: '',
    exam_time_id: ''
  }
  QUERY_URL = "kw/kwSpecialExamPlan/page.htm";
  DELETE_URL = "kw/kwSpecialExamPlan/delete.htm";
  GET_URL = "kw/kwSpecialExamPlan/get.htm";
  ADD_URL = "kw/kwSpecialExamPlan/add.htm";
  EDIT_URL = "kw/kwSpecialExamPlan/edit.htm";
  EXAM_TIME_URL = "pl/plExamTime/listtime.htm";
  SPECIAL_LIST_URL = "pl/plCourse/listSpecial.htm";
  DOWNLOAD_URL = "kw/kwSpecialExamPlan/download.htm";
  UPLOAD_URL = "kw/kwSpecialExamPlan/upload.htm";
  ERROR_URL = "kw/kwSpecialExamPlan/errDownload.htm";
  ERROR_DOWNLOAD_URL = "std/stdEnrol/downloadError.htm";
  examtimeList = [];
  courseList = [];
  filePath: any;
  exam_time_id: number;

  @ViewChild('uploader')
  uploader: FrameUploaderComponent;

  constructor(public bsModalService: BsModalService) {
    super();
    const fb = new FormBuilder();
    this.formModel = fb.group({
      id: [null],
      course_id: ['', Validators.required],
      course_name: ['', Validators.required],
      exam_time_id: ['', Validators.required],
      plan_date: ['', Validators.required]
    });
  }

  ngOnInit() {
    FrameRequest.requestArray(this.EXAM_TIME_URL, {}, (arrary) => {
      this.examtimeList = arrary;
    });
    FrameRequest.requestArray(this.SPECIAL_LIST_URL, { ids: '' },
      (arrary) => {
        this.courseList = arrary;
      });
  }

  getCourseName(course_id: number) {
    let course = this.courseList.find(item => {
      return item.id === Number(course_id);
    });
    if (course) {
      this.item.course_name = course.course_name;
    } else {
      this.item.course_name = '';
    }
  }

  //模板下载
  download() {
    FrameRequest.download(this.DOWNLOAD_URL);
  }

  //打开导入界面
  goUpload(template: TemplateRef<any>, modalcss?: string) {
    this.filePath = '';
    let css = modalcss ? modalcss : "modal-lg";
    this.bsModalWin = this.bsModalService.show(template, { class: css });
  }


  //上传文件-单个个文件上传
  uploadFile(item?: any) {
    if (!this.exam_time_id) {
      AppComponent.alert({ 'title': '提示', 'text': '请选择考试日期' });
      return;
    }
    this.uploader.choooseFileAndUpload({ exam_time_id: this.exam_time_id }, ((ajax) => {
      if (ajax.errorCode === -1) {
        this.filePath = ajax.url;
        AppComponent.alert({ 'title': '提示', 'text': '有错误数据，请修正后，在重新上传' });
      } else {
        AppComponent.GobalComponent.successMsg('数据导入成功');
        this.hideModal();
        this.framePager.reload();
      }

    }).bind(this));
  }

  //错误数据下载
  downloadErrFile() {
    FrameRequest.download(this.ERROR_DOWNLOAD_URL, { filePath: this.filePath });
  }
}
