import {Component, EventEmitter, Input, OnDestroy, OnInit, Output} from '@angular/core';
import {BsModalService} from "ngx-bootstrap/modal";
import {AjaxService} from "../../../service/ajax.service";
import {Pager} from "../../../service/pager";

@Component({
  selector: 'frame-ajax',
  templateUrl: './frame-ajax.html'
})
export class FrameAjax implements OnInit, OnDestroy {

  /*******************
   * 请求地址
   */
  @Input()
  url: string;
  /**********************
   * 请求是否显示操作等待界面
   */
  @Input()
  requestWithLoading = false;

  /*********************
   * 返回数据类型
   */
  @Input()
  ajaxDataType = 'entity';

  /********************
   * 是否自动请求数据
   */
  @Input()
  autoRequest = true;

  /*****************
   * 请求的数据
   */
  @Input()
  requestData: {};
  /******************
   * 请求参数信息
   */
  @Input()
  requestParam: any;

  /****************
   * 请求后回调参数
   */
  @Input()
  callback: Function;
  /****************
   * 轮询请求间隔时长，单位秒
   */
  @Input()
  intervalSeconds: number = 0;


  /****************
   * 数据加载完毕
   */
  @Output()
  ajaxRequestComplete = new EventEmitter<Pager>();

  // 定时器，用于轮询时使用
  private intervalId: any;
  // 请求回来的数据
  private responseData: any;
  // 请求回来的map
  private responseMap: any;
  // 请求回来的数组
  private responseArray: any;
  // 请求返回的实体
  private responseEntity: any;

  // bsService用于打开弹出框
  constructor(private bsService: BsModalService) {
    this.responseData = {};
    this.responseMap = {};
    this.responseArray = [];
    this.responseEntity = {};
  }

  ngOnInit(): void {
    if (this.autoRequest) {
      setTimeout(this.request.bind(this), 5);
    }
    // 判断是否是不间断请求
    if (this.intervalSeconds > 0)
      this.intervalId = setInterval(this.request.bind(this), this.intervalSeconds * 1000);
  }

  /**********************
   * Ajax请求
   */
  request(url?: string, data?: {}, callback?: Function, param?: {}, options?: {}) {
    // 实际请求的url
    const vUrl = url || this.url;
    // 实际请求的数据
    const vData = Object.assign({}, this.requestData, data);
    // 实际的回调函数
    const vCallback = callback || this.callback;
    const vParam = Object.assign({}, this.requestParam, param);
    if (vParam.showMsg === undefined)
      vParam.showMsg = false;

    if (vParam.RequestDataType === undefined)
      vParam.RequestDataType = this.ajaxDataType;
    let fn = function (response) {
      if (vParam.RequestDataType === 'entity') {
        this.responseEntity = response.entity;
      } else if (vParam.RequestDataType === 'map') {
        this.responseMap = response.map;
      } else if (vParam.RequestDataType === 'array') {
        this.responseArray = response.array;
      } else {
        this.responseData = response;
      }

      if (vCallback)
        vCallback.call(undefined, response);
      this.ajaxRequestComplete.emit(response);
    };
    vParam.callback = fn.bind(this);
    if(vUrl) {
      AjaxService.ajaxRequest(vUrl, vData, vParam, options);
    }

  }

  /****************
   * Ajax请求返回Map对象
   */
  public getMap() {
    return this.responseMap;
  }

  /****************
   * Ajax请求返回Array对象
   */
  public getArray() {
    return this.responseArray;
  }

  /****************
   * Ajax请求返回Entity对象
   */
  public getEntity() {
    return this.responseEntity;
  }

  /****************
   * Ajax请求返回
   */
  public getData() {
    return this.responseData;
  }


  /******************
   * 销毁前清理定时器
   */
  ngOnDestroy(): void {
    clearTimeout(this.intervalId);
  }

    /****************
   * 重新加载
   */
  reload(data?) {
    this.request(undefined, data);
  }

  public optrRecord(url: string, data: {}, param?: { successMsg?: string, successMsgType?: string, confirmMsg?: string, callback?: Function, showMsg?: boolean,}, options?: { RequestWithLoading?: boolean }) {
    param = param || {};
    let callback = param.callback || function () {
    };
    let fn = function (response) {
      callback.call(undefined, response);
      this.reload();
      this.ajaxRequestComplete.emit(response);
    };
    param.callback = fn.bind(this);
    AjaxService.ajaxRequest(url, data, param, options);

  }

}
