// table-list 所需的一系列操作方法

/**
 * formatter
 *
 * @param {*} tableData 表单数据
 * @param {*} column 列数据
 * @returns {string} val 解析后的结果
 */
const formatter = (tableData, column, val) => {
  const key = column.property;
  if (tableData.dict && tableData.dict[key]) {
    const dict = tableData.dict[key];
    return dict[val] || val;
  }
  return val;
};

/**
 * formatterAmount
 *
 * @param {*} row 单元格数据
 * @param {*} column 列数据
 * @returns {string} val 解析后的结果
 */
const formatterAmount = (row, column) => {
  const property = column.property;
  const amount = row[property];
  if (!amount) {
    return "0.00";
  }
  if (amount == 0) {
    return "0.00";
  }
  return amount;
};

/**
 * formatterDate
 *
 * @param {*} row 单元格数据
 * @param {*} column 列数据
 * @returns {string} val 解析后的结果
 */
const formatterDate = (row, column) => {
  const property = column.property;
  const time = row[property];
  if (!time) return "--";
  let date =
    time.length < 13 ? new Date(Number(time) * 1000) : new Date(Number(time));
  let Y = date.getFullYear() + "-";
  let M =
    (date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1) + "-";
  let D = panLeft(date.getDate()) + " ";
  let h = panLeft(date.getHours()) + ":";
  let m = panLeft(date.getMinutes()) + ":";
  let s = panLeft(date.getSeconds());
  return Y + M + D + h + m + s;
};

/**
 * formatterDateOnly
 *
 * @param {*} row 单元格数据
 * @param {*} column 列数据
 * @returns {string} val 解析后的结果
 */
const formatterDateOnly = (row, column) => {
  const property = column.property;
  const time = row[property];
  if (!time) return "-";
  let date = new Date(Number(time));
  let Y = date.getFullYear() + "-";
  let M =
    (date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1) + "-";
  let D = panLeft(date.getDate()) + " ";
  return Y + M + D;
};

function panLeft(num) {
  return num < 10 ? "0" + num : num;
}

/**
 * 通过id从数组中查找
 *
 * @param {array} list 数组
 * @param {string} key 需要查找的key
 * @param {string} val 需要查找的val
 * @returns {object} data 找到的数据
 */
const find = (list, key, val) => {
  let index = -1;
  let data = null;
  list.forEach((item, i) => {
    if (item[key] === val) {
      index = i;
      data = JSON.parse(JSON.stringify(item));
      return;
    }
  });
  return {
    index,
    data,
  };
};

/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 * @param {*} rootId 根Id 默认 0
 */
function handleTree(data, id, parentId, children, rootId) {
  id = id || "id";
  parentId = parentId || "parentId";
  children = children || "children";
  rootId =
    rootId ||
    Math.min.apply(
      Math,
      data.map((item) => {
        return item[parentId];
      })
    ) ||
    null;
  //对源数据深度克隆
  const cloneData = JSON.parse(JSON.stringify(data));
  //循环所有项
  const treeData = cloneData.filter((father) => {
    let branchArr = cloneData.filter((child) => {
      //返回每一项的子级数组
      console.log(child[parentId]);
      return father[id] === child[parentId];
    });
    branchArr.length > 0 ? (father.children = branchArr) : "";
    //返回第一层
    return father[parentId] === rootId;
  });
  console.log(treeData);
  return treeData != "" ? treeData : data;
}

export {
  formatter,
  formatterAmount,
  formatterDate,
  formatterDateOnly,
  find,
  handleTree,
};
