import { computed, ref } from 'vue';
import { useVentStore } from '/@/store/modules/vent';
import { BasicColumn } from '/@/components/Table';

const ventStore = useVentStore();
const webColumnsKey = ref('');

const arrToColumns = (tableHeaderColumns = []) => {
  // debugger
  const columns: BasicColumn[] = [];
  tableHeaderColumns.forEach((item: any) => {
    const columnsItem = {
      title: item.unit ? `${item.des}(${item.unit})` : item.des, //_dictText
      dataIndex: item.dict ? `${item.monitorcode}_dictText` : item.monitorcode,
      width: item.width || 100,
      defaultHidden: !item.showflag,
      sorter: item.sort ? true : false,
      customRender: ({ text }) => {
        return text ? text : '/';
      },
    };
    columns.push(columnsItem);
  });
  return columns;
};

export const setWebColumnsKey = (key: string) => {
  webColumnsKey.value = key;
};

export const getTableHeaderColumns = computed(() => {
  const allTableHeaderColumnArr = ventStore.getAllTableHeaderColumnsApi;
  const tabelHeaderColumns = allTableHeaderColumnArr[webColumnsKey.value];
  return arrToColumns(tabelHeaderColumns);
});
