<!-- 集采交易-我的集采-集采申请-价格编辑 -->
<template>
    <app-drawer title="编辑" :width="400" v-model:show="show">
        <el-form ref="formRef" class="el-form--vertical" label-width="60px" :model="formItem" :rules="formRules">
            <el-form-item label="数量" prop="Qty">
                <el-input-number placeholder="请输入" :min="0" :max="maxValue" :precision="2" v-model="formItem.Qty" />
            </el-form-item>
            <el-form-item label="价格" prop="Price">
                <el-input-number placeholder="请输入" :min="0" :max="maxValue" :precision="2" v-model="formItem.Price" />
            </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 type { FormInstance, FormRules } from 'element-plus'
import { regular } from '@/constants/regex'
import { maxValue } from '@/constants/common'
import AppDrawer from '@pc/components/base/drawer/index.vue'

const props = defineProps({
    selectedRow: {
        type: Object as PropType<Proto.GZCenterPurchasePrice>
    }
})

const emit = defineEmits(['update'])
const show = ref(true)
const formRef = ref<FormInstance>()
const formItem = ref<Partial<Proto.GZCenterPurchasePrice>>({ StepIndex: 0, ...props.selectedRow })

const formRules: FormRules = {
    Qty: [{
        required: true,
        message: '请输入数量',
        pattern: regular.positive.reg
    }],
    Price: [{
        required: true,
        message: '请输入价格',
        pattern: regular.positive.reg
    }],
}

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

const onSubmit = () => {
    formRef.value?.validate((valid) => {
        if (valid) {
            emit('update', formItem.value)
            onCancel()
        }
    })
}
</script>