import {Component, OnInit} from '@angular/core';
import {FramePageComponent} from "../../../frame/core/page/frame.page";
import {BsModalService} from "ngx-bootstrap/modal";
import {AjaxService} from '../../../../core/service/ajax.service';
import {MsgService} from '../../../../core/service/msg.service';
import {ModalService} from '../../../../core/service/modal.service';
import {FuncService} from 'src/app/core/service/func.service';
import {FrameService} from 'src/app/core/service/frame.service';

@Component({
  selector: 'app-std-ticket',
  templateUrl: './std-ticket.component.html',
  styleUrls: ['./std-ticket.component.scss']
})
export class StdTicketComponent extends FramePageComponent implements OnInit {
  schoolArray = [];
  batchArray = [];
  timesArray = [];
  query = {
    school_id: '',
    batch_id: '',
    exam_start_time: '',
  }

  constructor(private modalService: BsModalService) {
    super(modalService);
  }

  ngOnInit() {
    this.AjaxService.requestArray('./cf/school/list.htm', {}, (array) => {
      this.schoolArray = array;
      if (this.schoolArray.length == 1) {
        this.query.school_id = this.schoolArray[0].school_id;
        this.queryBatchArray();
      }
    });

  }

  queryBatchArray() {
    AjaxService.requestArray("cf/batch/list.htm", {school_id: this.query.school_id}, (array) => {
      this.batchArray = array;
      if (this.batchArray.length >= 1) {
        this.query.batch_id = this.batchArray[0].batch_id;
        this.queryTimeArray();
      }else if (this.batchArray.length == 0) {
        this.query.batch_id=null;
      }
      this.requestData();
    });
  }
  queryTimeArray() {
    this.AjaxService.requestArray('./std/time/list.htm', this.query, (array) => {
      this.timesArray = array;
    });
  }

  //重新生成所有准考证
  reMakeAll() {
    MsgService.confirmAlert("确定要全部重新生成吗?", () => {
      AjaxService.request('std/ticket/all/remake.htm', {school_id : this.query.school_id, batch_id : this.query.batch_id}, (ajax) => {
        ModalService.showThreadWindow(this.bsService, ajax.entity, {threadTitle: '请求已提交，正在进行重新生成'} );
      });

    });
  }

  //重新生成考生准考证
  makeStdTicket(ticket, flag) {
    let msg = '确定要生成该考生准考证吗！';
    if (flag) {
      msg = '确定要重新生成该考生的准考证码？';
    }
    MsgService.confirmAlert("确定重新生成吗?", () => {
      FuncService.ajaxDownload('std/ticket/make.htm', {ticket_id: ticket.ticket_id});
    });
  }

  downloadTicket(v) {
    window.open(FrameService.getParamValue('EnrolMaterialWebPath') + '/' + v.file_path);
  }

  deleteAllTicket() {

  }

  requestData() {
    this.pager.reload();
    this.queryTimeArray();
  }


  doUploaFile() {
    debugger;
    //线程参数 和 业务参数
    let reqParam = {
      thread_name: "导入生成准考证",
      error_flag: "InActive",
      callback_type: "Swal",
      school_id : this.query.school_id,
      batch_id : this.query.batch_id
    };
    //上传参数
    let uploadParam = {
      uploadTitle:'导入生成准考证',
      templateFile:'StdTicketImp.xls',
      uploadCallback: this.uploadWithThreadCallback.bind(this),
      autoClose:true
    };
    ModalService.showUploadWindow(this.bsService,'./std/ticket/upload.htm', reqParam, uploadParam);
  }

  uploadWithThreadCallback(ajax) {
    console.log("k--->" + ajax);
    MsgService.successToastr("导入成功!");
    this.pager.reload();
  }


  downloadSeat() {
    AjaxService.requestEntity('std/desk/download.htm', this.query, (entity) => {
      ModalService.showThreadWindow(this.bsService, entity, {threadTitle: '正在整理数据，请稍候'});
    });
  }

  downloadPapers() {
    AjaxService.requestEntity('std/paper/download.htm', this.query, (entity) => {
      ModalService.showThreadWindow(this.bsService, entity, {threadTitle: '正在整理数据，请稍候'});
    });
  }

  downloadDoor() {
    AjaxService.requestEntity('std/door/download.htm', this.query, (entity) => {
      ModalService.showThreadWindow(this.bsService, entity, {threadTitle: '正在整理数据，请稍候'});
    });
  }

}
