import { initData } from '@/common/methods'
import { SelectOption } from '@/common/setup/filter/interface'
import APP from '@/services'
import { getUsrId } from '@/services/bus/user'
import { QueryMiddleGoodsDetail } from '@/services/go/ermcp/goodsInfo'
import { Ermcp3Brand, Ermcp3Wrstandard, ErmcpDeliveryGoodsDetailEx } from '@/services/go/ermcp/goodsInfo/interface'
import moment, { Moment } from "moment"
import { ref, SetupContext } from "vue"
import { reportCycleType, reportCyleTimeType } from "./enum"
import { Value } from "./interface"

export function handleReprotType(context: SetupContext) {
    // 选中的周期类型
    const selectedReportType = ref<Value>({ key: reportCycleType.day })
    // 时间格式化类型
    const dateFormat = ref<reportCyleTimeType>(reportCyleTimeType.day)
    // 时间
    const time = ref<Moment>(getInitTime());
    // 交易用户
    const selectedUser = ref<Value>({ key: getUsrId() })
    // 周期类型与时间类型 一对一的关系
    const timeMap = new Map([
        [reportCycleType.day, reportCyleTimeType.day],
        [reportCycleType.month, reportCyleTimeType.month],
        [reportCycleType.year, reportCyleTimeType.year],
        [reportCycleType.week, reportCyleTimeType.week],
    ])
    // 报表类型
    const reportType = ref<{ value: number, lable: string }[]>([
        { value: reportCycleType.day, lable: '日报表' },
        { value: reportCycleType.week, lable: '周报表' },
        { value: reportCycleType.month, lable: '月报表' },
    ])
    // 初始化时间
    function getInitTime() {
        const yesterday = moment().add(-1, 'days')
        return moment(yesterday, dateFormat.value)
    }
    function getFomateType(): reportCyleTimeType {
        const { key } = selectedReportType.value
        const temp = timeMap.get(key)
        return temp ? temp : reportCyleTimeType.day
    }
    // 周期类型变更
    function reportTypeChange() {
        time.value = getInitTime()
        dateFormat.value = getFomateType()
        update()

    }
    // 时间变更
    function timeChange() {
        update()
    }
    // 交易用户变更
    function userChange() {
        update()
    }
    // 根据周期类型 格式化时间
    function formateTime() {
        return moment(time.value).format(getFomateType())
    }
    // 是否为 月报表
    function isMonth() {
        return selectedReportType.value.key === reportCycleType.month
    }
    // 通知上层更新数据
    function update() {
        const param = { cycletype: selectedReportType.value.key, cycletime: formateTime(), userid: selectedUser.value.key }
        context.emit('update', param)
    }
    return { selectedReportType, reportType, isMonth, reportTypeChange, dateFormat, time, timeChange, selectedUser, userChange }
}

export function handleInitTypeAndTime() {
    // 初始化时间
    function getInitTime() {
        const yesterday = moment().add(-1, 'days')
        return moment(yesterday).format(reportCyleTimeType.day)
    }
    // 初始化类型
    function getInitType() {
        return reportCycleType.day
    }
    return { getInitTime, getInitType }
}

// 获取套保品种
export function handlMiddleGoods() {
    const middleGoods = ref<SelectOption[]>([])
    function getTBList() {
        return QueryMiddleGoodsDetail().then((res) => {
            const result: SelectOption[] = [];
            res.forEach((e) => {
                const { isvalid, middlegoodsname, middlegoodsid } = e.mg;
                if (isvalid) {
                    result.push({ value: middlegoodsid, lable: middlegoodsname });
                }
            });
            middleGoods.value = result
        })
    }
    return { middleGoods, getTBList }
}

export function handleDG(context: SetupContext) {
    // 现货品种
    const DGList = ref<ErmcpDeliveryGoodsDetailEx[]>([])
    const selectedDG = ref<Value | undefined>(undefined);
    // 品牌
    const gblist = ref<Ermcp3Brand[]>([])
    const selectedGB = ref<Value | undefined>(undefined);
    // 品类列表
    const gmlist = ref<Ermcp3Wrstandard[]>([])
    const selectedGM = ref<Value | undefined>(undefined);
    function getDG() {
        DGList.value = APP.get('DeliveryGoodsList').filter((e: ErmcpDeliveryGoodsDetailEx) => e.data.isvalid === 1)
    }
    initData(() => {
        getDG()
    })

    function DGChange(value: any) {
        const temp = DGList.value.find((e: ErmcpDeliveryGoodsDetailEx) => e.data.deliverygoodsname === value.key)
        if (temp) {
            gblist.value = temp.gblist
            gmlist.value = temp.gmlist
        }
        filterAction()
    }
    function GBOrGMChange() {
        filterAction()
    }
    function filterAction() {
        const deliverygoodsname = selectedDG.value ? [selectedDG.value.key] : ['']
        const brandname = selectedGB.value ? [selectedGB.value.key] : ['']
        const wrstandardname = selectedGM.value ? [selectedGM.value.key] : ['']
        const result = { deliverygoodsname, brandname, wrstandardname }
        context.emit('filter', result)
    }
    function reset() {
        selectedDG.value = undefined
        selectedGB.value = undefined
        selectedGM.value = undefined
        filterAction()
    }
    return { DGList, gblist, gmlist, selectedDG, selectedGB, selectedGM, DGChange, reset, GBOrGMChange }
}

// 获取品种数据
