import { updatePortfolioStatus } from "@/api/portfolio";

// 更改投资组合状态
export async function updateStatus(id, status, reason) {
  const params = { id, status, reason };
  const res = await updatePortfolioStatus(params).catch(err => {
    this.$message.error(err.message);
  });
  if (res.code === 0) {
    this.$message({ type: "success", message: "请求成功!" });
  } else {
    this.$message.error(res.message);
  }
}

export function getNumToTime(str) {
  try {
    const hour = str.substring(0, 2);
    const min = str.substring(2, 4);
    const second = str.substring(4, 6);
    return `${hour}:${min}:${second}`;
  } catch (error) {
    console.error(error.message);
  }
}

export function getNumToDate(str) {
  try {
    str = Number(str).toFixed();
    const year = str.substring(0, 4);
    const month = str.substring(4, 6);
    const date = str.substring(6, 8);
    return `${year}-${month}-${date}`;
  } catch (error) {
    console.error(error.message);
  }
}

export function getNumToDateAll(str) {
  try {
    str = Number(str).toFixed();
    let date = str.substring(0, 8);
    // let time = str.substring(8, 14)
    let time;
    if (str.length > 8) {
      time = str.substring(8, 14);
      time = getNumToTime(time);
    }
    date = getNumToDate(date);
    // time = getNumToTime(time)
    if (str.length > 8) {
      return `${date} ${time}`;
    } else {
      return `${date}`;
    }
  } catch (error) {
    console.error(error.message);
  }
}

export function getTimeToNum(time, type) {
  // console.log(time)
  if (!time) {
    return;
  }
  const date = time;
  let year = date.getFullYear();
  const month = PrefixInteger(date.getMonth() + 1, 2);
  if (type !== 0) {
    year = PrefixInteger(date.getFullYear() - type, 4);
  }
  const day = PrefixInteger(date.getDate(), 2);
  return `${year}${month}${day}`;
}

export function PrefixInteger(num, length) {
  return ("0000000000000000" + num).substr(-length);
}

export function toFixedNum(num, length) {
  try {
    const thisNum = num;
    return thisNum.toFixed(length);
  } catch (error) {
    return num;
  }
}
