import request from '@/config/request';

async function http(config) {
  return new Promise(resolve => {
    request(config)
      .then(res => {
        resolve(res.data);
      })
      .catch(err => {
        console.log(err);
        resolve(false);
      });
  });
}

export const apiDevelopment = {
  LevelList,
  LevelDetail,
  CurrentTask,
  UploadFile,
  SubmitTask
};

export default apiDevelopment;

// personId 这个用于临时测试用
const personId = 15;

/**
 * 等级列表
 * @returns
 */
function LevelList() {
  return http({
    url: '/api/Level/List',
    method: 'get',
    params: {
      personId: personId
    }
  });
}

/**
 * 等级详情
 * @param
 * \{ levelId ，personId  \}
 *
 * @returns
 */
function LevelDetail(params) {
  return http({
    url: '/api/Level/LevelDetail',
    method: 'get',
    params: {
      personId: personId,
      ...params
    }
  });
}

/**
 * 任务列表
 */
function CurrentTask() {
  return http({
    url: '/api/Task/CurrentTask',
    method: 'get',
    params: {
      personId: personId
    }
  });
}

/**
 * 提交任务
 */

function SubmitTask(param) {
  return http({
    url: '/api/Task/Submit',
    method: 'post',
    data: {
      personId: personId,
     ...param
    }
  });
}

/**
 * 上传文件
 */
function UploadFile(param) {
  return http({
    url: "/api/File/Upload",
    method: "post",
    data: param,
    headers: { "Content-Type": "multipart/form-data" },
    transformRequest: function(data) {
      const formData = new FormData();
      let i = data.formData.entries();
      let j = i.next();
      while (!j.done) {
        formData.set(j.value[0], j.value[1]);
        j = i.next();
      }
      return formData;
    }
  });
}
