const Notify = require('../widget/Notify.js');

module.exports = {
  pageSize: 20,

  // 异常处理
  processException: function (XMLHttpRequest, textStatus, errorThrown) {
    const _self = this;
    console.log(XMLHttpRequest);
    if (XMLHttpRequest.status === 400) {
      // 400 Bad Request
      Notify.error('提示', XMLHttpRequest.responseText, true);
    } else if (XMLHttpRequest.status === 401) {
      const currentUrl = window.location;
      const href = window.location.href;
      // Notify.error('提示', XMLHttpRequest.responseText, true);
      // 当前未处于登陆的界面
      // 系统未登录
      if (href.indexOf('login') < 0 && href.indexOf('redirectUrl=') < 0) {
        // 处理钉钉免登陆
        const clientId = this.getRouteParam('clientId');
        const appName = this.getRouteParam('appName');
        const corpId = this.getRouteParam('corpId');

        let newUrl;
        if (clientId != null && clientId.length > 0 && appName != null && appName.length > 0 && corpId != null && corpId.length > 0) {
          newUrl = _self.getRedirectUrl('index.html?#/login?clientId=' + clientId + '&appName=' + appName + '&corpId=' + corpId + '&redirectUrl=' + encodeURIComponent(currentUrl));
        } else {
          newUrl = _self.getRedirectUrl('index.html?#/login?redirectUrl=' + encodeURIComponent(currentUrl));
        }
        window.location = newUrl;
      }
    } else if (XMLHttpRequest.status === 500) {
      // 500 Internal Server Error
      Notify.error('提示', XMLHttpRequest.responseText, true);
      if (XMLHttpRequest.responseText.indexOf('登录超时') > 0) {
        // 如果异常信息包含“登录超时”，则2秒后跳转到登录页面
        setTimeout(function () {
          window.location = _self.getRedirectUrl('/index.html?#/login');
        }, 2 * 1000);
      }
    } else {
      Notify.error('服务器异常', XMLHttpRequest.responseText, true);
    }
  },

  /**
	 * 获取跳转的路径
	 * @param {*} url
	 */
  getRedirectUrl: function (url) {
    const href = window.location.href;
    if (href.indexOf('wmsapp.html') >= 0) {
      return this.getRootPath() + '/wmsapp.html/' + url;
    } else if (href.indexOf('wmsapp') >= 0) {
      return this.getRootPath() + '/wmsapp/' + url;
    } else {
      return this.getRootPath() + url;
    }
  },

  // 获取API的地址
  getApiURL: function (apiName) {
    return '/api/' + apiName;
  },

  // 给请求头中加上account和token信息
  addTokenToRequest: function (request) {
    let token = $.cookie('token');
    let account = $.cookie('account');
    if (token == undefined) {
      const localStorageToken = localStorage.getItem('token');
      if (localStorageToken != undefined) {
        token = localStorageToken;
      }
    }
    if (account == undefined) {
      const localStorageAccount = localStorage.getItem('account');
      if (localStorageAccount != undefined) {
        account = localStorageAccount;
      }
    }
    request.setRequestHeader('account', account);
    request.setRequestHeader('token', token);
  },

  /**
	 * 获取Token
	 */
  getToken: function () {
    return $.cookie('token');
  },

  // 清空 Cookie
  clearCookie: function () {
    document.cookie.split(';').forEach(function (c) {
      document.cookie = c.replace(/^ +/, '').replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/');
    });
  },

  clearLocalStorage: function () {
    // 清理localStorage时需要保留的参数列表
    const reserveParams = ['hostPageBaseURL', 'workShopId', 'resourceInstanceId',
      'resourceInstanceName', 'apsBaseUrl', 'cameraBaseURL',
    ];
    // 存放的信息
    const reserveParamValues = [];

    // 获取参数信息
    const len = reserveParams.length;
    for (let i = 0; i < len; i++) {
      const reserveParam = reserveParams[i];
      let reserveParamValue = '';
      if (localStorage.getItem(reserveParam) != undefined) {
        reserveParamValue = localStorage.getItem(reserveParam);
      }
      reserveParamValues.push(reserveParamValue);
    }

    // 清理localStorage
    window.localStorage.clear();

    // 还原参数信息
    for (let i = 0; i < len; i++) {
      localStorage.setItem(reserveParams[i], reserveParamValues[i]);
    }
  },

  /**
	 * 修复多个模态框的BUG
	 * bootstrap中如果在模态框弹出模态框，即多重模态框，那么在关闭模态框后，上一级的模态框无法滚动，
	 * 而且滚动事件会穿透到body层。原理是只要有modal被打开，body会被赋予modal-open这个类，
	 * 使其overflow变为hidden，无法滚动；而只要有modal被关闭，body的modal-open这个类就会被remove掉。
	 * 所以解决方法如上代码，非最老的一层modal监听隐藏事件，为body重新添加modal-open类。
	 * @param {Object} uuid
	 */
  fixModalBug: function (uuid) {
    $('#' + uuid).on('hidden.bs.modal', function () {
      $(document.body).addClass('modal-open');
    });
  },

  getImageSrc: function (className, imageName) {
    let accountId = $.cookie('account');
    accountId = (accountId == null ? 0 : accountId);
    return this.getFileServerUrl() + 'Files/' + accountId + '/Images/' + className + '/' + imageName;
  },

  // 获取略缩图图片路径
  getThumbnailImageSrc: function (className, imageName) {
    let accountId = $.cookie('account');
    accountId = (accountId == null ? 0 : accountId);
    return this.getFileServerUrl() + 'Files/' + accountId + '/Images/' + className + '/thumbnail/' + imageName;
  },

  getImageError: function () {
    if (this.clientRunningEnvironment == 'BROWSER') {
      return '../../static/image/imageNotFound.png';
    } else {
      return '../static/image/imageNotFound.png';
    }
  },

  /**
	 * 图片找不到，加载默认的图片
	 */
  imgNotFound: function (event) {
    const img = event;
    if (this.clientRunningEnvironment == 'BROWSER') {
      img.src = '../../static/image/imageNotFound.png';
    } else {
      img.src = '../static/image/imageNotFound.png';
    }
    img.onerror = null;
  },

  /**
	 * 获取主机地址
	 */
  getRootPath: function () {
    // 获取当前网址，如： http://localhost:8083/myproj/view/my.jsp
    const curWwwPath = window.document.location.href;
    // 获取主机地址之后的目录，如： myproj/view/my.jsp
    const pathName = window.document.location.pathname;
    const pos = curWwwPath.indexOf(pathName);
    // 获取主机地址，如： http://localhost:8083
    const localhostPaht = curWwwPath.substring(0, pos);
    return localhostPaht;
  },

  getHostPageBaseURL: function () {
    return this.getRootPath() + '/';
  },

  // 获取图片路径url
  getFileServerUrl: function () {
    return this.getRootPath() + '/';
  },

  /**
   * 获取路由中的参数
   * @param {参数名称} name
   */
  getRouteParam: function (name) {
    const reg = new RegExp('(^|\\?|&)' + name + '=([^&]*)(\\s|&|$)', 'i');
    if (reg.test(location.href)) return unescape(RegExp.$2.replace(/\+/g, ' '));
    return '';
  },

  /**
     * 获取附件的路径
     * @param  {[type]} className [description]
     * @param  {[type]} imageName [description]
     * @return {[type]}           [description]
     */
  getAttachmentsSrc: function (className, imageName) {
    let accountId = $.cookie('account');
    accountId = (accountId == null ? 0 : accountId);
    return this.getFileServerUrl() + 'Files/' + accountId + '/Attachments/' + className + '/' + imageName;
  },
};
