<!-- 订单管理-我的履约-修改联络信息 -->
<template>
    <app-drawer title="买方联络信息" :width="480" v-model:show="show" :loading="loading" :refresh="refresh">
        <el-form ref="formRef" label-width="80px" :model="formData" :rules="formRules">
            <el-form-item label="履约计划">
                <span>{{ selectedRow.curstepid }}</span>
            </el-form-item>
            <el-form-item label="联络信息">
                <el-input type="textarea" :rows="3" v-model="formData.contactInfo" />
            </el-form-item>
            <el-form-item label="收货地址">
                <app-address @change="addressChange" />
            </el-form-item>
            <el-form-item label="发票信息">
                <app-invoice @change="invoiceChange" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)" plain>取消</el-button>
            <el-button type="primary" @click="onSubmit">确认</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { ref, PropType } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import { usePerformanceModify } from '@/business/order'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppAddress from '@pc/components/modules/address/index.vue'
import AppInvoice from '@pc/components/modules/invoice/index.vue'

const props = defineProps({
    selectedRow: {
        type: Object as PropType<Ermcp.MyPerformancRsp>,
        default: () => ({})
    }
})

const { loading, formData, formSubmit } = usePerformanceModify(props.selectedRow)
const show = ref(true)
const refresh = ref(false)
const formRef = ref<FormInstance>()
const formRules: FormRules = {}

// 选择地址
const addressChange = (item?: Ermcp.UserReceiveInfoRsp) => {
    formData.address = item
}

// 选择发票
const invoiceChange = (item?: Ermcp.WrUserReceiptInfoRsp) => {
    formData.invoice = item
}

const onCancel = (isRefresh = false) => {
    show.value = false
    refresh.value = isRefresh
}

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            formSubmit().then(() => {
                ElMessage.success('提交成功')
                onCancel(true)
            }).catch((err) => {
                ElMessage.error('提交失败：' + err)
            })
        }
    })
}
</script>