<template>
    <global-page
        title="编辑航站不正常事件"
        navLeftType="text"
        :showNavRight="true"
        navRightType="button"
        navRightText="保存"
        @handleRightClick="handleSave"
    >
        <view class="mocp-form">
            <up-form labelPosition="left" labelWidth="auto" :model="formData" :rules="rules">
                <up-form-item label="事件来源" prop="source" :borderBottom="true" required>
                    <global-picker v-model="formData.source" pickAlign="right" :options="getParamsByType('EventResource')" clearable></global-picker>
                </up-form-item>
                <up-form-item label="航站" prop="terminalCode" :borderBottom="true" required>
                    <global-picker
                        pickAlign="right"
                        v-model="formData.terminalCode"
                        clearable
                        :options="baseStore.getTerminalSelect"
                        filter
                    ></global-picker>
                </up-form-item>
                <up-form-item label="责任单位" prop="repairUnit" :borderBottom="true" required>
                    <global-picker v-model="formData.repairUnit" pickAlign="right" :options="repairUnitList" clearable filter></global-picker>
                </up-form-item>
                <up-form-item label="责任人" prop="personLiable" :borderBottom="true" required>
                    <up-input v-model="formData.personLiable" border="none" inputAlign="right" placeholder="请输入" clearable></up-input>
                </up-form-item>
                <up-form-item label="机号" prop="machineNumber" :borderBottom="true" required>
                    <global-picker
                        pickAlign="right"
                        v-model="formData.machineNumber"
                        :options="deviceNumList"
                        clearable
                        @change="changeAc"
                        filter
                    ></global-picker>
                </up-form-item>
                <up-form-item label="机型" :borderBottom="true">
                    <up-input
                        border="none"
                        inputAlign="right"
                        clearable
                        v-model="formData.model"
                        disabled
                        disabledColor="#fff"
                        placeholder="请选择机号自动关联"
                    ></up-input>
                </up-form-item>
                <up-form-item label="航司" :borderBottom="true">
                    <up-input
                        border="none"
                        inputAlign="right"
                        clearable
                        disabled
                        disabledColor="#fff"
                        :placeholder="formData.aviation ? '' : '请选择机号自动关联'"
                    >
                        <template #suffix>
                            {{ useGetDictByValue('', formData.aviation, { data: baseStore.getAirlineSelect }) }}
                        </template>
                    </up-input>
                </up-form-item>
                <up-form-item label="事件描述" prop="eventDescription" :borderBottom="true" required labelPosition="top">
                    <up-textarea v-model.trim="formData.eventDescription" placeholder="请输入" :height="40" border="none"></up-textarea>
                </up-form-item>
                <up-form-item label="问题点" prop="problemPoints" :borderBottom="true" required>
                    <up-input v-model="formData.problemPoints" border="none" inputAlign="right" placeholder="请输入" clearable></up-input>
                </up-form-item>
                <up-form-item label="类别" prop="category" :borderBottom="true" required>
                    <global-picker
                        v-model="formData.category"
                        pickAlign="right"
                        clearable
                        :options="getParamsByType('EventCategory')"
                    ></global-picker>
                </up-form-item>
                <up-form-item label="扣分情况" prop="pointsDeduction" :borderBottom="true" required>
                    <up-input
                        type="number"
                        v-model="formData.pointsDeduction"
                        border="none"
                        inputAlign="right"
                        placeholder="请输入"
                        clearable
                    ></up-input>
                </up-form-item>
                <up-form-item label="接报日期" prop="occurrenceTime" :borderBottom="true" required>
                    <global-date
                        pickAlign="right"
                        v-model="formData.occurrenceTime"
                        clearable
                        mode="date"
                        format="YYYY/MM/DD"
                        placeholder="请选择日期"
                    ></global-date>
                </up-form-item>
                <up-form-item label="调查单位" prop="surveyUnit" :borderBottom="true" required>
                    <up-input v-model="formData.surveyUnit" border="none" inputAlign="right" placeholder="请输入" clearable></up-input>
                </up-form-item>
                <up-form-item label="事件性质" prop="eventProperty" :borderBottom="true" required>
                    <global-picker
                        v-model="formData.eventProperty"
                        pickAlign="right"
                        :options="getParamsByType('EventProperty')"
                        clearable
                    ></global-picker>
                </up-form-item>
                <up-form-item label="备注" prop="remake" :borderBottom="true" labelPosition="top">
                    <up-textarea v-model.trim="formData.remake" placeholder="请输入" :height="40" border="none"></up-textarea>
                </up-form-item>
                <up-form-item label="调查已反馈" prop="survey" :borderBottom="true" required>
                    <global-picker v-model="formData.survey" pickAlign="right" dictkey="aem_surveyListSelect2" clearable></global-picker>
                </up-form-item>
                <up-form-item label="附件" :borderBottom="true" labelPosition="top">
                    <global-upload
                        :fileList="formData.surveyAttaFileVoList"
                        v-model="formData.surveyAtta"
                        mapFieldName="id"
                        @getFileList="getSurveyAttaFileVoList"
                        style="margin-top: 24rpx"
                    ></global-upload>
                </up-form-item>
                <up-form-item label="调查完成时限" prop="surveyTimeLimit" :borderBottom="true" v-if="!(formData.survey && formData.survey == '2')">
                    <global-date
                        pickAlign="right"
                        v-model="formData.surveyTimeLimit"
                        clearable
                        mode="date"
                        format="YYYY/MM/DD"
                        placeholder="请选择日期"
                    ></global-date>
                </up-form-item>
                <up-form-item
                    label="调查结果简述"
                    prop="surveyFeedback"
                    :borderBottom="true"
                    labelPosition="top"
                    v-if="!(formData.survey && formData.survey == '2')"
                >
                    <up-textarea v-model.trim="formData.surveyFeedback" placeholder="请输入" :height="40" border="none"></up-textarea>
                </up-form-item>
                <up-form-item label="完成整改" prop="correctiveComplete" :borderBottom="true" required>
                    <global-picker v-model="formData.correctiveComplete" pickAlign="right" dictkey="aem_surveyListSelect2" clearable></global-picker>
                </up-form-item>
                <up-form-item label="附件" :borderBottom="true" labelPosition="top">
                    <global-upload
                        :fileList="formData.correctiveAttaFileVoList"
                        v-model="formData.correctiveAtta"
                        mapFieldName="id"
                        @getFileList="getCorrectiveAtta"
                        style="margin-top: 24rpx"
                    ></global-upload>
                </up-form-item>
                <up-form-item
                    label="整改完成时限"
                    prop="correctiveTimeLimit"
                    :borderBottom="true"
                    v-if="!(formData.correctiveComplete && formData.correctiveComplete == '2')"
                >
                    <global-date
                        pickAlign="right"
                        v-model="formData.correctiveTimeLimit"
                        clearable
                        mode="date"
                        format="YYYY/MM/DD"
                        placeholder="请选择日期"
                    ></global-date>
                </up-form-item>
                <up-form-item
                    label="整改措施"
                    prop="correctiveMeasures"
                    :borderBottom="true"
                    labelPosition="top"
                    v-if="!(formData.correctiveComplete && formData.correctiveComplete == '2')"
                >
                    <up-textarea v-model.trim="formData.correctiveMeasures" placeholder="请输入" :height="40" border="none"></up-textarea>
                </up-form-item>
                <up-form-item label="是否延误" prop="delay" :borderBottom="true" required>
                    <global-picker v-model="formData.delay" pickAlign="right" clearable dictkey="aem_yesOrNoRadio"></global-picker>
                </up-form-item>
            </up-form>
        </view>
    </global-page>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app'
import { storeToRefs } from 'pinia'
import useBaseStore from 'mocp/store/base'
import { useGetDictByValue } from 'mocp/hooks/use-dict/useDict'
import { getParamsByType } from 'mocp/hooks/use-params/useParams'
import { correctiveAttaFileVoList, formData, rules, surveyAttaFileVoList } from './constants/edit.compositions'
import useAbnormalEventManageStore from 'mocp/store/abnormalEventManage'
import { changeAc, getSurveyAttaFileVoList, getCorrectiveAtta, resetData } from './constants/edit.functionals'
import { updateAdverseEventsApi } from 'mocp/api/abnormalEventManage'

const baseStore = useBaseStore()
//获取下拉框选项
const {
    selectList: { deviceNumList, repairUnitList }
} = useBaseStore()
const abnormalEventManageStore = useAbnormalEventManageStore()
const { details } = storeToRefs(abnormalEventManageStore)
//页面初始化
onLoad(() => {
    Object.assign(formData, details.value)
})
//保存
const handleSave = async () => {
    const res = await updateAdverseEventsApi(formData, { loading: true })
    if (res.code == 200) {
        uni.$mocpJump.navigateBack()
        uni.$mocpMessage.success(res.message)
        //更新详情
        res.data.surveyAttaFileVoList = surveyAttaFileVoList.value
        res.data.correctiveAttaFileVoList = correctiveAttaFileVoList.value
        abnormalEventManageStore.setState('details', res.data)
        resetData()
    } else {
        uni.$mocpMessage.error(res.message)
    }
}
</script>
<style lang="scss" scoped>
.mocp-form {
    background: #fff;
    margin-bottom: 24rpx;
    padding: 0 32rpx;
}
</style>
