import { ref } from "vue";

export function useFinanceTables() {
  // 投决管理
  let jcTjjhData = ref([]);
  let tjglColumns = ref([]);
  const importTjjh = (data, file) => {
    if (data?.length > 3) {
      tjglColumns.value = [
        {
          label: data[0][0],
          key: data[0][0],
          children: [
            { label: data[1][0], key: data[1][0] },
            { label: data[1][1], key: data[1][1] },
            {
              label: data[1][2],
              key: data[1][2],
              children: data[2].slice(2).map((item) => ({
                label: item,
                key: item,
              })),
            },
          ],
        },
      ];
      let tableData = data.slice(3);
      let columnKeys = data.slice(2, 3)[0];
      jcTjjhData.value = tableData.map((item) => {
        let obj = {};
        item.map((child, index) => {
          obj[columnKeys[index]] = child;
        });
        return obj;
      });
    }
  };

  // 项目投资资金流量表
  let xmtzzjllData = ref([]);
  let xmtzzjllColumns = ref([]);
  const importXmtzzjll = (data, file) => {
    if (data?.length > 3) {
      xmtzzjllColumns.value = [
        {
          label: data[0][0],
          key: data[0][0],
          children: [
            { label: data[1][0], key: data[1][0] },
            { label: data[1][1], key: data[1][1] },
            {
              label: data[1][2],
              key: data[1][2],
              children: data[2].slice(2).map((item) => ({
                label: item,
                key: item,
              })),
            },
          ],
        },
      ];
      let tableData = data.slice(3);
      let columnKeys = data.slice(2, 3)[0];
      xmtzzjllData.value = tableData.map((item) => {
        let obj = {};
        item.map((child, index) => {
          obj[columnKeys[index]] = child;
        });
        return obj;
      });
    }
  };

  // 项目资本金现金流量表
  let xmzbjxjllData = ref([]);
  let xmzbjxjllColumns = ref([]);
  const importXmzbjxjll = (data, file) => {
    if (data?.length > 3) {
      xmzbjxjllColumns.value = [
        {
          label: data[0][0],
          key: data[0][0],
          children: [
            { label: data[1][0], key: data[1][0] },
            { label: data[1][1], key: data[1][1] },
            {
              label: data[1][2],
              key: data[1][2],
              children: data[2].slice(2).map((item) => ({
                label: item,
                key: item,
              })),
            },
          ],
        },
      ];
      let tableData = data.slice(3);
      let columnKeys = data.slice(2, 3)[0];
      xmzbjxjllData.value = tableData.map((item) => {
        let obj = {};
        item.map((child, index) => {
          obj[columnKeys[index]] = child;
        });
        return obj;
      });
    }
  };

  // 能建方现金流量表
  let njfxjllData = ref([]);
  let njfxjllColumns = ref([]);
  const importNjfxjll = (data, file) => {
    if (data?.length > 3) {
      njfxjllColumns.value = [
        {
          label: data[0][0],
          key: data[0][0],
          children: [
            { label: data[1][0], key: data[1][0] },
            { label: data[1][1], key: data[1][1] },
            {
              label: data[1][2],
              key: data[1][2],
              children: data[2].slice(2).map((item) => ({
                label: item,
                key: item,
              })),
            },
          ],
        },
      ];
      let tableData = data.slice(3);
      let columnKeys = data.slice(2, 3)[0];
      njfxjllData.value = tableData.map((item) => {
        let obj = {};
        item.map((child, index) => {
          obj[columnKeys[index]] = child;
        });
        return obj;
      });
    }
  };

  // 利润表
  let lrbData = ref([]);
  let lrbColumns = ref([]);
  const importLrb = (data, file) => {
    if (data?.length > 3) {
      lrbColumns.value = [
        {
          label: data[0][0],
          key: data[0][0],
          children: [
            { label: data[1][0], key: data[1][0] },
            { label: data[1][1], key: data[1][1] },
            {
              label: data[1][2],
              key: data[1][2],
              children: data[2].slice(2).map((item) => ({
                label: item,
                key: item,
              })),
            },
          ],
        },
      ];
      let tableData = data.slice(3);
      let columnKeys = data.slice(2, 3)[0];
      lrbData.value = tableData.map((item) => {
        let obj = {};
        item.map((child, index) => {
          obj[columnKeys[index]] = child;
        });
        return obj;
      });
    }
  };

  // 获取动态表格数据（导出用）
  const formatDynamicTableData = (columns, data) => {
    if (!columns.value.length) return [];
    let row1 = [];
    let row2 = [];
    let row3 = [];
    columns.value[0].children.map((column) => {
      row1.push(columns.value[0].label);
      row2.push(column.label);
      if (column.children) {
        column.children.map((item) => {
          row1.push(columns.value[0].label);
          row2.push(column.label);
          row3.push(item.label);
        });
      } else {
        row3.push(column.label);
      }
    });
    let rowData = [];
    data.value.map((d) => {
      let arr = [];
      row3.map((item) => {
        arr.push(d[item]);
      });
      rowData.push(arr);
    });
    return [row1, row2, row3, ...rowData];
  };

  const getTjjhData = () => formatDynamicTableData(tjglColumns, jcTjjhData);
  const getXmtzzjllData = () => formatDynamicTableData(xmtzzjllColumns, xmtzzjllData);
  const getXmzbjxjllData = () => formatDynamicTableData(xmzbjxjllColumns, xmzbjxjllData);
  const getNjfxjllData = () => formatDynamicTableData(njfxjllColumns, njfxjllData);
  const getLrbData = () => formatDynamicTableData(lrbColumns, lrbData);

  return {
    jcTjjhData, tjglColumns, importTjjh,
    xmtzzjllData, xmtzzjllColumns, importXmtzzjll,
    xmzbjxjllData, xmzbjxjllColumns, importXmzbjxjll,
    njfxjllData, njfxjllColumns, importNjfxjll,
    lrbData, lrbColumns, importLrb,
    getTjjhData, getXmtzzjllData, getXmzbjxjllData, getNjfxjllData, getLrbData,
  };
}
