import {Component, OnInit, ViewChild} 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 {StdDetailComponent} from '../std-detail/std-detail.component';
import {FrameUpload} from '../../../../core/frame/upload/frame-upload/frame-upload';
import {ModalService} from '../../../../core/service/modal.service';
import {FramePager} from '../../../../core/frame/pager/frame-pager/frame-pager';

@Component({
  selector: 'app-std-page',
  templateUrl: './std-page.component.html',
  styleUrls: ['./std-page.component.scss']
})
export class StdPageComponent extends FramePageComponent implements OnInit {
  @ViewChild('uploader', {static: false}) uploader: FrameUpload;
  @ViewChild('pager', {static: false}) pager: FramePager;

  errFilePath: string;

  constructor(private modalService: BsModalService) {
    super(modalService);
    this.modalComponent = StdDetailComponent;
  }

  ngOnInit() {
  }

  doUploaFile() {
    // 线程参数 和 业务参数
    const reqParam = {
      thread_name: '考生上报线程',
      error_flag: 'InActive',
      callback_type: 'Swal'
    };
    // 上传参数-模板
    const uploadParam = {
      uploadTitle: '考生导入',
      templateFile: 'ZkStdImp.xlsx',
      uploadCallback: this.uploadWithThreadCallback.bind(this),
      autoClose: true
    };
    ModalService.showUploadWindow(this.bsService, './std/reg/upload.htm', reqParam, uploadParam);
  }

  uploadWithThreadCallback(ajax) {
    console.log('upload callback --->' + ajax.errorCode + ' , ' + ajax.url);
    if (ajax.errorCode !== 0 && ajax.url) {
      this.errFilePath = ajax.url;
      MsgService.errorToastr('导入数据错误,请下载错误文件!');
    } else {
      MsgService.successToastr('导入成功!');
    }
    this.pager.reload();
  }

  // 导入错误文件
  downloaddError() {
    if (this.errFilePath && this.errFilePath.length > 0) {
      this.FuncService.ajaxDownload('std/reg/download.htm', {path: this.errFilePath});
    }
  }

  formatShow(weixin_id) {
    if (weixin_id && weixin_id.length > 0) {
      return '已绑定';
    }
    return '';
  }

  unbind(std) {
    MsgService.confirmAlert('确定解绑当前考生吗？', () => {
      AjaxService.requestEntity('std/unbind.htm', {std_id: std.std_id, weixin_id: std.weixin_id}, (entity) => {
        this.pager.reload();
        MsgService.successToastr('解绑成功!');
      });
    });
  }

  uploadImage() {
    // 线程参数 和 业务参数
    const reqParam = {
      thread_name: '考生照片导入线程',
      error_flag: 'InActive',
      callback_type: 'Swal'
    };
    // 上传参数-模板
    const uploadParam = {
      uploadTitle: '考生照片导入',
      templateFile: 'stdImage.zip',
      uploadCallback: this.uploadImageCallback.bind(this),
      autoClose: true
    };
    ModalService.showUploadWindow(this.bsService, './std/reg/image/upload.htm', reqParam, uploadParam);
  }

  uploadImageCallback(ajax) {
    if (ajax.errorCode === 0) {
      MsgService.successToastr('导入成功!');
     // this.pager.reload();
    }

  }

  export() {

  }
}
