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

export function initMG(): Ermcp3MiddleGoodsDetailEx {
    return {
        dglist: [],  // 关联的现货品种列表
        gplist: [],  // 关联商品组列表
        mg: {
            areauserid: 0,
            evaluateratio: 0,
            goodsgroupid: 0,
            isvalid: 0,
            middlegoodsid: 0,
            needhedgeratio: 0,
            qtydecimalplace: 0,
            relatedgoodsid: 0,
            relatedgoodstype: 0,
            goodsunitid: 0,
            createtime: '',
            enumdicname: '',
            middlegoodscode: '',
            middlegoodsname: '',
            modifytime: '',
            remark: '',
        }
    }
}

/**
 * 获取套保品种数据
 * @returns 
 */
export function handleMG() {
    const { menuList, menuMap } = getInitMenuData('goods_info_hedge')
    const loading = ref<boolean>(false);
    // 套保品种列表
    const MGList = ref<Ermcp3MiddleGoodsDetailEx[]>([])
    // 选中的具体某一天套保品种数据
    const selctedMG = reactive<Ermcp3MiddleGoodsDetailEx>(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 { isvalid, middlegoodsname, middlegoodsid } = e.mg
            const normal = 'goods_info_hedge_normal', disable = 'goods_info_hedge_disable';
            const result = { key: middlegoodsid.toString(), title: middlegoodsname }
            if (isvalid) { //1-有效(正常)
                if (menuMap.has(normal)) {
                    const index = menuMap.get(normal) as number;
                    menuList.value[index].children?.push(result)
                }
            } else {    // 0:无效(停用)
                if (menuMap.has(disable)) {
                    const index = menuMap.get(disable) 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.mg.middlegoodsid === Number(key))
        if (obj) {
            mergeObj(selctedMG, obj)
        }
    }
    /**
 * 更新数据
 */
    function updateDG(param?: number) {
        getMG(param).then(() => {
            classfiyMG()
            const findResult = MGList.value.find(e => e.mg.middlegoodsid === selctedMG.mg.middlegoodsid)
            findResult && mergeObj(selctedMG, findResult)
        })
    }
    return { loading, menuList, isNormal, selctedMG, queryMG, chooseDG, updateDG }
}