<!-- 我的仓储-我的库存-上架 -->
<template>
    <app-drawer title="上架挂牌" :width="480" v-model:show="show" :loading="submitLoading" :refresh="refresh">
        <el-form ref="formRef" label-width="100px" :model="formData" :rules="formRules">
            <el-form-item label="商品编号">
                {{selectedRow.goodsno}}
            </el-form-item>
            <el-form-item label="履约方式" prop="PerformanceTemplateID">
                <app-performance v-model="formData.PerformanceTemplateID" />
            </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 { useSellOrderListing } from '@/business/trade/listing'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppPerformance from '@pc/components/modules/performance/index.vue'

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

const { submitLoading, formData, formSubmit } = useSellOrderListing(props.selectedRow)
const show = ref(true)
const refresh = ref(false)
const formRef = ref<FormInstance>()

const formRules: FormRules = {
    PerformanceTemplateID: [{ required: true, message: '请选择履约方式' }],
}

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>