import { ref, shallowRef, computed } from 'vue'
import { useDataTable } from '@/hooks/datatable'
import { getTableColumns } from '@/business/table'
import { searchSellOrder } from '@/services/api/trade'
import {
    Category,
    getCurrencyTypeList,
    getShapeTypeList,
    getColorTypeList,
    getCutTypeList,
    getClarityTypeList,
    getPolishTypeList,
    getSymmetryTypeList,
    getFluorescenceTypeList,
    getCertTypeList,
    getStyleTypeList,
} from '@/constants/diamond'

export function useSearch(category: Category) {
    const { dataList, total, pageIndex, pageSize } = useDataTable<Ermcp.SellOrderSearchRsp & { checked: boolean }>()
    const loading = shallowRef(false)
    const columns = shallowRef(getTableColumns('listing_sellorder'))
    const warehouseList = shallowRef<Ermcp.WarehouseInfoRsp[]>([]) // 仓库列表

    // 部分请求参数类型重定义
    type SellOrderSearchReq = Omit<Ermcp.SellOrderSearchReq, 'zsshapetype' | 'zscurrencytype' | 'zscerttype' | 'zsstyletype'> & {
        zsshapetype?: number[],
        zscurrencytype?: number[],
        zscerttype?: number[],
        zsstyletype?: number[],
    }

    const formData = ref<SellOrderSearchReq>({
        zscategory: category,
    })

    const enums = {
        currencyTypeList: getCurrencyTypeList(),
        shapeTypeList: getShapeTypeList(),
        colorTypeList: getColorTypeList(),
        cutTypeList: getCutTypeList(),
        clarityTypeList: getClarityTypeList(),
        polishTypeList: getPolishTypeList(),
        symmetryTypeList: getSymmetryTypeList(),
        fluorescenceTypeList: getFluorescenceTypeList(),
        certTypeList: getCertTypeList(),
        styleTypeList: getStyleTypeList(),
    }

    // 记录上次选中的列表项索引
    let prevIndex = -1

    // 选中的列表项
    const checkedItems = computed(() => dataList.value.filter((e) => e.checked))
    // 选择列表项
    const selectItem = (index: number) => {
        const selection = checkedItems.value
        if (selection.length) {
            // 最多只能选择4项
            if (selection.length > 4) {
                dataList.value[prevIndex].checked = false
            }
            prevIndex = index
        } else {
            prevIndex = -1
        }
    }

    // 获取出售大厅委托单
    const getSellOrderList = () => {
        const { zsshapetype, zscurrencytype, zscerttype, warehouseid, zsstyletype, ...param } = formData.value
        loading.value = true
        return searchSellOrder({
            data: {
                ...param,
                page: pageIndex.value,
                pagesize: pageSize.value,
                warehouseid: warehouseid || undefined,
                zsshapetype: zsshapetype?.map((val) => val.toString()),
                zscurrencytype: zscurrencytype?.map((val) => val.toString()),
                zscerttype: zscerttype?.map((val) => val.toString()),
                zsstyletype: zsstyletype?.map((val) => val.toString()),
            },
            success: (res) => {
                total.value = res.total
                dataList.value = res.data.map((e) => ({ ...e, checked: false }))
            },
            complete: () => {
                loading.value = false
            }
        })
    }

    const onSearch = () => {
        pageIndex.value = 1
        getSellOrderList()
    }

    const onReset = () => {
        pageIndex.value = 1
        formData.value = {
            zscategory: category
        }
        getSellOrderList()
    }

    // 获取仓库列表
    // queryWarehouseInfo({
    //     data: {
    //         userid: getUserId(),
    //         status: 1,
    //         isincludeexchange: true,
    //     },
    //     success: (res) => {
    //         warehouseList.value = res.data
    //     }
    // })

    return {
        loading,
        warehouseList,
        formData,
        enums,
        dataList,
        checkedItems,
        columns,
        total,
        pageIndex,
        pageSize,
        selectItem,
        getSellOrderList,
        onSearch,
        onReset,
    }
}