import {Component, OnInit, ViewChild} from '@angular/core';
import {FrameEditComponent} from '../../../../frame/core/FrameEditComponent';
import {AjaxService} from '../../../../../core/ajax/ajax.service';
import {UploaderComponent} from '../../../../../core/uploader/uploader.component';
import {MsgService} from '../../../../../core/msg/msg.service';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {BsModalRef} from 'ngx-bootstrap';
import {BsModalService} from 'ngx-bootstrap';
import {DomSanitizer} from '@angular/platform-browser';
import {FramePageComponent} from '../../../../frame/core/FramePageComponent';

@Component({
  selector: 'app-agent-arrange',
  templateUrl: './agent-arrange.component.html'
})
export class AgentArrangeComponent  extends FramePageComponent implements OnInit {

  initMap: any;
  valForm: FormGroup;

  @ViewChild('uploader', {static: false}) uploader: UploaderComponent;
  @ViewChild('uploader2', {static: false}) uploader2: UploaderComponent;

  constructor(modalService: BsModalService, fb: FormBuilder, sanitizer: DomSanitizer) {
    super(modalService, sanitizer);
    this.valForm = fb.group({
      'ticket_note': [null, Validators.compose([Validators.required])],
      'ticket_title': [null, Validators.compose([Validators.required])]
    });
    this.initMap = {};
    this.initData();
  }

  ngOnInit() {

  }


  initData() {
    AjaxService.requestMap('school/school/arrange/init.htm', {}, (map) => {
      this.initMap = map;
    });
  }

  goUpload() {
    debugger;
    if (this.initMap.StdEnrolSubjectCount > 0 ) {

      // MsgService.confirm('已经有编排记录，重新上传将覆盖原有编排信息（包括已经打印的准考证信息！），确定要覆盖操作吗？', () => {
      //   this.uploader.choooseFileAndUpload({}, this.uploadCallback.bind(this));
      // });
      this.uploader.uploadFiles();
    } else {
      //this.uploader.choooseFileAndUpload({}, this.uploadCallback.bind(this));
      this.uploader.uploadFiles();
    }
  }

  goUploadScore() {
    debugger;
      if (this.initMap.AspectScoreCount > 0 ) {
        // MsgHelper.confirm('已经有成绩记录，重新上传将覆盖原有成绩！），确定要覆盖操作吗？', () => {
        //   this.uploader2.choooseFileAndUpload({}, this.uploadCallback.bind(this));
        // });
        this.uploader2.uploadFiles();
      } else {
        //this.uploader2.choooseFileAndUpload({}, this.uploadCallback.bind(this));
        this.uploader2.uploadFiles();
      }
  }


  uploadCallback() {
    this.initData();
    MsgService.successToaster('数据上传成功！');
  }

  goView() {
    this.valForm.markAllAsTouched();
    if (!this.validForm()) {
      return;
    }
    AjaxService.ajaxDownload('school/school/arrange/ticket/view.htm', this.initMap.SchoolReg)
  }

  goMakeAllTicket () {
    this.valForm.markAllAsTouched();
    if (!this.validForm()) {
      return;
    }
    this.initMap.SchoolReg.RequestWithLoading = true;
    if (this.initMap.SchoolReg.ticket_time ) {
      MsgService.confirm('所有准考证已经生成，确定要重新生成准考证吗？', () => {
        AjaxService.requestEntity('school/school/arrange/ticket/all.htm', this.initMap.SchoolReg, () => {
          this.initData();
          MsgService.successToaster('所有考生准考证已生成');
        });
      });
    } else {
      AjaxService.requestEntity('school/school/arrange/ticket/all.htm', this.initMap.SchoolReg, () => {
        this.initData();
        MsgService.successToaster('所有考生准考证已生成');
      });
    }
  }

  // 判断表单是否通过了验证
  validForm(): boolean {
    this.valForm.markAllAsTouched();
    return this.valForm.valid;
  }
}
