import APP from '@/services';
import { QueryDeliveryGoodsDetail } from '@/services/go/ermcp/goodsInfo';
import { ErmcpDeliveryGoodsDetailEx, MenuList } from '@/services/go/ermcp/goodsInfo/interface';
import { mergeObj } from '@/utils/objHandle';
import { reactive, Ref, ref } from 'vue';
import { getInitMenuData } from '../../setup';

/**
 * 初始化 现货品种对象数据
 * @returns
 */
export function initDG(): ErmcpDeliveryGoodsDetailEx {
    return {
        data: {
            agreeunit: 0,
            areauserid: 0,
            auditflag: 0,
            categoryid: 0,
            deliverygoodscode: '',
            deliverygoodsid: 0,
            deliverygoodsname: '--',
            deliverygoodstype: 0,
            dgstatus: 0,
            enumdicname: '--',
            goodsunitid: 0,
            isvalid: 0,
            qtydecimalplace: 0,
            remark: '',
            standardqty: 0,
            standardqtyrange: 0
        },
        gblist: [],
        gmlist: [],
        mgList: [],
    }
}


interface MenuType {
    menuList: Ref<MenuList[]>;
    menuMap: Map<string, number>;
}


/**
 * 获取现货品种数据
 * @returns
 */
export function handleDG() {
    const { menuList, menuMap } = getInitMenuData('goods_info_spot')
    const loading = ref<boolean>(false);
    // 现货品种列表
    const deliveryGoodsList = ref<ErmcpDeliveryGoodsDetailEx[]>([])
    // 选中的具体某一天现货品种数据
    const selctedDeliveryGoods = reactive<ErmcpDeliveryGoodsDetailEx>(initDG())
    const isNormal = ref<boolean>(true) // true => 正常； false => 停用
    function classfiyDG() {
        menuList.value.forEach(el => {
            if (el.children) {
                el.children.length = 0
            }
        });
        const res = APP.get('DeliveryGoodsList') as ErmcpDeliveryGoodsDetailEx[]
        deliveryGoodsList.value = res;
        res.forEach(e => {
            const { dgstatus, isvalid, deliverygoodsname, deliverygoodsid } = e.data
            const normal = 'goods_info_spot_normal', disable = 'goods_info_spot_disable';
            const result = { key: deliverygoodsid.toString(), title: deliverygoodsname }
            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 queryDG() {
        classfiyDG()
        // 获取默认第一条数据
        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 = deliveryGoodsList.value.find(e => e.data.deliverygoodsid === Number(key))
        if (obj) {
            mergeObj(selctedDeliveryGoods, obj)
            console.log('selctedDeliveryGoods', selctedDeliveryGoods);
        }
    }
    /**
     * 更新数据
     */
    function updateDG() {
        QueryDeliveryGoodsDetail().then(() => {
            classfiyDG()
        })
    }
    return { loading, deliveryGoodsList, menuList, queryDG, updateDG, isNormal, selctedDeliveryGoods, chooseDG }
}

