import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
import { request } from '@/plugins/http-fetch';
import { parseDateToStr } from '@/utils/date-utils';
/**
 * 查询公告列表
 * @param query 查询参数
 * @returns object
 */
export async function listMain() {
  const result = await request({
    url: '/api/rest/systemManagement/v1/elementType/all/objectType/systemState',
    method: 'get',
    timeout: 30 * 1000,
  });
  // console.log(result);
  let realData = result.data.data;
  const mergedData = realData.map((obj: any) => {
    // console.log(obj);
    const [key, value] = Object.entries(obj)[0];
    const ipAddress = (value as any).ipAddress;
    const systemState = (value as any).systemState;
    const errCode = systemState && systemState['errorCode'];
    var time = new Date();
    // console.log(key, value);
    let mergedObj;
    if (errCode === undefined && systemState) {
      mergedObj = {
        ...systemState,
        refresh: parseDateToStr(time),
        ipAddress: ipAddress,
        name: key.split('/').join('_'),
        status: '正常',
      };
    } else {
      mergedObj = {
        version: '-',
        refresh: parseDateToStr(time),
        ipAddress: ipAddress,
        name: key.split('/').join('_'),
        status: '异常',
      };
    }
    return mergedObj;
  });

  // console.log(mergedData);
  //  console.log(rowArr)
  return mergedData;
}

/**
 * 获取服务器时间
 * @returns object
 */
export async function getServerTime() {
  // 发起请求
  const result = await request({
    url: `/api/rest/systemManagement/v1/elementType/OMC/objectType/time`,
    method: 'get',
  });
  // 解析数据
  if (result.code === RESULT_CODE_SUCCESS && result.data) {
    return Object.assign(result, {
      data: result.data.data,
    });
  }
  return result;
}
