<!-- 商品详情-询价申请 -->
<template>
    <app-drawer title="询价" :width="600" v-model:show="show" :loading="loading">
        <el-form ref="formRef" label-width="60px" :model="formData" :rules="formRules">
            <el-form-item label="价格" prop="ApplyPrice">
                <div class="el-form-item--col">
                    <el-input type="number" placeholder="请输入" v-model.number="formData.ApplyPrice" />
                    <span style="white-space:nowrap;"
                        v-if="selectedRow.zscurrencytype !== CurrencyType.CNY">汇率：{{getExrate(selectedRow.zscurrencytype)}}</span>
                </div>
            </el-form-item>
            <el-form-item label="备注" prop="ApplyRemark">
                <el-input type="textarea" :rows="3" v-model="formData.ApplyRemark" />
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="onCancel" 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 { CurrencyType } from '@/constants/diamond'
import { useExrateStore } from '@/stores'
import { useBuyOrderBargain } from '@/business/trade/desting'
import AppDrawer from '@pc/components/base/drawer/index.vue'

const props = defineProps({
    selectedRow: {
        type: Object as PropType<Ermcp.DiamondDetailsRsp>,
        required: true
    }
})

const { getExrate } = useExrateStore()
const { loading, formData, formSubmit } = useBuyOrderBargain(props.selectedRow)
const formRef = ref<FormInstance>()
const show = ref(true)

const formRules: FormRules = {
    ApplyPrice: [{ required: true, message: '请输入价格', trigger: 'blur' }],
}

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

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