import request from '@/utils/request'

// 查询项目申报v2列表
export function listPlan(query) {
  return request({
    url: '/scientific/plan/list',
    method: 'get',
    params: query
  })
}

// 查询项目申报v2详细
export function getPlan(handbookId) {
  return request({
    url: '/scientific/plan/' + handbookId,
    method: 'get'
  })
}

// 新增项目申报v2
export function addPlan(data) {
  return request({
    url: '/scientific/plan',
    method: 'post',
    data: data
  })
}

// 修改项目申报v2
export function updatePlan(data) {
  return request({
    url: '/scientific/plan',
    method: 'put',
    data: data
  })
}

// 删除项目申报计划
export function delPlan(planId) {
  return request({
    url: '/scientific/plan/' + planId,
    method: 'delete'
  })
}

// 封装寻找最新 表单 的函数，返回 Promise
export function findLatestDeploymentTimeIndex(response) {
  return new Promise((resolve, reject) => {
    let latestIndex = 0;
    let latestTime = new Date(response.rows[0].deploymentTime);

    response.rows.forEach((row, index) => {
      const currentTime = new Date(row.deploymentTime);

      if (currentTime > latestTime) {
        latestTime = currentTime;
        latestIndex = index;
      }
    });

    // 返回最新的索引
    resolve(latestIndex);
  });
}
