import {Component, OnDestroy, OnInit} from '@angular/core';
import {BsModalRef} from 'ngx-bootstrap';
import {FramePageComponent} from '../../core/FramePageComponent';
import {AjaxService} from '../../../../core/ajax/ajax.service';
import {environment} from '../../../../../environments/environment';
import {MsgService} from '../../../../core/msg/msg.service';

@Component({
  selector: 'app-frame-thread-running',
  templateUrl: './frame-thread-running.component.html',
  styleUrls: ['./frame-thread-running.component.scss']
})
export class FrameThreadRunningComponent implements OnInit, OnDestroy {

  threadParam: any;
  frameThread: any;
  // 去服务器获取线程间隔时长，单位秒
  intervalSeconds = 2;
  // 是否允许操作员解除当前界面
  allowCancel = false;
  // 信息显示模式，
  msgType = 'array';
  // 标题
  runningTitle = '后台线程运行中，请稍后';



  msgArray = new Array();


  private closed;

  constructor(private bsModalRef: BsModalRef) {
    this.frameThread = {};
  }

  ngOnInit() {
    setTimeout( () => {
      this.msgArray.push(this.frameThread);
      this.requestMsg();
    }, 100);
  }

  requestMsg() {
    if (!this.closed) {
      AjaxService.requestEntity('frame/thread/find.htm', {thread_id: this.frameThread.thread_id}, (entity) => {
        if (entity.thread_notice !== this.frameThread.thread_notice) {
          if (this.msgType === 'entity') {
            this.msgArray = [entity];
          } else {
            this.msgArray.push(entity);
          }
        }
        this.frameThread = entity;
        if (entity.thread_status === 'Running') {
          setTimeout(this.requestMsg.bind(this), this.intervalSeconds * 1000);
        } else if (entity.thread_status === 'Complete') {
          // console.log(this);
           if (entity.callback_type === 'Download') {
             let json = eval('(' +this.frameThread.callback_param +')');
             // AjaxService.ajaxDownload('frame/temporary/download.htm', JSON.parse( this.frameThread.callback_param));
             AjaxService.ajaxDownload('frame/temporary/download.htm', json);
             MsgService.successAlert('任务已完成，该任务将返回一个文件，我们已经在新窗口开始了文件下载，请留意下载进度！');
           } else if (entity.callback_type === 'Swal') {
             MsgService.successAlert(entity.callback_param);
           }
           if (this.threadParam && this.threadParam.callback) {
             this.threadParam.callback.call(undefined);
           }
        } else if (entity.thread_status === 'Error') {
          // if (entity.callback_type === 'Download') {
          //   //JSON.parse(this.frameThread.callback_param)
          //   // AjaxService.ajaxDownload('frame/temporary/download.htm', eval('(' +this.frameThread.callback_param +')'));
          //   MsgService.errorAlert('任务异常，处理结果返回一个文件，我们已经在新窗口开始了文件下载，请留意下载进度！');
          // } else if (entity.callback_type === 'Swal') {
            MsgService.errorAlert(entity.callback_param);
         // }
        }
      });
    }
  }

  // 关闭窗体
  closeWindow() {
    this.bsModalRef.hide();
  }

  ngOnDestroy(): void {
    this.closed = true;
  }

}
