import request from "./request";
import moment from "moment";

export async function queryWorkType(params) {
  return request(`v2/workload/type/info`, "GET", params, true);
}

export async function queryAllWorkType() {
  uni.showLoading();
  try {
    const { data } = await queryWorkType({ parent_id: -1 });
    let typeList = [];
    let allType = {};
    for (let i = 0; i < data.length; i++) {
      let item = data[i];
      allType[item.id] = item;
      if (item.parent_id == 0) {
        typeList.push(item);
      }
    }
    return { typeList, allType };
  } catch (error) {
    console.log(error);
    // return { typeList: [], allType: [] };
  } finally {
    uni.hideLoading();
  }
}
/**
 * 
    + pageSize:每页记录数
    + currentPage：当前页
    + project_id：项目id
    + user_id：用户id
    + dep_id：部门id
    + type_id：工作分项id
    + audit_status：审核状态
    + s_time：开始时间
    + e_time：结束时间 
 */
// 查询本人工时列表
export async function queryWorkHours(params = {}) {
  params.pageSize = 9999;
  const res = await request(`v2/workload/record`, "GET", params);
  const { list } = res.data;
  var data = {};
  list.forEach((item) => {
    var day = moment(item.ts).format("YYYY-MM-DD");
    item.day = day;
    if (!data[day]) data[day] = [];
    data[day].push(item);
  });
  return data;
}
// 查询可审核工时
export async function queryAuthWorkHours(params = {}) {
  params.pageSize = 9999;
  const res = await request(`v2/workload/list/auth`, "GET", params);
  const { list } = res.data;
  var data = {};
  list.forEach((item) => {
    var day = moment(item.ts).format("YYYY-MM-DD");
    item.day = day;
    if (!data[day]) data[day] = [];
    data[day].push(item);
  });
  return data;
}

export async function queryProject(data) {
  const res = await request(`v2/workload/project`, "GET", data);
  return res.data.list.map((item) => ({
    ...item,
    fullName: `${item.Name}(${item.Code})`,
  }));
}
/** id，status，desc */
export async function authWorkload(data) {
  return await request(`v2/workload/auths`, "POST", data);
}

export async function addWorkHours(data) {
  return await request(`v2/workload/record`, "POST", data);
}

export async function addAuthWorkHours(data) {
  return await request(`v2/workload/records`, "POST", data);
}
