var Common = require('../common/Common.js');

module.exports = {

  /**
     * 查询Trace
     */
  uniqueByTraceId: function (traceId) {
    return new Promise((resolve, reject) => {
      $.ajax({
        url: Common.getApiURL('TraceResource/uniqueByTraceId'),
        type: 'get',
        dataType: 'json',
        contentType: 'application/json',
        data: {
          'traceId': traceId,
        },
        beforeSend: function (request) {
          Common.addTokenToRequest(request);
        },
        success: function (data) {
          resolve(data);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          reject(XMLHttpRequest);
        },
      });
    });
  },

  /**
     * 设置追踪单已完成
     */
  updateTraceFinished: function (traceId) {
    return new Promise((resolve, reject) => {
      $.ajax({
        url: Common.getApiURL('TraceResource/updateTraceFinished'),
        type: 'post',
        contentType: 'application/json',
        beforeSend: function (request) {
          Common.addTokenToRequest(request);
        },
        data: JSON.stringify({
          'id': traceId,
        }),
        success: function (data) {
          resolve(data);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          reject(XMLHttpRequest);
        },
      });
    });
  },

  listFinishedByProjectId: function (projectId, pagination, content) {
    return new Promise((resolve, reject) => {
      $.ajax({
        url: Common.getApiURL('TraceResource/listFinishedByProjectId'),
        type: 'get',
        dataType: 'json',
        contentType: 'application/json',
        data: {
          'projectId': projectId,
          'currentPage': pagination.current_page,
          'pageSize': pagination.per_page,
          'content': content,
        },
        beforeSend: function (request) {
          Common.addTokenToRequest(request);
        },
        success: function (data) {
          resolve(data);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          reject(XMLHttpRequest);
        },
      });
    });
  },
  getImg: function () {
    var _self = this;
    Object.defineProperty(Image.prototype, 'authsrc', {
      writable: true,
      enumerable: true,
      configurable: true,
    });
    window.onload = () => {
      var imgs = document.getElementsByTagName('img');
      for (var i = 0, len = imgs.length; i < len; i++) {
        _self.requsetImg(imgs[i]);
      }
    };
  },
  requsetImg: function (img) {
    var url = img.getAttribute('authsrc');
    if (url != undefined && url != null) {
      var request = new XMLHttpRequest();
      request.responseType = 'blob';
      request.open('get', url, true);
      Common.addTokenToRequest(request);
      request.onreadystatechange = e => {
        if (request.readyState == XMLHttpRequest.DONE && request.status == 200) {
          img.src = URL.createObjectURL(request.response);
          img.onload = () => {
            URL.revokeObjectURL(img.src);
          };
        }
      };
      request.send(null);
    }
  },
};