import { ref } from 'vue';

export function useSearchInline(options) {
  const searchVisible = ref(true); // 内联搜索默认显示

  // 处理搜索回调
  const handleFilterBack = (data) => {
    if (data.event === 'confirm' || data.event === 'reset') {
      options.filterParams.tools = data.data.tools;
      options.filterParams.data = data.data.data;
      options.filterParams.conditionLabel = data.data.conditionLabel;
      options.getData(1);
    }
  };

  // 删除单个筛选条件
  const deleteFilter = (key) => {
    if (options.filterParams.tools[key].type == 'tinputprepend') {
      options.filterParams.data[key] =
        options.filterParams.tools[key][options.filterParams.tools[key].field];
    } else {
      options.filterParams.data[key] = options.filterParams.tools[key].value;
    }
    delete options.filterParams.conditionLabel[key];
    options.getData(1);
  };

  // 切换搜索区域显示/隐藏
  const toggleSearch = () => {
    searchVisible.value = !searchVisible.value;
  };

  return {
    searchVisible,
    handleFilterBack,
    deleteFilter,
    toggleSearch,
  };
}
