import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { TaskDistCancelComponent } from '../task-dist-cancel/task-dist-cancel.component';
import { TaskDistDetailComponent } from '../task-dist-detail/task-dist-detail.component';

@Component({
  selector: 'app-task-dist',
  templateUrl: './task-dist.component.html',
  styleUrls: ['./task-dist.component.scss']
})
export class TaskDistComponent extends FramePageComponent implements OnInit {

  schoolArr = [];
  batchArr = [];
  task:any;

  constructor(private modalService: BsModalService, private cdr : ChangeDetectorRef) { 
    super(modalService);
  }

  ngOnInit(): void {
    this.task = {};
    this.AjaxService.requestArray('cf/school/list.htm', {}, (array)=> {
      this.schoolArr = array;
      if(this.schoolArr && this.schoolArr.length == 1) {
        this.task.school_id = this.schoolArr[0].school_id;
        this.requestBatch();
      }
    });

  }

  //查找批次
  requestBatch() {
    this.AjaxService.requestArray('cf/school/batch/list.htm', {
      school_id : this.task.school_id
    }, (array) => {
      this.batchArr = array;
      this.cdr.detectChanges();
      if(this.batchArr && this.batchArr.length == 1) {
        this.task.batch_id = this.batchArr[0].batch_id;
        this.ajax.reload({batch_id: this.task.batch_id});
      }
    });
  }

  //回收当日未评分考生
  recycleStd() {
    this.MsgService.confirmAlert('确定回收今日已经分发，但是还未评分的考生吗？', () => {
      this.AjaxService.request('ex/task/dist/recycle.htm', {batch_id: this.task.batch_id}, () => {
        this.MsgService.successToastr('考生回收成功！');
        this.ajax.reload({batch_id: this.task.batch_id});
      }, {RequestWithLoading: true});
    });
  }

  //取消任务分发
  cancelTaskModal() {
    this.onModalClose = () => {
      this.ajax.reload({batch_id: this.task.batch_id});
    };
    this.showComponentModal(TaskDistCancelComponent, {ResponseEntity: this.task}, {class: 'modal-5'});
  }

  //打开任务分发窗口
  showTaskModal() {
    this.onModalClose = () => {
      this.ajax.reload({batch_id: this.task.batch_id});
    };
    this.showComponentModal(TaskDistDetailComponent, {ResponseEntity: this.task}, {class: 'modal-9p'});
  }

  //导出未分发考生
  exportStdReport() {
    if(!this.task.batch_id) {
      this.MsgService.errorAlert('请选择批次！');
      return;
    }
    this.FuncService.ajaxDownload('ex/task/dist/export/std.htm', {batch_id: this.task.batch_id});
  }

  //导出所有分发任务
  exportTaskReport() {
    if(!this.task.batch_id) {
      this.MsgService.errorAlert('请选择批次！');
      return;
    }
    let entity = {
      report_name: 'DistTaskReport',
      batch_id: this.task.batch_id
    }
    this.FuncService.ajaxDownload('frame/report/download.htm', entity);
  }

}
