import { Column, ColumnType } from '@/common/setup/table';
import { handlerManagerList } from '@/common/setup/user';
import { QuerySpotContract } from '@/services/go/ermcp/spot-contract/index';
import { Ermcp3ContractRsp } from '@/services/go/ermcp/spot-contract/interface';
import { message } from 'ant-design-vue';
import { ref } from 'vue';

// 1-未提交 2-待审核 3-履约中 4-已完成
type EnumType = 1 | 2 | 3 | 4;

/**
 * 获取表格列表数据
 * @param type
 * @returns
 */
export function queryTableList(type: EnumType) {
    // 加载状态
    const loading = ref<boolean>(false);
    // 表格数据
    const tableList = ref<Ermcp3ContractRsp[]>([]);
    function queryTable() {
        QuerySpotContract({ querytype: type })
            .then((res) => {
                tableList.value = res.map((e, i) => {
                    return { ...e, key: String(i) };
                });
                loading.value = false;
                console.log('查询列表', tableList);
            })
            .catch((err) => {
                message.error(err);
                loading.value = false;
            });
    }
    return { loading, tableList, queryTable }
}

// 获取 业务账户
const { tableList, queryTable, findManagerName } = handlerManagerList(ref<boolean>(false), 1);
export { tableList as businessManager, queryTable as queryBusinessManager, findManagerName };

// 表格过滤回调函数，处理特殊字段
export const tableFilterCB = (e: Column, item: ColumnType, filtered: any) => {
    if (e.columntitle === '对手方') {
        item.onFilter = (value: string, record: Ermcp3ContractRsp) => {
            if (record.contracttype === 1) {
                //采购方
                return record.sellusername.includes(value);
            } else {
                // 销售
                return record.buyusername.includes(value);
            }
        };
        item.filteredValue = filtered.negative || null;
    }
};


export function handleEnumdic(text: number | string, record: Ermcp3ContractRsp) {
    return text + record.enumdicname
}
