import { ContentConfig, BeforeRequestType } from "/@/components/PageContent";
import { createHttpRequset } from "../../../_http/createRequset";
import { ElMessage } from "element-plus";
import { isBeyondTime } from "../../../_utils";

const columns = [
  {
    prop: "addtime",
    label: "竞价时间",
    minWidth: "155px"
  },
  {
    prop: "name",
    label: "业务部门",
    minWidth: "155px"
  },
  {
    prop: "total",
    label: "竞价数量",
    minWidth: "155px"
  }
];

//获取要合并的行数
function getSpanNumber(data, prop) {
  //data要处理的数组，prop要合并的属性，比如name
  //数组的长度，有时候后台可能返回个null而不是[]
  const length = Array.isArray(data) ? data.length : 0;
  if (length > 0) {
    //用于标识位置
    let position = 0;
    //用于对比的数据
    let temp = data[0][prop];
    //要返回的结果
    const result = [1];
    //假设数据是AABCC，我们的目标就是返回20120
    for (let i = 1; i < length; i++) {
      if (data[i][prop] == temp) {
        //标识位置的数据加一
        result[position] += 1;
        //当前位置添0
        result[i] = 0;
      } else {
        //不相同时，修改标识位置，该位置设为1，修改对比值
        position = i;
        result[i] = 1;
        temp = data[i][prop];
      }
    }
    //返回结果
    return result;
  } else {
    return [0];
  }
}

const contentConfig: ContentConfig = {
  columns,
  root: true,
  notPagination: true,
  superUserNoAction: false,
  notNeedCompanyFilter: true,
  spanMethod({ rowIndex, columnIndex }, tableData) {
    if (columnIndex == 0) {
      //合并相同的名字
      const nameSpan = getSpanNumber(tableData, "addtime");
      return {
        rowspan: nameSpan[rowIndex],
        colspan: 1
      };
    }
  },
  beforeRequestList(params: Record<string, string>) {
    const { start_date, end_date } = params;
    if (!start_date || !end_date) {
      ElMessage.warning("请选择一个时间区间 ");
      return BeforeRequestType.EMPTY;
    }

    const beyondTime = isBeyondTime({
      message: "时间区间不能超过 30 天",
      start: start_date,
      end: end_date,
      len: 30
    });

    return beyondTime ? BeforeRequestType.EMPTY : BeforeRequestType.NEXT;
  },
  apis: {
    httpList: createHttpRequset("reportzixuntotal")
  }
};

export default contentConfig;
