import { formatTime } from '@/common/methods';
import { getRangeTime } from '@/utils/time';
import { Moment } from 'moment';
import { isRef, Ref, ref, SetupContext } from 'vue';
import * as type from './interface';
import { InputList, SelectList, SlotParam } from './interface';


export function handleFilter(select: SelectList[] | Ref<SelectList[]>, input: InputList[], context: SetupContext, slot?: type.SlotParam) {
    function getSelectedData(select: SelectList[] | Ref<SelectList[]>) {
        return isRef(select) ? select.value.map(e => {
            const fn = e.change;
            if (fn) {
                e.change = (value: any) => {
                    fn(value.key);
                    search()
                }
            } else {
                e.change = search
            }

            return e
        }) : select.map(e => {
            e.change = search
            return e
        })
    }
    const selectList = ref<type.SelectList[]>(getSelectedData(select));
    const inputList = ref<type.InputList[]>(input);

    const fixedBtnList = ref<type.FixedBtnList[]>([
        { lable: '查询', event: search },
        { lable: '重置', event: reset },
    ]);
    function search() {
        const result: type.Obj = {};
        selectList.value.forEach((e) => {
            const { value, key } = e;
            const cache = value ? (value).value : '';
            result[key] = [cache];
        });
        inputList.value.forEach((e) => {
            const { value, key } = e;
            const cache = value ? value : '';
            result[key] = [cache];
        });
        slot?.search?.(result)
        context.emit('search', result);
    }
    function reset() {
        selectList.value.forEach((e) => (e.value = undefined));
        inputList.value.forEach((e) => (e.value = ''));
        slot?.reset?.()
        search();
    }
    function updateSelected(select: SelectList[] | Ref<SelectList[]>,) {
        selectList.value = getSelectedData(select)
    }
    return { selectList, inputList, fixedBtnList, updateSelected };
}

export type { InputList, SelectList };

/**
 * 时间过滤
 * @param key 时间过滤对应的key
 * @returns 
 */
export function handleTimeFilter(key: string) {
    // 范围时间内过滤
    function getFilterRangeTime(date: Ref<Moment[]>): SlotParam {
        return {
            search(result: any) {
                // 处理范围时间内搜索
                const time = date.value;
                const temp = time.length > 1 ? getRangeTime(time[0], time[1]) : [''];
                result[key] = temp;
            },
            reset() {
                date.value = [];
            },
        };
    }
    // 具体时间过滤
    function getFilterItemTime(date: Ref<Moment | undefined>) {
        return {
            search(result: any) {
                const time = date.value;
                const temp = time ? [formatTime(time, 'd')] : [''];
                result[key] = temp;
            },
            reset() {
                date.value = undefined;
            },
        };
    }
    return { getFilterRangeTime, getFilterItemTime }
}