/**
 * 参数处理
 * @param {*} params  参数
 */
export function tansParams(params) {
  let result = '';
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    var part = encodeURIComponent(propName) + '=';
    if (value !== null && value !== '' && typeof value !== 'undefined') {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== '' && typeof value[key] !== 'undefined') {
            let params = propName + '[' + key + ']';
            var subPart = encodeURIComponent(params) + '=';
            result += subPart + encodeURIComponent(value[key]) + '&';
          }
        }
      } else {
        result += part + encodeURIComponent(value) + '&';
      }
    }
  }
  return result;
}
// 转换字符串，undefined,null等转化为""
export function parseStrEmpty(str) {
  if (!str || str == 'undefined' || str == 'null') {
    return '';
  }
  return str;
}
// 日期格式化
export function parseTime(time, pattern) {
  if (arguments.length === 0 || !time) {
    return null;
  }
  const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}';
  let date;
  if (typeof time === 'object') {
    date = time;
  } else {
    if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
      time = parseInt(time);
    } else if (typeof time === 'string') {
      time = time
        .replace(new RegExp(/-/gm), '/')
        .replace('T', ' ')
        .replace(new RegExp(/\.[\d]{3}/gm), '');
    }
    if (typeof time === 'number' && time.toString().length === 10) {
      time = time * 1000;
    }
    date = new Date(time);
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay(),
  };
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key];
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value];
    }
    if (result.length > 0 && value < 10) {
      value = '0' + value;
    }
    return value || 0;
  });
  return time_str;
}
// 千分号
export function Micrometer(num) {
  let numt = (num || 0).toString().split('.');
  if (numt[1] == undefined) {
    return numt[0].replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
  } else {
    return numt[0].replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') + '.' + numt[1];
  }
}
export function weeklyTimeDivision(time, type) {
  if (type == 0) {
    return time.split(' ')[0] + '  ';
  } else {
    return time.split(' ')[1];
  }
}
export function parseTimeParagraph(dayTime) {
  var currentDate = new Date(dayTime);
  var timesStamp = currentDate.getTime();
  var currenDay = currentDate.getDay();
  var dates = [];
  var tabTime = [];
  for (var i = 0; i < 14; i++) {
    var dataTime = new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - ((currenDay + 6) % 7))).toLocaleDateString().replace(/\//g, '-');
    var dataTimeArr = dataTime.split('-');
    if (dataTimeArr[1] < 10) {
      dataTimeArr[1] = '0' + dataTimeArr[1];
    }
    if (dataTimeArr[2] < 10) {
      dataTimeArr[2] = '0' + dataTimeArr[2];
    }
    dates.push(dataTimeArr.join('-') + ' ' + ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][new Date(dataTime.toString().replace(/-/g, '/')).getDay()]);
  }
  tabTime.push(dates[0]);
  tabTime.push(dates[4]);
  return dates;
}
export function weeklay(dataTime) {
  return ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][new Date(dataTime).getDay()];
}

// 回显数据字典
export function selectDictLabel(datas, value) {
  var actions = [];
  Object.keys(datas).some((key) => {
    if (datas[key].dictValue == '' + value) {
      actions.push(datas[key].dictLabel);
      return true;
    }
  });
  return actions.join('');
}

export function selectDictLabelu(datas, value) {
  var actions = [];
  Object.keys(datas).some((key) => {
    if (datas[key].dictValue == '' + value) {
      actions.push(datas[key].text);
      return true;
    }
  });
  return actions.join('');
}

// 回显数据字典（字符串数组）
export function selectDictLabels(datas, value, separator) {
  var actions = [];
  var currentSeparator = undefined === separator ? ',' : separator;
  var temp = value.split(currentSeparator);
  Object.keys(value.split(currentSeparator)).some((val) => {
    Object.keys(datas).some((key) => {
      if (datas[key].dictValue == '' + temp[val]) {
        actions.push(datas[key].dictLabel + currentSeparator);
      }
    });
  });
  return actions.join('').substring(0, actions.join('').length - 1);
}

// 字符串格式化(%s )
export function sprintf(str) {
  var args = arguments,
    flag = true,
    i = 1;
  str = str.replace(/%s/g, function () {
    var arg = args[i++];
    if (typeof arg === 'undefined') {
      flag = false;
      return '';
    }
    return arg;
  });
  return flag ? str : '';
}

// 转换字符串，undefined,null等转化为""
export function praseStrEmpty(str) {
  if (!str || str == 'undefined' || str == 'null') {
    return '';
  }
  return str;
}

//
export function twoPointSum(latA, lonA, latB, lonB) {
  var coordinate = 6371000;
  var PI = 3.14159265358979324;
  var x = Math.cos((latA * PI) / 180) * Math.cos((latB * PI) / 180) * Math.cos(((lonA - lonB) * PI) / 180);
  var y = Math.sin((latA * PI) / 180) * Math.sin((latB * PI) / 180);
  var s = x + y;
  if (s > 1) s = 1;
  if (s < -1) s = -1;
  var alphabet = Math.acos(s);
  var PointSum = alphabet * coordinate;
  return PointSum;
}

export function CJ02BD(gcjLat, gcjLon) {
  var x = gcjLon,
    y = gcjLat;
  var x_pi = (3.14159265358979324 * 3000.0) / 180.0;
  var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
  var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
  var bdLon = z * Math.cos(theta) + 0.0065;
  var bdLat = z * Math.sin(theta) + 0.006;

  return {
    lat: bdLat,
    lon: bdLon,
  };
}
