import { Component, OnInit } from '@angular/core';
import { FramePageComponent } from '../../core/FramePageComponent';
import { constructor } from 'lodash';
import { AjaxService } from '../../../../core/ajax/ajax.service';
import { BsModalService } from 'ngx-bootstrap';
import { MsgService } from '../../../../core/msg/msg.service';

@Component({
  selector: 'app-frame-thread-page',
  templateUrl: './frame-thread-page.component.html',
  styleUrls: ['./frame-thread-page.component.scss']
})
export class FrameThreadPageComponent extends FramePageComponent implements OnInit {


  FIND_THREAD_URL = "frame/thread/find.htm";
  DOWN_LOAD_URL = "frame/thread/download.htm";

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

  ngOnInit() {
  }

  //下载文件和弹窗
  requestResult(thread_id: any) {
    AjaxService.requestEntity(this.FIND_THREAD_URL, { thread_id: thread_id }, (entity) => {
      let path = entity.callback_param;
      let fileName = path.substring(path.lastIndexOf('\\') + 1, path.length);
      if (entity.thread_status === 'Complete') {
        if (entity.callback_type === 'Download') {
          AjaxService.ajaxDownload(this.DOWN_LOAD_URL, { file_name: fileName, path: path });
          MsgService.successAlert('任务已完成，该任务将返回一个文件，我们已经在新窗口开始了文件下载，请留意下载进度！');
        } else if (entity.callback_type === 'Swal') {
          MsgService.successAlert(entity.callback_param);
        }
      } else if (entity.thread_status === 'Error') {
        if (entity.callback_type === 'Download') {
          AjaxService.ajaxDownload(this.DOWN_LOAD_URL, { file_name: fileName, path: path });
          MsgService.errorAlert('任务异常，处理结果返回一个文件，我们已经在新窗口开始了文件下载，请留意下载进度！');
        } else if (entity.callback_type === 'Swal') {
          MsgService.errorAlert(entity.callback_param);
        }
      }

    });
  }



}
