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

@Component({
  selector: 'frame-ajax',
  templateUrl: './ajax.component.html'
})
export class AjaxComponent implements OnInit, OnDestroy {
  @Input()
  url: string;
  @Input()
  showLoading = false;
  @Input()
  ajaxType = 'Entity';
  @Input()
  autoRequest = true;
  @Input()
  requestData: {};
  @Input()
  requestParam: any;
  @Input()
  callback: Function;


  private responseData: any;
  private responseMap: any;
  private responseArray: any;
  private responseEntity: any;


  private intervalId: any;


  constructor(private bsService: BsModalService) {
    this.responseData = {};
    this.responseMap = {};
    this.responseArray = [];
    this.responseEntity = {};
  }


  getMap() {
    return this.responseMap;
  }

  getArray() {
    return this.responseArray;
  }

  getEntity() {
    return this.responseEntity;
  }

  /**********************
   * 另外独立的请求
   */
  request(url?: string, data?: {}, callback?: Function, param?: {}) {
    const vUrl = url === undefined ? this.url : url;
    const vData = Object.assign({}, this.requestData, data);
    const vCallback = callback === undefined ? this.callback : callback;
    const vParam = Object.assign({}, this.requestParam, param);

    if (vParam.RequestType === undefined) {
      vParam.RequestType = this.ajaxType;
    }
    AjaxService.request(vUrl, vData, (response) => {
      if (vParam.RequestType === 'Entity') {
        this.responseEntity = response;
        if (vCallback) {
          vCallback.call(this, response);
        }
      } else if (vParam.RequestType === 'Map') {
        this.responseMap = response;
        if (vCallback) {
          vCallback.call(this, response);
        }
      } else if (vParam.RequestType === 'Array') {
        this.responseArray = response;
        if (vCallback) {
          vCallback.call(this, response);
        }
      } else {
        this.responseData = response;
        if (vCallback) {
          vCallback.call(this, response);
        }
      }
    }, vParam);

  }

  reload(data?) {
    this.request(undefined, data);
  }

  public optrRecord(url: string, data: {}, param?: { successMsg?: string, successMsgType?: string, confirmMsg?: string, callback?: Function }, options?: { RequestWithLoading?: boolean }) {

    if (options === undefined) {
      options = {};
    }
    if (options.RequestWithLoading === undefined) {
      options.RequestWithLoading = true;
    }

    const confirmMsg = param ? param.confirmMsg : undefined;

    const fn = () => {
      AjaxService.request(url, data, (response) => {
        // 判断是否是后台线程
        if (response.threadId) {
          // 如果返回的json有threadId，表示是一个后台线程
          MsgService.showThreadRunning(this.bsService, response.entity, {threadTitle: '请求已提交，数据后台处理中......'}, param);
        } else {
          const successMsg = param ? (param.successMsg ? param.successMsg : '操作成功') : '操作成功';
          if (successMsg) {
            const successMsgType = param ? (param.successMsgType ? param.successMsgType : 'Toaster') : 'Toaster';

            if (successMsgType === 'Toaster') {
              MsgService.successToaster(successMsg);
            } else if (successMsgType === 'Swal') {
              MsgService.successAlert(successMsg);
            } else {
              alert(successMsg);
            }
          }
          const callback = param ? param.callback : undefined;
          if (callback) {
            callback.call(this, data);
          }
        }
      }, options);
    };

    if (confirmMsg) {
      MsgService.confirm(confirmMsg, fn);
    } else {
      fn.call(this);
    }

  }

  ngOnInit(): void {
    if (this.autoRequest) {
      setTimeout(() => {
        this.request();
      }, 10);

    }

  }

  ngOnDestroy(): void {
    clearTimeout(this.intervalId);
  }

}
