<!-- 预售大厅-预售大厅-认购 -->
<template>
    <app-drawer title="认购" v-model:show="show" :width="420" :loading="loading" :refresh="refresh">
        <el-form ref="formRef" label-width="120px" :model="formData" :rules="formRules">
            <el-form-item label="商品名称">
                {{ selectedRow.wrstandardname }}
            </el-form-item>
            <el-form-item label="最小采购单位">
                {{ selectedRow.minbuyqty }} (克拉)
            </el-form-item>
            <el-form-item label="最大采购单位">
                {{ selectedRow.maxbuyqty }} (克拉)
            </el-form-item>
            <el-form-item label="剩余预售量">
                {{ remainqty }} (克拉)
            </el-form-item>
            <el-form-item label="认购数量(克拉)" prop="OrderQty">
                <el-input-number placeholder="请输入" :min="0" :max="maxValue" :precision="2" v-model="formData.OrderQty"
                    style="width: 100%;" />
            </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 { shallowRef, reactive, PropType, computed } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { validate } from '@/constants/regex'
import { maxValue } from '@/constants/common'
import { gzPresaleOrder } from '@/services/api/presale'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import Long from 'long'

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

const formRef = shallowRef<FormInstance>()
const show = shallowRef(true)
const refresh = shallowRef(false)
const loading = shallowRef(false)

const formData = reactive<Partial<Proto.GZPresaleOrderReq>>({
    WRTradeOrderID: Long.fromString(props.selectedRow.sellwrtradeorderid), // 仓单贸易委托单ID,必填
})

// 剩余预售量
const remainqty = computed(() => (props.selectedRow.presaleqty - props.selectedRow.tradeqty).toFixed(2))

const formRules: FormRules = {
    OrderQty: [{
        required: true,
        validator: (rule, value, callback) => {
            const message = validate(value, 'positive')
            if (message) {
                callback(new Error('请输入认购数量'))
            } else {
                const { minbuyqty, maxbuyqty } = props.selectedRow
                const qty = Number(value)
                if (qty > Number(remainqty.value)) {
                    callback(new Error('剩余数量不足'))
                } else if (qty < minbuyqty) {
                    callback(new Error('数量不能小于' + minbuyqty))
                } else if (qty > maxbuyqty) {
                    callback(new Error('数量不能大于' + maxbuyqty))
                } else {
                    callback()
                }
            }
        }
    }],
}

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

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