import { reactive, ref, watch } from 'vue'
import { useStore } from '/@/store/index';


export default function () {
  const statusParams = reactive({
    status: 1
  })

  return { statusParams }
}

export function useSearch<T>(api: any, resKey: string, expandParams?: any) {

  //  <pagination v-if="params.total" :total="params.total" v-model:page="params.pageNum" v-model:limit="params.pageSize" @pagination="getList()" />

  // import api from '/@/api/system';
  // import { ApiRow } from '/@/api/model/system/menu';
  // import { useSearch } from '/@/hooks/useCommon';

  // const { params, tableData, getList } = useSearch<ApiRow[]>(api.api.getList, 'Info', { name: '', address: '' });
  // getList() // 获取列表数据

  interface SearchParams {
    status: -1 | 0 | 1,
    pageNum: number;
    pageSize: number;
    total: number;
    [key: string]: any;
  }

  const params = reactive<SearchParams>({
    status: -1,
    pageNum: 1,
    pageSize: 20,
    total: 0,
    ...expandParams
  })

  const loading = ref(false)

  const tableData = ref<T[] | any>([])

  const getList = async (pageNum?: number) => {
    typeof pageNum === 'number' && (params.pageNum = pageNum);
    tableData.value = [];
    loading.value = true;
    params.total = 0;
    const res = await api(params).finally(() => loading.value = false)
    // console.log(res)
    tableData.value = (resKey ? (res[resKey]) : (res)) || [];
    params.total = res.total;
  };

  const resetQuery = () => {
    Object.assign(params, { ...expandParams })
    getList(1)
  };

  return { params, tableData, getList, loading, resetQuery }
}

export function useThemeChange(configArr: any[]) {
  const store = useStore();

  watch(() => store.state.themeConfig.themeConfig.isIsDark, (isIsDark) => {
    configArr.forEach(config => {
      config.value.theme = isIsDark ? 'hack' : ''
    })
  });
}

export function useThemeChangeFunc(func: Function) {
  const store = useStore();

  watch(() => store.state.themeConfig.themeConfig.isIsDark, (isIsDark) => {
    func(isIsDark)
  });
}
