import { VXETable } from "vxe-table";
import FilterExtend from "../components/FilterExtend.vue";
import { hasValFunc, compareInputFunc, getDefaultFilter, dataCompareFunc } from "./index";

// 创建一个复杂的筛选器
VXETable.renderer.add("FilterExtend", {
  // 不显示底部按钮，使用自定义的按钮
  showFilterFooter: false,
  // 筛选模板
  renderFilter(renderOpts, params) {
    return <FilterExtend params={{ ...params, ...renderOpts.props }}></FilterExtend>;
  },
  // 重置数据方法
  filterResetMethod(params) {
    const { options, column } = params;
    options.forEach(option => {
      option.data = getDefaultFilter(column)[0].data;
    });
  },
  // 筛选数据方法
  filterMethod(params: any) {
    const { option, row, column } = params;
    const cellValue = row[column.field];
    const { vals, f1Type, f1Val, f2Val, f2Type, fMode } = option.data;
    if (cellValue != undefined || cellValue != null) {
      if (vals.length) {
        let _val = isNaN(Number(cellValue)) ? cellValue : cellValue.toString();
        // 通过指定值筛选
        return vals.includes(_val);
        // } else if (f1Type) {
        //   return cellValue.indexOf(f1Val) > -1;
      } else if (option.hasOwnProperty("format") && option.format && f1Val) {
        // 日期
        if (f2Val) {
          return (
            dataCompareFunc(cellValue, f1Val, f1Type, option.format) && dataCompareFunc(cellValue, f2Val, f2Type, option.format)
          );
        } else {
          return dataCompareFunc(cellValue, f1Val, fMode, option.format);
        }
      } else if (f1Val) {
        return compareInputFunc(cellValue, f1Val, fMode);
      }
    }
    return false;
  }
});
