<template>
    <global-page
        title="经理或授权人意见"
        navLeftType="text"
        :showNavRight="true"
        navRightType="button"
        navRightText="保存"
        @handleRightClick="handleSave"
    >
        <view class="mocp-form">
            <up-form labelPosition="left" labelWidth="auto">
                <up-form-item label="机队经理或其授权人" :borderBottom="true">
                    <global-picker pickAlign="right" v-model="formData.otherName" clearable :options="otherNameOpts" filter></global-picker>
                </up-form-item>
                <up-form-item label="机队经理或其授权人意见" :borderBottom="true">
                    <global-picker pickAlign="right" v-model="formData.opinionType" clearable dictkey="te_opinionType" emptyValue="0"></global-picker>
                </up-form-item>
            </up-form>
        </view>
    </global-page>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app'
import useTechnologyEvaluationStore from 'mocp/store/technology-evaluation'
import { storeToRefs } from 'pinia'
import { reactive, ref } from 'vue'
import useBaseStore from 'mocp/store/base'
import { addOpinionApi } from 'mocp/api/technology-evaluation'

const baseStore = useBaseStore()
const technologyEvaluationStore = useTechnologyEvaluationStore()
const { details } = storeToRefs(technologyEvaluationStore)
const formData = reactive({
    id: '',
    num: '',
    otherName: '',
    opinionType: ''
})
//页面初始化
const otherNameOpts = ref([])
onLoad(() => {
    formData.id = details.value.id
    formData.num = details.value.num
    formData.otherName = details.value.otherName
    formData.opinionType = details.value.opinionType
    baseStore.getAdminList().then((res) => {
        otherNameOpts.value = res
    })
})
//保存
const handleSave = async () => {
    const res = await addOpinionApi(formData, { loading: true })
    if (res.code == 200) {
        uni.$mocpJump.navigateBack()
        uni.$mocpMessage.success(res.message)
        res.data.otherFullName = otherNameOpts.value.find((item) => item.value == formData.otherName)?.label.split('(')[0] || ''
        //更新详情
        technologyEvaluationStore.setState('details', res.data)
    } else {
        uni.$mocpMessage.error(res.message)
    }
}
</script>
<style lang="scss" scoped>
.mocp-form {
    background: #fff;
    margin-bottom: 24rpx;
    padding: 0 32rpx;
}
</style>
