<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']" ref="collapseRef">
                    <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.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>
                                        <global-date-picker v-model="formData.startTime" :disabled="getDisabled"></global-date-picker>
                                    </template>
                                </up-cell>
                            </up-cell-group>
                        </view>
                        <view class="mocp-cell">
                            <up-cell-group>
                                <template v-for="(item, index) in formData.workItems" :key="item.id">
                                    <up-cell>
                                        <template #title>
                                            <view>{{ index + 1 }}、{{ item.title }}</view>
                                        </template>
                                    </up-cell>
                                    <up-cell title="是否完成">
                                        <template #value>
                                            <view>
                                                <global-radio
                                                    radioAlign="right"
                                                    v-model="item.isComplete"
                                                    clearable
                                                    dictkey="wdm_complete"
                                                    :disabled="getDisabled"
                                                ></global-radio>
                                            </view>
                                        </template>
                                    </up-cell>
                                    <up-cell title="发现问题及处理措施" :border="false">
                                        <template #label>
                                            <up-textarea
                                                placeholder="请输入"
                                                v-model="item.measure"
                                                :height="40"
                                                border="none"
                                                :disabled="getDisabled"
                                            ></up-textarea>
                                        </template>
                                    </up-cell>
                                    <up-cell>
                                        <template #label>
                                            <global-upload
                                                v-model="item.files"
                                                :file-list="getWorkVo.workItems?.[index].files"
                                                :show-upload="!getDisabled"
                                            ></global-upload>
                                        </template>
                                    </up-cell>
                                </template>
                            </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>
                                        <global-date-picker v-model="formData.endTime" :disabled="getDisabled"></global-date-picker>
                                    </template>
                                </up-cell>
                            </up-cell-group>
                        </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>
    <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 { getPwdWorkTemplateItemsByStnApi, savePatrolWorkApi, saveReceiveInfoApi } from 'mocp/api/windDamageManagement'
import { cloneDeep } from 'lodash'

const baseStore = useBaseStore()
const windDamageManagementStore = useWindDamageManagementStore()
const { details, getBasicVo, getConclusionVo, getWorkVo, basicId, tabIndex, getDisabled } = storeToRefs(windDamageManagementStore)
onLoad(() => {
    init()
})
const collapseRef = ref()
//获取巡场检查单工作内容模板明细
const getPwdWorkTemplateItemsByStn = async () => {
    const res = await getPwdWorkTemplateItemsByStnApi({ terminal: details.value?.terminal }, { loading: true })
    if (res.code == 200) {
        formData.workItems =
            res.data.map((item) => {
                return {
                    ...item,
                    isComplete: '0',
                    measure: '',
                    files: ''
                }
            }) || []
    } else {
        uni.$mocpMessage.error(res.message)
    }
}
const init = async () => {
    await windDamageManagementStore.getAllTaskInfoByWeather()
    formData.id = getWorkVo.value?.id
    formData.startTime = getWorkVo.value?.startTime
    formData.endTime = getWorkVo.value?.endTime
    if (getWorkVo.value?.workItems) {
        formData.workItems = cloneDeep(getWorkVo.value?.workItems) || []
    } else {
        await getPwdWorkTemplateItemsByStn()
    }
    formData.basicId = basicId.value
    collapseRef.value?.init()
}
//接收历史
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?.patrolIsOpen == '0') {
        return uni.$mocpMessage.warning('气象信息已失效')
    }
    navigateTo('/panel/windDamageManagement/weather', { terminal: details.value?.terminal })
}
//保存
const formData = reactive({
    basicId: '',
    endTime: undefined,
    id: '',
    startTime: undefined,
    workItems: []
})
const handleRightClick = async () => {
    const res = await savePatrolWorkApi(formData, { loading: true })
    if (res.code == 200) {
        uni.$mocpMessage.success(res.message)
        init()
        uni.$emit('windDamageManagementReload')
    } else {
        uni.$mocpMessage.error(res.message)
    }
}
</script>
<style lang="scss" scoped>
@import './constants/details.scss';
</style>
