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

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

  EXAM_TIME_URL = "pl/plExamTime/listtime.htm";
  QUERY_URL = "std/stdEnrol/pageSpecial.htm";
  DELETE_URL = "std/stdEnrol/deleteSpecial.htm";
  UPLOAD_URL = "std/stdEnrol/specialUpload.htm";
  DOWNLOAD_URL = "std/stdEnrol/download.htm";
  ERROR_DOWNLOAD_URL = "std/stdEnrol/downloadError.htm";

  examtimeList = [];
  exam_time_id: number;
  filePath: string;

  constructor(public bsModalService: BsModalService) {
    super();
  }

  ngOnInit() {
    FrameRequest.requestArray(this.EXAM_TIME_URL, {}, (arrary) => {
      this.examtimeList = arrary;
    });
  }

  //模板下载
  download() {
    FrameRequest.download(this.DOWNLOAD_URL,  { fileName: 'special_course_enrol.xlsx' });
  }

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

  //文件导入
  upload() {
    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 });
  }

}
