<template>
    <global-page :title="getTitle" showFooterBtn @handleFooterClick="handleSubmit">
        <view class="mocp-form">
            <up-form ref="formRef" labelPosition="left" labelWidth="auto" :model="formData" :rules="rules">
                <up-form-item label="预报风速" :borderBottom="true" prop="windSpeed" required>
                    <up-input border="none" inputAlign="right" clearable v-model="formData.windSpeed" placeholder="请输入预报风速"></up-input>
                </up-form-item>
                <up-form-item label="基地值班" :borderBottom="true">
                    <custom-admin-picker
                        pickAlign="right"
                        clearable
                        v-model="formData.dutyUid"
                        :search-value="getBasicVo.dutyName"
                    ></custom-admin-picker>
                </up-form-item>
                <up-form-item label="航站" :borderBottom="true" prop="terminal" required>
                    <up-input border="none" inputAlign="right" clearable v-model="formData.terminal" disabled disabledColor="#fff"></up-input>
                </up-form-item>
                <up-form-item label="机号" :borderBottom="true" prop="acs" required v-if="tabIndex > 0">
                    <global-picker pickAlign="right" v-model="formData.acs" :options="deviceNumList" clearable filter></global-picker>
                </up-form-item>
                <up-form-item :label="tabIndex == 0 ? '巡场人员' : '操作人'" :borderBottom="true">
                    <custom-admin-picker
                        pickAlign="right"
                        clearable
                        v-model="formData.operateUid"
                        :search-value="getBasicVo.operateName"
                    ></custom-admin-picker>
                </up-form-item>
            </up-form>
        </view>
    </global-page>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app'
import { computed, reactive, ref } from 'vue'
import { savePwdCommonTaskApi } from 'mocp/api/windDamageManagement'
import { storeToRefs } from 'pinia'
import useWindDamageManagementStore from 'mocp/store/windDamageManagement'
import useBaseStore from 'mocp/store/base'

const {
    selectList: { deviceNumList }
} = useBaseStore()
const windDamageManagementStore = useWindDamageManagementStore()
const { details, getBasicVo, tabIndex } = storeToRefs(windDamageManagementStore)
const rules = reactive({
    windSpeed: [
        {
            required: true,
            message: '请输入预报风速',
            trigger: ['blur', 'change']
        }
    ],
    acs: [
        {
            required: true,
            message: '请选择机号',
            trigger: ['blur', 'change']
        }
    ]
})
const getTitle = computed(() => {
    if (formData.taskType == 1) {
        return '追加防风害巡场检查单'
    } else if (formData.taskType == 2) {
        return '追加防风害操作单'
    } else if (formData.taskType == 3) {
        return '追加系留操作单'
    } else {
        return '追加防尘暴签署单'
    }
})
// 表单数据
const formData = reactive({
    dutyUid: undefined,
    operateUid: undefined,
    taskType: 1,
    acs: '',
    terminal: '',
    windSpeed: '',
    weatherId: '',
    weatherSource: undefined
})
// 页面初始化
onLoad(async () => {
    await windDamageManagementStore.getAllTaskInfoByWeather()
    formData.taskType = tabIndex.value + 1
    formData.terminal = details.value?.terminal
    formData.weatherId = details.value?.weatherId
    formData.weatherSource = details.value?.weatherSource
    formData.dutyUid = getBasicVo.value.dutyUid
    formData.operateUid = getBasicVo.value.operateUid
    formData.windSpeed = getBasicVo.value.windSpeed
})
//提交表单
const formRef = ref()
const handleSubmit = async () => {
    await formRef.value?.validate()
    const res = await savePwdCommonTaskApi(formData, { loading: true })
    if (res.code == 200) {
        uni.$mocpJump.navigateBack()
        uni.$emit('windDamageManagementReload')
        uni.$mocpMessage.success(res.message)
    } else {
        uni.$mocpMessage.error(res.message)
    }
}
</script>
<style lang="scss" scoped>
.mocp-form {
    background: #fff;
    padding: 0 32rpx;
}
</style>
