import { QueryGoodsfGroup } from '@/services/go/ermcp/goodsInfo';
import { Ermcp3GoodsGroup } from '@/services/go/ermcp/goodsInfo/interface';
import { message } from 'ant-design-vue';
import { reactive, ref, UnwrapRef } from 'vue';
import { FormState } from './interface';

/**
 * 处理表单数据
 * @returns 
 */
export function handleFromState() {
    const formState: UnwrapRef<FormState> = reactive({
        deliverygoodscode: '', // string 交割商品代码(新增时有值)
        deliverygoodsname: '', // string 交割商品名称(新增时有值) // 可能没值  有值不能为中文
        deliverygoodsid: 0,// uint64 交割商品id(修改时有值)
        unitid: undefined, // uint64 单位ID
        type: 1,// int32 类型 1 新增 2 修改
        remark: '', // string 备注
        gldwrstandards: [
            { wrstandardname: '', unitid: undefined, convertfactor: null, }
        ], //        GLDWRStandardEx 现货商品型号数据
        glddgfactoryItems: [
            { dgfactoryitemvalue: '', }
        ], //   GLDDGFactoryItemEx 现货商品品牌数据
        wrsconvertdetails: [
            { middlegoodsid: undefined, unitid: null, unitidName: '', convertratio: null, }
        ], // WRSConvertDetailEx 现货商品折算配置明细数据
    });
    type Key = 'gldwrstandards' | 'glddgfactoryItems' | 'wrsconvertdetails'
    /**
     *  向动态表单里添加一条数据
     * @param key 'gldwrstandards' | 'glddgfactoryItems' | 'wrsconvertdetails'
     */
    function addOne(key: Key): void {
        if (key === 'gldwrstandards') {
            formState[key].push({ wrstandardname: '', unitid: undefined, convertfactor: null })
        } else if (key === 'glddgfactoryItems') {
            formState[key].push({ dgfactoryitemvalue: '', })
        } else if (key === 'wrsconvertdetails') {
            formState[key].push({ middlegoodsid: undefined, unitid: null, unitidName: '', convertratio: null, })
        }
    }
    /**
     * 删除动态表单一条数据
     * @param key 
     * @param i 
     */
    function deleteOne(key: Key, i: number): void {
        formState[key].splice(i, 1)
    }
    return { formState, addOne, deleteOne }
}

/**
 * 获取期货商品组
 * @returns 
 */
export function handleGoodsGroup() {
    const goodsGroup = ref<Ermcp3GoodsGroup[]>([])
    function getGoodsGroup() {
        QueryGoodsfGroup().then(res => {
            console.log('获取期货商品组', res);
            goodsGroup.value = res.filter(e => e.goodsgroupstatus === 1);
        }).catch(err => {
            message.error(err)
        })
    }
    return { goodsGroup, getGoodsGroup }
}

