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

@Component({
  selector: 'app-pl-pj-task',
  templateUrl: './pl-pj-task.component.html',
  styleUrls: ['./pl-pj-task.component.scss']
})
export class PlPjTaskComponent extends FlexTableComponent implements OnInit {

  query = {
    query: "",
    year_code: "",
    pj_school_id: "",
    is_net_mark: "",
    is_allot: ""
  }

  QUERY_URL = "pl/plPjTask/page.htm";
  ADD_URL = "pl/plPjTask/add.htm";
  DELETE_URL = "pl/plPjTask/delete.htm";
  GET_URL = "pl/plPjTask/get.htm";
  EDIT_URL = "pl/plPjTask/edit.htm";
  EXAM_TIME_URL = "pl/plExamTime/list.htm";
  PJ_SCHOOL_URL = "pl/plPjSchool/list.htm";
  COURSE_PLAN_URL = "pl/mergePublishPlan/list.htm";
  CURR_YEAR_CODE_URL = "pl/plExamTime/getCurrentYearCode.htm";
  EXPORT_URL = "pl/plPjTask/export.htm";
  DOWNLOAD_URL = "pl/plPjTask/download.htm";
  ERROR_DOWNLOAD_URL = "pl/plPjTask/downloadError.htm";
  UPLOAD_URL = "pl/plPjTask/upload.htm";
  TASK_YEAR_CODE_URL = "pl/plPjTask/listYearcode.htm";
  YEAR_CODE_URL = "pl/plExamTime/list.htm";
  SYCN_TASK_URL = "pl/plPjTask/syncTask.htm";
  SYCN_COURSE_URL = "pl/plPjTask/syncCourseTask.htm";

  UNASSIGN_COURSE_URL = "pl/plPjTask/exportUnassign.htm";

  timeList = [];
  schoolList = [];
  courseList = [];
  currentYearCode = "";
  flag = false;
  filePath: any;//错误文件路径
  modalWin: any;
  yearCode: any;
  course_yearCode: any;
  historyYearcode: any;

  @ViewChild('uploader')
  uploader: FrameUploaderComponent;

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

  ngOnInit() {
    FrameRequest.requestArray(this.EXAM_TIME_URL, {}, (array) => {
      this.timeList = array;
    });
    FrameRequest.requestArray(this.PJ_SCHOOL_URL, {}, (array) => {
      this.schoolList = array;
    });
    FrameRequest.requestEntity(this.CURR_YEAR_CODE_URL, {}, (entity) => {
      this.currentYearCode = entity;
    });
  }

  search(param) {
    if (this.query.is_allot === "0" && !this.query.year_code) {
      AppComponent.alert({ title: '提示', text: '请选择批次!' });
      return false;
    }else{
      if (this.query.year_code === null) {
        this.query.year_code = "";
      }
      if (this.query.pj_school_id === null) {
        this.query.pj_school_id = "";
      }
      param.query = this.query.query;
      param.year_code = this.query.year_code;
      param.pj_school_id = this.query.pj_school_id;
      param.is_net_mark = this.query.is_net_mark;
      param.is_allot = this.query.is_allot;
    }
   
  }

  //根据批次获取考试计划
  getCourse(year_code) {
    if (!year_code) {
      year_code = this.currentYearCode;
    }
    FrameRequest.requestArray(this.COURSE_PLAN_URL, { year_code: year_code }, (array) => {
      this.courseList = array;
    });
  }

  async preGoEditEvent(entity?) {
    if (!entity) {
      this.flag = false;
      if (!this.item) {
        this.item = {};
      }
      this.item.year_code = this.currentYearCode;
    } else {
      this.flag = true;
    }
    return true;
  }

  //导出评卷任务
  export() {
    if (!this.query.year_code) {
      AppComponent.alert({ title: '提示', text: '请选择批次' });
      return;
    }
    if (!this.query.pj_school_id) {
      AppComponent.alert({ title: '提示', text: '请选择评卷学校' });
      return;
    }
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download(this.EXPORT_URL, { year_code: this.query.year_code, pj_school_id: this.query.pj_school_id, is_net_mark: this.query.is_net_mark }, () => {
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("评卷任务分配表生成成功");
    });
  }

  download() {
    FrameRequest.download(this.DOWNLOAD_URL, { fileName: 'pl_pj_task.xlsx' }, () => {
      AppComponent.GobalComponent.successMsg("模板下载成功");
    });
  }

  /**
 * 批量分配
 */
  upload(item?: any) {
    if (!this.query.year_code) {
      AppComponent.alert({ 'title': '提示', 'text': '请选择批次' });
      return;
    }
    this.uploader.choooseFileAndUpload({ year_code: this.query.year_code }, ((ajax) => {
      if (ajax.errorCode === -1) {
        this.filePath = ajax.url;
        AppComponent.alert({ 'title': '提示', 'text': '有错误数据，请修正后，在重新上传' });
      } else if (ajax.errorCode === -2) {
        AppComponent.alert({ 'title': '提示', 'text': ajax.errorMsg });
      } else {
        AppComponent.GobalComponent.successMsg('导入成功');
        this.filePath = '';
        this.hideModal();
        this.framePager.reload();
      }
    }).bind(this));
  }

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


  /**
   * 增加课程界面
   */
  syncTask(template: TemplateRef<any>, modalcss?) {
    let css = modalcss ? modalcss : "modal-lg";
    this.modalWin = this.bsModalService.show(template, { class: css });
  }
  /**
     * 模态窗口隐藏
     */
  hideModalWin() {
    if (this.modalWin) {
      this.modalWin.hide();
    }
  }
  //同步
  sync() {
    if (!this.yearCode) {
      AppComponent.alert({ 'title': '提示', 'text': '请选择本批次' });
      return;
    }
    if (!this.historyYearcode) {
      AppComponent.alert({ 'title': '提示', 'text': '请选择历史评卷批次' });
      return;
    }
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.request(this.SYCN_TASK_URL, { yearCode: this.yearCode, historyYearcode: this.historyYearcode }, (ajax) => {
      AppComponent.GobalComponent.setLoading(false);
      if (ajax.errorCode === -1) {
        AppComponent.alert({ 'title': '提示', 'text': ajax.errorMsg });
      } else {
        AppComponent.GobalComponent.successMsg("任务同步成功");
        this.hideModalWin();
        this.yearCode = "";
        this.historyYearcode = "";
      }
    });
  }


    //同步考试计划课程
    syncCourse() {
      if (!this.course_yearCode) {
        AppComponent.alert({ 'title': '提示', 'text': '请选择本批次' });
        return;
      }
     
      AppComponent.confirm("确定同步考试计划课程吗?", () => {
        AppComponent.GobalComponent.setLoading(true);
        FrameRequest.request(this.SYCN_COURSE_URL, { yearCode: this.course_yearCode }, (ajax) => {
          AppComponent.GobalComponent.setLoading(false);
          if (ajax.errorCode === -1) {
            AppComponent.alert({ 'title': '提示', 'text': ajax.errorMsg });
          } else {
            AppComponent.GobalComponent.successMsg("考试计划课程同步成功");
            this.hideModalWin();
            this.yearCode = "";
            this.historyYearcode = "";
          }
        });
      });
 
    }

  //导出未分配评卷任务
  exportNoAllotTask() {
    if(!this.query.year_code) {
      AppComponent.alert({ 'title': '提示', 'text': '请选择批次' });
      return ;
    }
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download(this.UNASSIGN_COURSE_URL, {year_code : this.query.year_code}, () => {
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("导出成功");
    });
  }
}
