<template>
    <global-page
        :padding="24"
        title="防沙尘暴签署单"
        :showFooterBtn="getBasicVo.rxStatus == '0'"
        footerBtnText="接收"
        @handleFooterClick="handleFooterClick"
        showNavRight
        navRightType="button"
        navRightText="保存"
        :navRightDisabled="getDisabled"
        @handleRightClick="handleRightClick"
    >
        <template v-if="details">
            <view class="mocp-cell">
                <up-collapse :value="['1', '2', '3']">
                    <up-collapse-item name="1">
                        <template #title>
                            <view class="mocp-title">
                                <text class="txt">基础信息</text>
                                <view>
                                    <global-tag :type="useGetDictByValue('wdm_rxStatusType', getBasicVo.rxStatus)">
                                        {{ useGetDictByValue('wdm_rxStatus', getBasicVo.rxStatus) }}
                                    </global-tag>
                                </view>
                            </view>
                        </template>
                        <template #value>
                            <view style="margin-right: 24rpx">
                                <global-button type="text" :disabled="getDisabled" @tap.stop="navigateTo('/panel/windDamageManagement/editBasic')">
                                    编辑
                                </global-button>
                            </view>
                        </template>
                        <view class="mocp-cell">
                            <up-cell-group>
                                <up-cell title="报文生成时间" :value="details.generateTime || '-'" />
                                <up-cell title="预报风速" :value="getBasicVo.windSpeed || '-'" />
                                <up-cell title="预计开始时间" :value="details.startTime || '-'" />
                                <up-cell title="基地值班" :value="getBasicVo.dutyName || '-'" />
                                <up-cell isLink>
                                    <template #title>
                                        <global-field label="航站"></global-field>
                                        <global-field :value="baseStore.getTerminalObject[details.terminal] || '-'"></global-field>
                                    </template>
                                    <template #value>
                                        <text @tap="goWeather">气象信息</text>
                                    </template>
                                </up-cell>
                                <up-cell title="机号" :value="getBasicVo.ac || '-'" />
                                <up-cell title="操作人" :value="getBasicVo.operateName || '-'" />
                                <up-cell title="发起人" :value="getBasicVo.proposedName || '-'" />
                                <up-cell title="发起时间" :value="timeStampFormat(getBasicVo.proposedTime) || '-'" />
                                <up-cell isLink>
                                    <template #title>
                                        <global-field label="填报人:" :value="getBasicVo.fillingName || '-'"></global-field>
                                        <global-field label="填报时间:" :value="timeStampFormat(getBasicVo.fillingTime) || '-'"></global-field>
                                    </template>
                                    <template #value>
                                        <text @tap="openReceiveHistory">接收历史</text>
                                    </template>
                                </up-cell>
                            </up-cell-group>
                        </view>
                    </up-collapse-item>
                    <up-collapse-item title="工作内容" name="2">
                        <view class="mocp-cell">
                            <up-cell-group>
                                <up-cell title="开始时间">
                                    <template #value>
                                        <view>
                                            <global-date
                                                mode="datetime"
                                                pickAlign="right"
                                                v-model="formData.startTime"
                                                format="YYYY-MM-DD HH:mm:ss"
                                                clearable
                                                :disabled="getDisabled"
                                            ></global-date>
                                        </view>
                                    </template>
                                </up-cell>
                                <up-cell title="查看TA文件" :border="false" isLink @tap="openTaByAc" />
                            </up-cell-group>
                        </view>
                        <view class="mocp-cell">
                            <up-cell-group>
                                <!-- 1 -->
                                <up-cell>
                                    <template #title>
                                        <view>
                                            <view>
                                                1、如伴有尘暴、沙暴、扬沙（航后飞机和停场飞机（含长过站停场））等天气，需参考TA《各机型大风条件下稳定性标准及沙尘暴天气条件下的防护措施》执行封堵工作。
                                            </view>
                                            <view>查询路径：工程网-TDMS-文件编制-TA查询。</view>
                                        </view>
                                    </template>
                                </up-cell>
                                <up-cell title="是否完成">
                                    <template #value>
                                        <view>
                                            <global-radio
                                                radioAlign="right"
                                                v-model="formData.taComplete"
                                                clearable
                                                dictkey="wdm_complete"
                                                :disabled="getDisabled"
                                            ></global-radio>
                                        </view>
                                    </template>
                                </up-cell>
                                <up-cell title="发现问题及处理措施" :border="false">
                                    <template #label>
                                        <up-textarea
                                            placeholder="请输入"
                                            v-model="formData.taMeasure"
                                            :height="40"
                                            border="none"
                                            :disabled="getDisabled"
                                        ></up-textarea>
                                    </template>
                                </up-cell>
                                <up-cell>
                                    <template #label>
                                        <global-upload
                                            v-model="formData.taFiles"
                                            :file-list="getWorkVo.taFiles"
                                            :show-upload="!getDisabled"
                                        ></global-upload>
                                    </template>
                                </up-cell>
                            </up-cell-group>
                        </view>
                        <view class="mocp-cell">
                            <up-cell-group>
                                <up-cell>
                                    <template #title>填报人:{{ getWorkVo.fillingName || '-' }}</template>
                                    <template #value>填报时间:{{ timeStampFormat(getWorkVo.fillingTime) || '-' }}</template>
                                </up-cell>
                                <up-cell title="结束时间">
                                    <template #value>
                                        <view>
                                            <global-date
                                                mode="datetime"
                                                pickAlign="right"
                                                format="YYYY-MM-DD HH:mm:ss"
                                                v-model="formData.endTime"
                                                clearable
                                                :disabled="getDisabled"
                                            ></global-date>
                                        </view>
                                    </template>
                                </up-cell>
                            </up-cell-group>
                        </view>
                        <view class="mocp-cell">
                            <up-cell-group>
                                <up-cell>
                                    <template #title>操作人:{{ getBasicVo.relieveName || '-' }}</template>
                                    <template #value>操作时间:{{ timeStampFormat(getBasicVo.relieveTime) || '-' }}</template>
                                </up-cell>
                            </up-cell-group>
                            <view style="padding: 12rpx">
                                <global-button
                                    size="medium"
                                    type="primary"
                                    @tap="saveRelieveInfo"
                                    :disabled="getBasicVo.isRelieve == '1' || getDisabled"
                                >
                                    解除封堵
                                </global-button>
                            </view>
                        </view>
                    </up-collapse-item>
                    <up-collapse-item title="结论" name="3">
                        <template #value>
                            <view style="margin-right: 24rpx">
                                <global-button
                                    type="text"
                                    :disabled="getDisabled"
                                    @tap.stop="navigateTo('/panel/windDamageManagement/editConclusion')"
                                >
                                    编辑
                                </global-button>
                            </view>
                        </template>
                        <view class="mocp-cell">
                            <up-cell-group>
                                <up-cell title="是否有异常" :value="useGetDictByValue('wdm_isAbnormal', getConclusionVo.isAbnormal) || '-'" />
                                <up-cell>
                                    <template #label>
                                        {{ getConclusionVo.content || '无' }}
                                    </template>
                                </up-cell>
                                <up-cell title="附件">
                                    <template #label>
                                        <view style="margin-top: 16rpx">
                                            <global-upload :file-list="getConclusionVo.files" :show-upload="false"></global-upload>
                                        </view>
                                    </template>
                                </up-cell>
                                <up-cell>
                                    <template #title>填报人:{{ getConclusionVo.fillingName || '-' }}</template>
                                    <template #value>填报时间:{{ timeStampFormat(getConclusionVo.fillingTime) || '-' }}</template>
                                </up-cell>
                            </up-cell-group>
                        </view>
                    </up-collapse-item>
                </up-collapse>
            </view>
        </template>
    </global-page>
    <ReceiveHistoryList ref="receiveHistoryListRef"></ReceiveHistoryList>
    <TaList ref="taListRef"></TaList>
    <up-modal
        :width="250"
        title="提示"
        content="填写完成后请及时保存!"
        confirmText="知道了"
        :show="show"
        closeOnClickOverlay
        @confirm="saveReceiveInfo"
        @cancel="show = false"
        @close="show = false"
    ></up-modal>
</template>

<script setup>
import { storeToRefs } from 'pinia'
import useBaseStore from 'mocp/store/base'
import { timeStampFormat } from 'mocp/utils/tool'
import { useGetDictByValue } from 'mocp/hooks/use-dict/useDict'
import { onLoad } from '@dcloudio/uni-app'
import { reactive, ref } from 'vue'
import { navigateTo } from 'mocp/utils/jump'
import useWindDamageManagementStore from 'mocp/store/windDamageManagement'
import ReceiveHistoryList from './components/ReceiveHistoryList.vue'
import useUserStore from 'mocp/store/user'
import { saveReceiveInfoApi, saveRelieveInfoApi, saveSandstormWorkApi } from 'mocp/api/windDamageManagement'
import TaList from './components/TaList.vue'

const baseStore = useBaseStore()
const windDamageManagementStore = useWindDamageManagementStore()
const { details, getBasicVo, getConclusionVo, getWorkVo, basicId, tabIndex, getDisabled } = storeToRefs(windDamageManagementStore)
onLoad(() => {
    init()
})
const init = async () => {
    await windDamageManagementStore.getAllTaskInfoByWeather()
    Object.assign(formData, getWorkVo.value)
    formData.basicId = basicId.value
}
//接收历史
const receiveHistoryListRef = ref()
const openReceiveHistory = () => {
    receiveHistoryListRef.value?.open({ basicId: basicId.value, taskType: tabIndex.value + 1 })
}
//接收
const userStore = useUserStore()
const show = ref(false)
const handleFooterClick = () => {
    show.value = true
}
const saveReceiveInfo = async () => {
    const res = await saveReceiveInfoApi(
        { basicId: basicId.value, taskType: tabIndex.value + 1, receiveUid: userStore.userInfo?.id },
        { loading: true }
    )
    if (res.code == 200) {
        show.value = false
        uni.$mocpMessage.success(res.message)
        init()
        uni.$emit('windDamageManagementReload')
    } else {
        uni.$mocpMessage.error(res.message)
    }
}
//气象信息
const goWeather = () => {
    if (details.value?.sandstormIsOpen == '0') {
        return uni.$mocpMessage.warning('气象信息已失效')
    }
    navigateTo('/panel/windDamageManagement/weather', { terminal: details.value?.terminal })
}
//保存
const formData = reactive({
    basicId: '',
    endTime: undefined,
    id: '',
    startTime: undefined,
    taComplete: undefined,
    taFiles: '',
    taMeasure: ''
})
const handleRightClick = async () => {
    const res = await saveSandstormWorkApi(formData, { loading: true })
    if (res.code == 200) {
        uni.$mocpMessage.success(res.message)
        init()
        uni.$emit('windDamageManagementReload')
    } else {
        uni.$mocpMessage.error(res.message)
    }
}
//解除封堵
const saveRelieveInfo = async () => {
    const res = await saveRelieveInfoApi({ basicId: basicId.value, taskType: tabIndex.value + 1 }, { loading: true })
    if (res.code == 200) {
        uni.$mocpMessage.success(res.message)
        init()
        uni.$emit('windDamageManagementReload')
    } else {
        uni.$mocpMessage.error(res.message)
    }
}
//TA文件
const taListRef = ref()
const openTaByAc = async () => {
    taListRef.value?.open({ ac: getBasicVo.value.ac })
}
</script>
<style lang="scss" scoped>
@import './constants/details.scss';
</style>
