import { ref } from 'vue'
import { Ermcp3AreaSpot, Ermcp3AreaSpotDetail } from '@/services/go/ermcp/exposure/interface';
import { getContractTypeName } from '@/common/constants/enumsName';
import { ColumnType } from '@/common/methods/table';
import { FilterColumn } from '@/common/components/filter/interface';

/**
 * 表头对应字段
 */
function getColumns() {
    const columns: ColumnType[] = [
        {
            title: '现货商品',
            key: 'wrstandardname',
        },
        {
            title: '当前数量',
            key: 'totalspotqty',
            customRender: ({ record }: { record: Ermcp3AreaSpot }) => record.totalspotqty + record.enumdicname,
        },
        {
            title: '增加数量',
            key: 'increaseqty',
            customRender: ({ record }: { record: Ermcp3AreaSpot }) => record.increaseqty + record.enumdicname,
        },
        {
            title: '减少数量',
            key: 'decreaseqty',
            customRender: ({ record }: { record: Ermcp3AreaSpot }) => record.decreaseqty + record.enumdicname,
        },
        {
            title: '昨日数量',
            key: 'oritoalspotqty',
            customRender: ({ record }: { record: Ermcp3AreaSpot }) => record.oritoalspotqty + record.enumdicname,
        },
    ];

    return columns.map(el => {
        return {
            dataIndex: el.key,
            width: 100,
            align: 'center',
            ...el
        }
    })
}

/**
 * 明细表头对应字段
 */
function getColumnsDetail() {
    const columns = [
        {
            title: '合同(计划)编号',
            key: 'relatedno',
        },
        {
            title: '类型',
            key: 'contracttype',
            customRender: ({ text }: { text: number }) => getContractTypeName(text),
        },
        {
            title: '现货商品',
            key: 'wrstandardname'
        },
        {
            title: '现货商品数量',
            key: 'qty',
            customRender: ({ record }: { record: Ermcp3AreaSpotDetail }) => record.qty + record.enumdicname
        },
        {
            title: '时间',
            key: 'createtime',
        },
    ];

    return columns.map(el => {
        return {
            dataIndex: el.key,
            width: 100,
            align: 'center',
            ...el
        }
    })
}

export function useColumn() {
    const columns = ref(getColumns());
    const columnsDetail = ref(getColumnsDetail());

    const updateColumn = (filters: FilterColumn[]) => {
        columns.value.forEach((item) => {
            const f = filters.find((e) => e.key === item.key);
            if (f) {
                item.filteredValue = f.filteredValue;
                item.onFilter = f.onFilter;
            } else {
                item.filteredValue = [];
            }
        })
    }

    return {
        columns,
        columnsDetail,
        updateColumn,
    }
}