<template>
    <global-page-swiper
        title="信息通报"
        isDataList
        refresherEnabled
        loadingMoreEnabled
        auto
        ref="paging"
        :params="searchData"
        :api="getMocinfoListApi"
        :padding="24"
        :transformData="transformData"
        :tabList="tabList"
        tabValueField="state"
        tabsWidth="50%"
        showNavRight
        navRightType="icon"
        navRightIcon="saixuan-01"
        @handleRightClick="handleRightClick"
    >
        <template #="{ item }">
            <view
                @tap="goDetails(item)"
                class="item"
            >
                <view class="item-title">
                    <view class="left">
                        <custom-state :value="item.status?.toUpperCase()" />
                    </view>
                    <view class="right inforDisTags">
                        <global-tag
                            v-for="tagItem of item.tags"
                            :key="tagItem.text"
                            :type="tagItem.type" class="txt">
                            {{ tagItem.text }}
                        </global-tag>
                    </view>
                </view>
                <view class="item-content u-line-3">
                    <view class="inforDis">
                        <text v-if="item.machineNumber">机号：{{ item.machineNumber }}</text>
                        <text v-if="item.ATA">ATA：{{ item.ata }}</text>
                    </view>
                    <text>{{ item.faultDescription || '无' }}</text>
                </view>
                <view class="item-footer inforDisFooter">
                    <view class="label">
                        <global-icon icon="calendar" />
                        <text class="txt">
                            {{ item.time }}
                        </text>
                    </view>
                    <view class="label right">
                        <global-icon icon="idcard" />
                        <text class="txt">
                            值班人员：{{ item.filledBy }}
                        </text>
                    </view>
                </view>
            </view>
        </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'


//跳转
const inforDisclosureStore = useInforDisclosureStore()
const {searchData} = storeToRefs(inforDisclosureStore)

const tabList = [
    {
        name: '当前通报',
        value: 1
    },
    {
        name: '历史通报',
        value: 2
    }
]

const goDetails = (data) => {
    uni.$mocpJump.navigateTo('/panel/inforDisclosure/details', {id: data.id}).then(() => {
        inforDisclosureStore.setState('details', data)
    })
}

const transformData = (data) => {
    const GuaranteeStageList = getParamsByType('GuaranteeStage')
    const OperationPhaseList = getParamsByType('OperationPhase')
    const ReleaseStatuList = getParamsByType('ReleaseStatu')
    const FaultReportingList = getParamsByType('FaultReporting')
    const MajorCategoryList = getParamsByType('MajorCategory')

    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: item.failureState.toString() === '0' ?
                'open' : item.failureState.toString() === '1' ?
                    'close' : '作废',
            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 paging = ref()
uni.$on('inforDisclosureReload', () => {
    paging.value?.reload()
})
onUnload(() => {
    uni.$off('inforDisclosureReload')
})
</script>
<style lang="scss" scoped>
@import 'mocpStatic/css/list.scss';
@import './constants/inforDisclosure.scss';
</style>
