import {FrameService} from "../../../../core/service/frame.service";
import {Component, Directive, ElementRef, Type, ViewChild} from "@angular/core";
import {AjaxService} from "../../../../core/service/ajax.service";
import {FuncService} from "../../../../core/service/func.service";
import {MsgService} from "../../../../core/service/msg.service";
import {FramePager} from "../../../../core/frame/pager/frame-pager/frame-pager";
import {FrameAjax} from "../../../../core/frame/ajax/frame-ajax/frame-ajax";
import {BsModalRef, BsModalService, ModalOptions} from "ngx-bootstrap/modal";
import {VarService} from "../../../../core/service/var.service";
import {ModalService} from "../../../../core/service/modal.service";

/******************************
 * 列表类通用类
 */
@Directive()
export class FramePageComponent {

  /*******************
   * 框架辅助，用于获取后台常量、参数等
   */
  FrameService: any;
  /********************
   * Ajax请求辅助类，用于后台请求
   */
  AjaxService: any;
  /********************
   * 常用函数辅助类
   */
  FuncService: any;
  /********************
   * 消息服务辅助类
   */
  MsgService: any;
  /********************
   * 变量辅助类
   */
  VarService: any;
  /********************
   * 弹出框类
   */
  ModalService: any;


  // 分页组件
  @ViewChild('pager', {static: false}) pager: FramePager;
  // 分页组件
  @ViewChild('ajax', {static: false}) ajax: FrameAjax;
  // 自动获得焦点
  @ViewChild('elAutoFocus', {static: false}) elAutoFocus: ElementRef;
  /************
   * ngx弹出服务
   */
  bsService: BsModalService;
  /**************
   * ngx弹出框句柄
   */
  private bsModalRef: BsModalRef;
  /**************
   * 弹出参数信息
   */
  modalComponent: any;

  bsOptions: ModalOptions = {
    animated: true,
    keyboard: false,
    backdrop: true,
    ignoreBackdropClick: true,
    initialState: {},
    class: 'modal-75p'
  };

  /*******************
   * 构造函数，不定参数，满足不同环境下需求
   */
  constructor(...args) {
    // 初始化静态类
    this.FrameService = FrameService;
    this.AjaxService = AjaxService;
    this.FuncService = FuncService
    this.MsgService = MsgService;
    this.VarService = VarService;
    this.ModalService = ModalService;


    // 设置注入参数
    for (const s of args) {
      if (s instanceof BsModalService) {
        this.bsService = s;
      }
    }

    // 关闭弹出之后
    if (this.bsService) {
      this.bsService.onHidden.subscribe(() => {
        if (this.pager)
          this.pager.reload();
        this.onModalClose();
      });
    }

    // 自动定位焦点
    setTimeout(() => {
      if (this.elAutoFocus) {
        this.elAutoFocus.nativeElement.focus();
      }
    }, 0);

  }

  /***********************
   * ngx-bootstrap弹窗
   * @param param     传递的参数
   * @param options
   */
  showModal(param?: any, options?: ModalOptions) {
    this.showComponentModal(this.modalComponent, param, options);
  }

  /***********************
   * ngx-bootstrap弹窗
   * @param component 弹出的窗口使用哪个component
   * @param param     传递的参数
   * @param options
   */
  showComponentModal(component: any, param?: any, options?: ModalOptions) {
    if (!param)
      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);
    }
  }

  /***************************
   * 关闭窗口时调用的函数
   */
  onModalClose() {
  }

  /***************************
   * 线程下载报表
   */
  downloadReport(param) {
    AjaxService.requestEntity('frame/report/download/thread.htm', param, (entity) => {
      ModalService.showThreadWindow(this.bsService, entity, {threadTitle: '报表生成中，请稍候'});
    });
  }

  /***************************
   * 线程请求
   */
  requestWithThread(url, data?, param?, callback?) {
    if (param === undefined)
      param = {};
    if (param.confirmMsg) {
      MsgService.confirmAlert(param.confirmMsg, () =>{
        AjaxService.request(url, data, (ajax) => {
          ModalService.showThreadWindow(this.bsService, ajax.entity, callback);
        });
      })

    }else {
      AjaxService.request(url, data, (ajax) => {
        ModalService.showThreadWindow(this.bsService, ajax.entity, callback);
      });
    }
  }

}
