import { request } from 'umi';
import {
  DEFAULT_LAYOUT,
  DEFAULT_MAP_LAYOUT,
} from '@/Project/pages/DataMeter/config';

// 获得项目详情
export async function getProject(projectId: string | number) {
  return request(`/project/${projectId}`);
}

// 异常报警通知列表
export async function getFaultAnalysis(projectId: string | number) {
  return request(`/fault_analysis/result/${projectId}`);
}

// 项目预警
interface IProjectAlarmParams {
  pageSize: number;
  currentPage: number;
  msgType: string;
  projectId: number | string;
}
export async function getProjectAlarm(params: IProjectAlarmParams) {
  return request(`/notification/list`, {
    params: params,
  });
}

/**
 * 问题追踪列表
 */
interface IIssueListParams {
  currentPage: number;
  pageSize: number;
  projectId: number | string;
}
export async function getIssueList(params: IIssueListParams) {
  // return request(`/issue/list/${params.projectId}`);
  return request(`/issue/ticket/list/${params.projectId}`, {
    params: params,
  });
}

/**
 * 消息推送
 */
interface INotificationListParams {
  currentPage: number;
  pageSize: number;
}
export async function getNotificationList(params: INotificationListParams) {
  return request(`/notification/list?`, {
    params: params,
  });
}

/**
 * 上传多媒体资源
 */
interface IuploadMediaParams {
  type: number; //0-图片，1- 视频
  projectId: number;
  files: any[];
}
export async function uploadMedia(params: IuploadMediaParams) {
  return request(`/project-cabin-file/${params.projectId}/${params.type}`, {
    method: 'POST',
    body: {
      ...params,
    },
  });
}

/**
 * 多媒体资源查询
 * @param {object} params
 * @param {number} params.projectId
 * @returns
 */
export async function getMediaList(params: any) {
  return request(`/project-file/${params.projectId}/26/-1`);
}

/**
 * 视频监控查询
 * @param {object} params
 * @param {string} params.currentPage
 * @param {string} params.pageSize
 * @param {number} params.projectId
 * @returns
 */

export async function getMonitorList(projectId: string | number): Promise<any> {
  // return request(`/monitor/list/${params.projectId}`);
  let data: Api.IMonitor = await request(`/monitor_config/query/${projectId}`);
  let obj: any;
  try {
    obj = JSON.parse(data.config_json) || {};
  } catch (error) {
    obj = {};
  }
  return Object.keys(obj).map((key) => obj[key]) || [];
}

/**
 * 项目动态
 * @param {object} params
 * @param {string} params.currentPage
 * @param {string} params.pageSize
 * @param {number} params.projectId
 * @param {number} params.type 动态类型：-1-所有类型, 0-项目动态 1-新闻动态 2-大事记 3-所获奖项
 * @returns
 */

export async function getProjectActive(params: any) {
  return request(`/project-active/list/${params.projectId}/${params.type}}`, {
    params: params,
  });
}

/**
 * 设计｜采购｜施工详细进度
 * @param {object} params
 * @param {number} params.projectId
 * @returns
 */

export async function getProjectProgress(params: any) {
  return request(`/project-progress/${params.projectId}`);
}

/**
 * 设计｜采购｜施工总进度
 * @param {object} params
 * @param {number} params.projectId
 * @returns
 */

export async function getProjectTotalProgress(params: any) {
  return request(`/project-plan-progress/${params.projectId}`);
}

/**
 * 安全操作规范列表查询
 * @param {object} params
 * @param {number} params.projectId
 * @param {string} params.currentPage
 * @param {string} params.pageSize
 * @returns
 */

export async function getUserGuide(params: any) {
  return request(`/user_guide/list/${params.projectId}`, {
    params: params,
  });
}

/**
 * 项目日志列表查询
 * @param {object} params
 * @param {number} params.projectId
 * @param {string} params.currentPage
 * @param {string} params.pageSize
 * @returns
 */

export async function getDailyList(params: any) {
  return request(`/daily/list/${params.projectId}`, {
    params: params,
  });
}

/**
 * 删除资源
 * @param {object} params
 * @param {number} params.id
 * @returns
 */

export async function deleteFile(params: any) {
  return request(`/delete/project-cabin-file/${params.id}`, {
    method: 'POST',
  });
}

/**
 * 保存布局配置
 * @param {object} params
 * @param {number} params.projectId 项目id，地图页为0
 * @param {number} params.id 配置id，大于0，则更新
 * @param {number} params.module 所属模块：1-驾驶舱 2-模型分层 3-管道流向
 * @param {number} params.sub_module 子模块： （驾驶舱）0-地图驾驶舱 1-建设驾驶舱 2-运营驾驶舱
 * @param {string} params.config_json 配置json数据
 * @param {number} params.is_default 是否默认配置，大于1时会校验调用者权限
 * @returns
 */

export async function saveLayoutOptions(params: any) {
  return request(`/api/v1/project_config/save/${params.projectId || 0}`, {
    method: 'POST',
    body: {
      ...params,
    },
  });
}

/**
 * 读取配置
 * @param {object} params
 * @param {number} params.projectId 项目id，地图页为0
 * @param {number} params.module 所属模块：1-驾驶舱 2-模型分层 3-管道流向
 * @param {number} params.sub_module 子模块： （驾驶舱）0-地图驾驶舱 1-建设驾驶舱 2-运营驾驶舱
 * @param {number} params.is_default 是否默认配置，大于1时会校验调用者权限
 * @returns
 */

export async function getLayoutOptions(params: any) {
  const response = await request(
    `/api/v1/project_config/query/${params.projectId || 0}`,
    {
      params: params,
    },
  );

  var data = response.data;
  try {
    data.config_json = JSON.parse(data.config_json);
  } catch (error) {
    // 解析失败则使用默认布局
    var defaultLayout =
      params.sub_module === 0 ? DEFAULT_MAP_LAYOUT : DEFAULT_LAYOUT;
    data.config_json = defaultLayout;
  }
  return response;
}

// 获取故障列表
export async function getBreakdownRecord(params: any) {
  return request(`/breakdown-record/unhandled/list/${params.projectId}`);
}

//获取项目列表
export async function getProjectList(params: any) {
  const response = await request(`/api/v2/project`, {
    params: params,
  });
  let nowDate = new Date();
  (response.data.list || []).map((item: Api.IProject) => {
    let type;
    if (!item.EndDate) {
      type = 2;
    } else {
      type = nowDate >= new Date(item.EndDate) ? 2 : 1;
    }
    item.type = type;
  });
  return response;
}

//获取巡检结果
export async function getAutoPatrol(params: any) {
  return request(`/patrol/auto/data/${params.projectId}`);
}

export async function getBreakdownList(params: any) {
  return request(`/breakdown-record/list/${params.projectId}`, {
    params: params,
  });
}
export async function getPatrolRecord(params: any) {
  return request(`/patrol/data/${params.projectId}`, {
    params: params,
  });
}

export async function getMouldList(params: any) {
  return request(`/project_config/template-list/${params.project_id || 0}`, {
    params: params,
  });
}

export async function createMouldList(params: any) {
  return request(`/project_config/save-template/${params.projectId || 0}`, {
    method: 'POST',
    headers: { ContentType: 'application/x-www-form-urlencoded' },
    body: params.formData,
  });
}
export async function getAutoPatrolByRouteId(params: any) {
  return request(`/patrol/data/${params.projectId}/${params.routeId}`);
}
