<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" ref="formRef">
                <up-form-item label="领导批示意见" :borderBottom="true" prop="leaderApprovalComments" required>
                    <global-picker pickAlign="right" v-model="formData.leaderApprovalComments" clearable dictkey="aw_leaderState"></global-picker>
                </up-form-item>
                <up-form-item label="批示要点" :borderBottom="true" labelPosition="top" prop="approvalContent" required>
                    <up-textarea
                        v-model.trim="formData.approvalContent"
                        placeholder="一段很长很长的内容文字，长文本自动换行，该选项的描述是一段很长的内容"
                        :height="40"
                        border="none"
                    ></up-textarea>
                </up-form-item>
            </up-form>
        </view>
    </global-page>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app'
import { savaLeaderApprovalApi } from 'mocp/api/assign-work'
import useAssignWorkStore from 'mocp/store/assign-work'
import { storeToRefs } from 'pinia'
import { reactive, ref } from 'vue'

const assignWorkStore = useAssignWorkStore()
const { arrangeWorkExtend } = storeToRefs(assignWorkStore)
const formData = reactive({
    leaderApprovalComments: '',
    approvalContent: '',
    approvalUser: '',
    approvalTime: ''
})
const rules = reactive({
    leaderApprovalComments: [
        {
            type: 'number',
            required: true,
            message: '请选择领导批示意见',
            trigger: ['blur', 'change']
        }
    ],
    approvalContent: [
        {
            required: true,
            message: '请输入批示要点',
            trigger: ['blur', 'change']
        }
    ]
})
//页面初始化
onLoad(() => {
    formData.id = arrangeWorkExtend.value.id
    formData.workId = arrangeWorkExtend.value.workId
    formData.company = arrangeWorkExtend.value.company
    formData.leaderApprovalComments = arrangeWorkExtend.value.leaderApprovalComments
    formData.approvalContent = arrangeWorkExtend.value.approvalContent
    formData.approvalUser = arrangeWorkExtend.value.approvalUser
    formData.approvalTime = arrangeWorkExtend.value.approvalTime
})
//保存
const formRef = ref()
const handleSave = async () => {
    await formRef.value?.validate()
    const res = await savaLeaderApprovalApi(formData, { loading: true })
    if (res.code == 200) {
        uni.$mocpJump.navigateBack()
        uni.$mocpMessage.success(res.message)
        //更新当前责任单位详情
        assignWorkStore.setState('arrangeWorkExtend', res.data)
    } else {
        uni.$mocpMessage.error(res.message)
    }
}
</script>
<style lang="scss" scoped>
.mocp-form {
    background: #fff;
    margin-bottom: 24rpx;
    padding: 0 32rpx;
}
</style>
