<template>
    <global-page title="航班详情" navbgColor="#0e42d2" navColor="#fff">
        <view class="flightSupportDetails">
            <view class="details_header">
                <view class="header_no flex">
                    <global-airline-image
                        :width="64" :height="64"
                        :code="details.airlineCode">
                        <text class="airlineImg"></text>
                    </global-airline-image>
                    <text>{{ details.ac }}</text>
                </view>

                <view class="header_info flex">
                    <text class="margin-r-32">机型：{{ details.acTypeGc }}</text>
                    <text>机位：{{ details.airlineCode }}</text>
                </view>
            </view>

            <view class="details_swiper">
                <detailsSwiper :filterDetailsList="filterDetailsList" />
            </view>

            <view class="details_tab">
                <view class="tabCont flex">
                    <view
                        v-for="(item, index) of tabList"
                        :key="item"
                        @click="tabCurrent = index"
                        class="f-flex-1 tabItem"
                        :class="index == tabCurrent ? 'active' : ''"
                    >
                        {{ item }}
                    </view>
                </view>
                <scroll-view
                    class="tab-content"
                    :scroll-y="true"
                    :style="{ height: `calc(100vh - 690rpx - ${safeAreaInsets?.top + 'px'} - ${safeAreaInsets?.bottom + 'px'})` }">
                    <detailsFlightInfo v-if="tabCurrent === 0" />

                    <detailsOtherInfo v-else :weatherMsg="weatherMsg" :FcArrangeMsg="FcArrangeMsg" />
                </scroll-view>

            </view>
        </view>
    </global-page>
</template>

<script setup>
import { ref, watchEffect } from 'vue'
import { storeToRefs } from 'pinia'
import useFlightSupportStore from 'mocp/store/flight-support'
import { onLoad } from '@dcloudio/uni-app'
import {
    getFListApi,
    getReleaseListApi,
    findWeatherSynopsesMessagesApi,
    getFcArrangeApi
} from 'mocp/api/flight-support'
import detailsSwiper from '../components/DetailsSwiper.vue'
import detailsFlightInfo from '../components/DetailsFlightInfo.vue'
import detailsOtherInfo from '../components/DetailsOtherInfo.vue'
import { followAcTagAll } from 'mocp/utils/getFlightList'

const {safeAreaInsets} = uni.getSystemInfoSync()
const flightSupportStore = useFlightSupportStore()
const {
    searchData,
    details,
    filterDetailsList,
    filterDetailsCurrentIndex,
    getDetailsCurrentMsg
} = storeToRefs(flightSupportStore)
const tabList = ['航班信息', '其他信息']
const tabCurrent = ref(0)

onLoad(() => {
    getAllFlightsByAc()
})

const getAllFlightsByAc = async () => {
    const params = {
        acs2: details.value.ac,
        // pageSize: 100,
        startT: searchData.value.startT,
        stopT: searchData.value.stopT
    }
    let {list} = await getFListApi(params, {loading: true})
    if (list && list.length) {
        const allReleases = await Promise.all(list.map(a => getReleaseListApi({catchId: a.catchId}, {loading: true})))
        if (allReleases.every(a => Array.isArray(a.data))) {
            allReleases.forEach((q, w) => {
                list[w].releases = q.data
            })
        } else {
            uni.$mocpMessage.error('请求异常')
        }
        list = list.map(item => {
            if (!item.mfollowAcLists || !item.mfollowAcLists.length) {
                return {...item}
            } else {
                const mfollowAcLists = []
                for (let followAcItem of item.mfollowAcLists) {
                    mfollowAcLists.push({
                        ...followAcItem,
                        followStatusValue: followAcTagAll[followAcItem.followStatus]
                    })
                }
                return {
                    ...item,
                    mfollowAcLists
                }
            }
        })
        filterDetailsCurrentIndex.value = list.findIndex(a => a.depStn === details.value.depStn && a.stdChn === details.value.stdChn)
        filterDetailsList.value = list
    }
}

const weatherMsg = ref()
const FcArrangeMsg = ref()
watchEffect(async () => {
    const msg = {
        depStnList: [],
        arrStnList: []
    }
    const depStnRes = await findWeatherSynopsesMessagesApi(
        {airportCode: getDetailsCurrentMsg.value?.depStn},
        {loading: true}
    )
    if (depStnRes.code == '200') msg.depStnList = depStnRes.data[depStnRes.data.length - 1]
    const arrStnRes = await findWeatherSynopsesMessagesApi(
        {airportCode: getDetailsCurrentMsg.value?.arrStn},
        {loading: true}
    )
    if (arrStnRes.code == '200') msg.arrStnList = arrStnRes.data[arrStnRes.data.length - 1]
    weatherMsg.value = msg

    // console.log('getDetailsCurrentMsg', getDetailsCurrentMsg.value)
    const FcArrangeParams = {
        ac: getDetailsCurrentMsg.value?.ac,
        arrStn: getDetailsCurrentMsg.value?.arrStn,
        datopChn: getDetailsCurrentMsg.value?.flightDate,
        depStn: getDetailsCurrentMsg.value?.depStn,
        flightNo: getDetailsCurrentMsg.value?.flightNo,
        stdChn: getDetailsCurrentMsg.value?.stdChn
    }
    const FcRes = await getFcArrangeApi(FcArrangeParams, {loading: true})
    if (FcRes.code == '200' && FcRes.data) {
        const crwPilotInf = FcRes.data.crwPilotInf.replace(/s/g, '').split('；').map(function(item) {
            const nv = item.split(':')
            return {
                uname: nv[0],
                tel: nv[1],
                kind: '机组人员'
            }
        })
        const crwStewardInf = FcRes.data.crwStewardInf.replace(/s/g, '').split('；').map(function(item) {
            const nv = item.split(':')
            return {
                uname: nv[0],
                tel: nv[1],
                kind: '跟机机务'
            }
        })
        FcArrangeMsg.value = {
            crwPilotInf,
            crwStewardInf
        }
    }
    console.log('FcRes', FcArrangeMsg.value)
})

</script>
<style lang="scss" scoped>
@import '../constants/details';
</style>
