import {PagerComponent} from '../../../core/pager/pager.component';
import {ElementRef, ViewChild} from '@angular/core';
import {BsModalRef, BsModalService, ModalOptions} from 'ngx-bootstrap';
import {AjaxService} from '../../../core/ajax/ajax.service';
import {DomSanitizer} from '@angular/platform-browser';
import {FrameThreadRunningComponent} from '../thread/frame-thread-running/frame-thread-running.component';
import {MsgService} from '../../../core/msg/msg.service';
import {FrameService} from '../../../core/frame/frame.service';
import {AjaxComponent} from '../../../core/ajax/ajax.component';

export class FramePageComponent {
  // 当前在哪个步骤上
  currentStep = 'page';
  frameService: any;
  // 分页组件
  @ViewChild('pager', {static: false}) pager: PagerComponent;
  // 分页组件
  @ViewChild('ajax', {static: false}) ajax: AjaxComponent;
  // 自动获得焦点
  @ViewChild('elAutoFocus', {static: false}) elAutoFocus: ElementRef;

  // 弹框服务
  bsService: BsModalService;
  sanitizer: DomSanitizer;
  // 弹窗参数
  bsOptions: ModalOptions;
  // 弹出框类型
  modalComponent: any;

  private bsModalRef: BsModalRef;

  constructor(...args) {
    this.frameService = FrameService;
    for (const s of args) {
      if (s instanceof BsModalService) {
        this.bsService = s;
      }
      if (s instanceof DomSanitizer) {
        this.sanitizer = s;
      }
    }
    this.bsOptions = {
      animated: true,
      keyboard: false,
      backdrop: true,
      ignoreBackdropClick: true,
      initialState: {},
      class: 'modal-75p'
    };
    if (this.bsService === undefined) {
      this.bsService = AjaxService.injector.get<BsModalService>(BsModalService);
    }
    this.bsService.onHidden.subscribe((r: string) => {
      // 防止多次订阅，产生多次刷新的问题
      if (this.pager) {
        this.pager.reload();
      }
      this.onModalClose();
    });


    setTimeout(() => {
      if (this.elAutoFocus) {
        this.elAutoFocus.nativeElement.focus();
      }
    }, 0);
  }

  onModalClose() {

  }

  // 弹框, 弹窗之前做好数据获取准备，为防止弹出窗口后报错
  showComponentModal(component: any, param?: any, options?: any) {
    if (param === undefined) {
      param = {};
    }
    const modal = Object.assign({}, this.bsOptions, options);
    modal.initialState = param;
    modal.initialState['FramePageComponent'] = this;
    // 如果需要去服务器请求数据
    if (param['entityUrl']) {
      AjaxService.requestEntity(param['entityUrl'], param['entityParam'], (entity) => {
        modal.initialState['ResponseEntity'] = entity;
        this.bsModalRef = this.bsService.show(component, modal);
      });
    } else if (param['mapUrl']) {
      AjaxService.requestMap(param['mapUrl'], param['mapParam'], (map) => {
        modal.initialState['ResponseMap'] = map;
        this.bsModalRef = this.bsService.show(component, modal);
      }, {RequestWithLoading: true});
    } else if (param['arrayUrl']) {
      AjaxService.requestArray(param['arrayUrl'], param['arrayParam'], (array) => {
        modal.initialState['ResponseArray'] = array;
        this.bsModalRef = this.bsService.show(component, modal);
      }, {RequestWithLoading: true});
    } else {
      this.bsModalRef = this.bsService.show(component, modal);
    }
  }

  // 弹出非预定义组件的框
  showModal(param?: any, options?: any) {
    this.showComponentModal(this.modalComponent, param, options);
  }

  ajaxDownload(url: string, param?: any) {
    AjaxService.ajaxDownload(url, param);
  }

  showThreadRunning(thread: any, data?: any) {
    MsgService.showThreadRunning(this.bsService, thread, data);
  }


  requestWithThread(url, data?, param?) {
    if (param === undefined)
      param = {};
    if (param.confirmMsg) {
      MsgService.confirm(param.confirmMsg, () =>{
        AjaxService.request(url, data, (ajax) => {
          this.showThreadRunning(ajax.entity);
        });
      })

    }else {
      AjaxService.request(url, data, (ajax) => {
        this.showThreadRunning(ajax.entity);
      });
    }
  }

  downloadReport(param) {
    //(click)="ajaxDownload('frame/report/download.htm', {report_name:'LayoutMaterialWrittenRoom', report_title :group?.group_name + '考场汇总-', ly_group_id:group.ly_group_id})"
    AjaxService.requestEntity('frame/report/download/thread.htm', param, (entity) => {
      MsgService.showThreadRunning(this.bsService, entity, {threadTitle: '报表生成中，请稍候'});
    });
  }
}
