<template>
    <global-page-swiper
        title="信息通报"
        isDataList
        refresherEnabled
        loadingMoreEnabled
        auto
        ref="paging"
        :params="searchData"
        :api="getMocinfoListApi"
        :padding="24"
        :transformData="transformData"
        :tabList="tabList"
        tabValueField="event_status"
        tabsWidth="50%"
        showNavRight
        navRightType="icon"
        navRightIcon="saixuan-01"
        @handleRightClick="handleRightClick"
        @handleTabsChange="handleTabsChange"
    >
        <template #="{ item }">
            <inforDisclosureItem :item="item" />
        </template>

    </global-page-swiper>
</template>

<script setup>
import { ref } from 'vue'
import useInforDisclosureStore from 'mocp/store/inforDisclosure'
import { storeToRefs } from 'pinia'
import { onUnload } from '@dcloudio/uni-app'
import { getMocinfoListApi } from 'mocp/api/inforDisclosure'
import { getParamsByType } from 'mocp/hooks/use-params/useParams'
import inforDisclosureItem from './components/inforDisClosureItem.vue'

const inforDisclosureStore = useInforDisclosureStore()
const {searchData, currentTabsIndex} = storeToRefs(inforDisclosureStore)
const tabList = [
    {
        name: '当前通报',
        value: '1,2'
    },
    {
        name: '历史通报',
        value: '3,4,5'
    }
]

const transformData = (data) => {
    const GuaranteeStageList = getParamsByType('GuaranteeStage')
    const OperationPhaseList = getParamsByType('OperationPhase')
    const ReleaseStatuList = getParamsByType('ReleaseStatu')
    const FaultReportingList = getParamsByType('FaultReporting')
    const MajorCategoryList = getParamsByType('MajorCategory')
    const EventStatusList = getParamsByType('EventStatus')
    console.log('EventStatusList', EventStatusList)

    data = data.reduce((q, w) => {
        q.push(w)
        if (Array.isArray(w.cascade) && w.cascade.length > 0) {
            q.push(...w.cascade)
        }
        return q
    }, [])
    const mappedData = data.map(item => {
        return {
            ...item,
            time: item.creationTime.replace(/-/g, '/').replace(/:\d+?$/, ''),
            status: EventStatusList.find(statusItem => statusItem.value == item.eventStatus)?.label,
            tags: [
                {
                    text: item.pid === '0' ? '主项' : '子项',
                    type: item.pid === '0' ? 'primary' : 'warning'
                },
                {
                    text: GuaranteeStageList[item.guarantee * 1]?.label,
                    type: 'primary'
                },
                {
                    text: OperationPhaseList[item.functionInfo * 1]?.label,
                    type: 'primary'
                },
                {
                    text: ReleaseStatuList[item.rleaseState]?.label,
                    type: 'info'
                }
            ],
            reporterValue: FaultReportingList[Number(item.reporter)]?.label,
            professionalValue: MajorCategoryList[Number(item.professional)]?.label
        }
    })
    console.log('@@mappedData', mappedData)
    return mappedData
}

//筛选
const handleRightClick = () => {
    uni.$mocpJump.redirectTo('/panel/inforDisclosure/search')
}
const handleTabsChange = (e) => {
    console.log(e)
    currentTabsIndex.value = e
}
//刷新
const paging = ref()
uni.$on('inforDisclosureReload', () => {
    paging.value?.reload()
})
onUnload(() => {
    uni.$off('inforDisclosureReload')
})
</script>
<style lang="scss" scoped></style>
