<template>
    <view
        v-if="msg?.length"
        @tap="rowClicked(ps.itemMsg)"
        class="tableRow flex"
        :class="ps.itemMsg.isFinished ? 'mocp-bg-fill-4' : ''"
    >
        <view
            v-for="(item, index) of msg"
            :key="index"
            class="rowItem u-line-1 text-center"
            :style="{flex: item.flex}"
        >
            <global-airline-image :width="20" :height="20" class="text-center" v-if="item.keyValue === 'flightNo'"
                                  :code="item.airlineCode">
                {{ item.data }}
            </global-airline-image>

            <RenderTime
                v-else-if="item.keyValue === 'time'"
                :item="ps.itemMsg"
            />

            <view
                v-else-if="item.keyValue == 'status'"
                :class="item.color">
                {{ item.data }}
            </view>

            <text v-else>{{ item.data }}</text>
        </view>
    </view>
</template>

<script setup>
import { computed } from 'vue'
import Day from 'mocp/utils/dayjs'
import useFlightSupportStore from 'mocp/store/flight-support'
import { allInBoundMode, currentInBoundModeForFlightTablePage, decideReleaseStatus } from '../utils/currentInBoundMode'
import RenderTime from '../components/RenderTime.vue'
import { fightListPlanTime } from 'mocp/utils/getFlightList'
import { storeToRefs } from 'pinia'

const flightSupportStore = useFlightSupportStore()
const {details, getSelectedList} = storeToRefs(flightSupportStore)

const ps = defineProps({
    itemMsg: {
        type: Object,
        default: () => {
        }
    }
})

const msg = computed(() => {
    if (!ps.itemMsg) return {}
    const isCurrentIn = currentInBoundModeForFlightTablePage.value == allInBoundMode.In
    const filterListTab = getSelectedList.value.filter(item => !!item.selected)

    return filterListTab.map(item => {
        const defaultObj = {
            keyValue: item.keyValue,
            flex: item.flex,
            data: ps.itemMsg[item.keyValue]
        }
        // 航班号
        if (item.keyValue == 'flightNo') {
            return {
                ...defaultObj,
                airlineCode: ps.itemMsg['airlineCode']
            }
        }
        // 出发地
        if (item.keyValue == 'place') {
            return {
                ...defaultObj,
                data: isCurrentIn ?
                    ps.itemMsg.depStn :
                    ps.itemMsg.arrStn
            }
        }
        // 计划
        if (fightListPlanTime.includes(item.keyValue)) {
            return {
                ...defaultObj,
                data: isCurrentIn ?
                    Day(ps.itemMsg.staChn).format('HH:mm') :
                    Day(ps.itemMsg.stdChn).format('HH:mm')
            }
        }
        // 状态
        if (item.keyValue == 'status') {
            return {
                ...defaultObj,
                ...decideReleaseStatus(ps.itemMsg)
            }
        }
        // 特殊任务
        if (item.keyValue == 'isSpecialTask') {
            return {
                ...defaultObj,
                data: ps.itemMsg.isVip || ps.itemMsg.isHighlandFlights || ps.itemMsg.fltType === 'I' ? '特殊' : ''
            }
        }
        return {
            ...defaultObj
        }
    })
})

const rowClicked = (currentMsg) => {
    console.log('****', currentMsg)
    if (!currentMsg.ac) return uni.$mocpMessage.error('航班已取消')
    details.value = currentMsg
    console.log('@@@@details', details.value)
    uni.$mocpJump.navigateTo('/panel/flight-support/flight-support-details/index')
}
</script>
<style lang="scss" scoped>
@import '../constants/flightSupportList';
</style>
