import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { MsgService } from '../../../../../core/service/msg.service';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { FrameService } from '../../../../../core/service/frame.service';

declare let printUtil;

@Component({
  selector: 'app-check-in-bw',
  templateUrl: './check-in-bw.component.html',
  styleUrls: ['./check-in-bw.component.scss']
})
export class CheckInBwComponent extends FramePageComponent  implements OnInit {
  checkin: any;
  @ViewChild('certid', {static: false}) certid;
  stdInfo:any;
 

  //打印所用参数
  printOption: any;
  printDevice: any;
  autoPrint: any = true;

  constructor(modalService: BsModalService) {
    super(modalService);
    this.checkin = {};
    this.getPrint();
  }

  ngOnInit(): void {
  }

  //保存配对
  savePairing() {
    if (this.checkin.cert_id === undefined || $.trim(this.checkin.cert_id).length === 0)
      return;
    // 提交
    this.checkin.requesting = true;
    AjaxService.requestMap('exam/eport/pairin/save.htm', {cert_id: this.checkin.cert_id}, (map) => {
      if(map) {
        this.stdInfo = map;
      }
      if(this.autoPrint==true){
        this.goPrint();
      }
      this.afertCertIdRequest();
    }, {
      ErrorCallback: this.afertCertIdRequest.bind(this)
    });
  }

  afertCertIdRequest() {
    this.checkin.requesting = false;
    this.certid.nativeElement.focus();
    this.certid.nativeElement.select();

  }

   //开始打印
   goPrint() {
    if (!this.stdInfo.stdName) {
      return;
    }
    var html = "";
    html += "<div style='top:25px;font-size:22px;font-weight:bold;text-align:center;'>北京外国语大学</div>";
    html += "<div style='top:5px;font-size:16px;font-weight:bold;text-align:center;'>2023年综合评价招生</div>";
    html += "<div style='top:5px;font-size:20px;text-align:center;'>考试确认单</div>";
    html += "<div style='top:15px;font-size:16px;'>姓名:" + this.stdInfo.stdName + '</div>';
    html += "<div style='top:5px;font-size:16px;'>" + this.stdInfo.note1 + '</div>';
    html += "<div style='top:5px;font-size:16px;'>" + this.stdInfo.note2 + '</div>';
    html += "<div style='top:5px;font-size:16px;'>第一场:" + this.stdInfo.mainname + '</div>';
    html += "<div style='top:5px;font-size:16px;'>第二场:" + this.stdInfo.slavename + '</div>';
    if (printUtil) {
      printUtil.startPrint(html, this.printDevice)
    }
  }

//打印参数-初始化
  getPrint() {
    if (printUtil) {
      printUtil.getPrint((data) => {
        this.printOption = data;
        this.printDevice = data[0].name;
      })
    }
  }

}
