window.colorConfig = {
  normal: ['#3B6AF6', '#CDE0FC'],
  warning: ['#FF9C00', '#FBE8AB'],
  error: ['#F52F3E', '#F8D7D5'],
};
function strToObj(str) {
  var n = str.split('\n').filter(function (item) {
    return !item.startsWith('#');
  });
  var obj = {};
  for (var i = 0; i < n.length; i++) {
    var lastSpaceIndex = n[i].lastIndexOf(' ');
    if (lastSpaceIndex > -1) {
      var outKey = n[i].slice(0, lastSpaceIndex);
      var outValue = n[i].slice(lastSpaceIndex + 1);
      let matchs = outKey.match(/{.+=.+}/g);
      if (matchs) {
        var cutOutKey = outKey.split('{')[0];
        var child = {};
        let kToVArr = matchs[0]
          .replaceAll(/[{}"]/g, '')
          .split(',')
          .filter((item) => !!item);
        kToVArr.forEach(function (kToV) {
          var kvArr = kToV.split('=');
          child[kvArr[0]] = kvArr[1];
        });
        if (!obj[cutOutKey]) {
          obj[cutOutKey] = [{ conditions: child, value: outValue }];
        } else {
          obj[cutOutKey].push({ conditions: child, value: outValue });
        }
      } else {
        obj[outKey] = outValue;
      }
    }
  }
  return obj;
}
function createPie(ratio, domId, size) {
  var myChart = echarts.init(document.getElementById(domId));
  const handred = 100;
  let point = parseInt(ratio * 100);
  var colors =
    ratio >= 0.9
      ? colorConfig.error
      : ratio >= 0.7
      ? colorConfig.warning
      : colorConfig.normal;
  option = {
    title: {
      text: point + '%',
      x: 'center',
      y: 'center',
      textStyle: {
        fontWeight: 'bold',
        color: '#262626',
        fontSize: '28',
      },
    },
    tooltip: {
      formatter: function (params) {
        return params.name + '：' + params.percent + ' %';
      },
    },
    legend: {
      show: false,
      itemGap: 12,
      data: ['占比', '剩余'],
    },

    series: [
      {
        name: 'circle',
        type: 'pie',
        clockWise: true,
        radius: ['54', '78'],
        itemStyle: {
          normal: {
            label: {
              show: false,
            },
            labelLine: {
              show: false,
            },
          },
        },
        hoverAnimation: false,
        data: [
          {
            value: point,
            name: '占比',
            itemStyle: {
              normal: {
                color: colors[0],
                label: {
                  show: false,
                },
                labelLine: {
                  show: false,
                },
              },
            },
          },
          {
            name: '剩余',
            value: handred - point,
            itemStyle: {
              normal: {
                color: colors[1],
              },
            },
          },
        ],
      },
    ],
  };
  myChart.setOption(option);
}
function fillTextColor(domId, val, arr) {
  $(domId).css(
    'color',
    val > arr[1]
      ? colorConfig.error[0]
      : val > arr[0]
      ? colorConfig.warning[0]
      : '#262626',
  );
}
function fillTextColorByRatio(domId, val, ratioArr) {
  $(domId).css(
    'color',
    val > ratioArr[1]
      ? colorConfig.error[0]
      : val > ratioArr[0]
      ? colorConfig.warning[0]
      : '#262626',
  );
}
function getValueByReduce(arr, key) {
  var newArr = JSON.parse(JSON.stringify(arr));
  if (key) {
    newArr = arr.filter(function (item) {
      return item.conditions.area === 'heap';
    });
  }
  return newArr.reduce(function (total, item) {
    return total + Number(item.value);
  }, 0);
}

function countDown(dec) {
  //得到天 格式化成前缀加零的样式
  var d = parseInt(dec / 60 / 60 / 24);
  d = d < 10 ? '0' + d : d;
  //得到小时 格式化成前缀加零的样式
  var h = parseInt((dec / 60 / 60) % 24);
  h = h < 10 ? '0' + h : h;
  //得到分钟 格式化成前缀加零的样式
  var m = parseInt((dec / 60) % 60);
  m = m < 10 ? '0' + m : m;

  //得到秒 格式化成前缀加零的样式
  s = parseInt(dec % 60);
  s = s < 10 ? '0' + s : s;

  return `${d}  ${h}:${m}:${s}`;
}
