import { QueryMiddleGoodsDetail } from '@/services/go/ermcp/goodsInfo';
import { Ermcp3MiddleGoodsDetail } from '@/services/go/ermcp/goodsInfo/interface/middlegoodsdetail';
import { mergeObj } from '@/utils/objHandle';
import { message } from 'ant-design-vue';
import { reactive, ref } from 'vue';
import { getInitMenuData } from '../../setup';


export function initMG(): Ermcp3MiddleGoodsDetail {
    return {
        areauserid: 0, //机构用户ID
        enumdicname: '',//单位名称
        goodsunitid: 0,//套保品种单位id
        GPLST: [],//期货品种列表
        middlegoodscode: '',//套保品种代码
        middlegoodsid: 0,//套保品种ID(SEQ_ERMS_MIDDLEGOODS)
        middlegoodsname: '',//套保品种名称
        needarbitrageratio: 0,//套利比率
        needhedgeratio: 0,//套保比率
        WDLST: [],//商品信息列表(现货品类)
    }
}

/**
 * 获取套保品种数据
 * @returns 
 */
export function handleMG() {
    const { menuList, menuMap } = getInitMenuData('goods_info_hedge')
    const loading = ref<boolean>(false);
    // 套保品种列表
    const MGList = ref<Ermcp3MiddleGoodsDetail[]>([])
    // 选中的具体某一天套保品种数据
    const selctedMG = reactive<Ermcp3MiddleGoodsDetail>(initMG())
    const isNormal = ref<boolean>(true) // true => 正常； false => 停用
    function getMG(param?: number) {
        loading.value = true;
        return QueryMiddleGoodsDetail(param).then(res => {
            MGList.value.length = 0;
            MGList.value = res;
        }).catch(err => {
            message.error(err)
        }).finally(() => loading.value = false)
    }
    // 获取套保品种数据
    async function queryMG(param?: number) {
        await getMG(param)
        classfiyMG()
        // 获取默认第一条数据
        getFirstData()
    }
    function classfiyMG() {
        menuList.value.forEach(el => {
            if (el.children) {
                el.children.length = 0
            }
        });
        MGList.value.forEach(e => {
            const { middlegoodsname, middlegoodsid } = e
            const normal = 'goods_info_hedge_normal';
            const result = { key: middlegoodsid.toString(), title: middlegoodsname }

            if (menuMap.has(normal)) {
                const index = menuMap.get(normal) as number;
                menuList.value[index].children?.push(result)
            }
        })
    }
    function getFirstData() {
        // 获取默认第一条数据
        if (menuList.value[0].children?.length) {
            chooseDG({ key: menuList.value[0].children[0].key, is: true })
        } else if (menuList.value[1].children?.length) {
            chooseDG({ key: menuList.value[1].children[0].key, is: false })
        }
    }
    interface Value {
        key: string;
        is: boolean;
    }
    // 选择某条现货数据
    function chooseDG({ key, is }: Value) {
        isNormal.value = is
        const obj = MGList.value.find(e => e.middlegoodsid === Number(key))
        if (obj) {
            mergeObj(selctedMG, obj)
        }
    }
    /**
 * 更新数据
 */
    function updateDG(param?: number) {
        getMG(param).then(() => {
            classfiyMG()
            const findResult = MGList.value.find(e => e.middlegoodsid === selctedMG.middlegoodsid)
            findResult && mergeObj(selctedMG, findResult)
        })
    }
    return { loading, menuList, isNormal, selctedMG, queryMG, chooseDG, updateDG }
}