<script setup>
import { ref } from 'vue'
import { moneyUpdateService } from '@/api/information'
const dialogVisible = ref(false)
const formModel = ref({
  venueId: '',
  price: 0
})
const formRef = ref()

const rules = {
  price: [
    { required: true, message: '请输入费用', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        // 判断当前的 value 是否大于 0
        if (value <= 0) {
          callback(new Error('费用需要 >= 0'))
        } else {
          // 校验成功，也需要 callback()
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

// 组件对外暴露一个方法 open ，基于 open 传来的参数，区分添加还是编辑
const open = (id, price) => {
  formModel.value.venueId = id
  formModel.value.price = price
  console.log(formModel.value)

  dialogVisible.value = true
}
// 向外暴露
defineExpose({
  open
})

const emit = defineEmits(['success'])

const onSubmit = async () => {
  await formRef.value.validate()
  ElMessageBox.confirm('确认提交修改？', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      await moneyUpdateService(formModel.value)
      ElMessage.success('修改成功')
      onClose()
      emit('success')
    })
    .catch(() => {})
}

const onClose = () => {
  dialogVisible.value = false
  formModel.value.venueId = ''
  formModel.value.price = 0
  formRef.value.clearValidate()
}
</script>
<template>
  <el-dialog v-model="dialogVisible" title="场地费用" width="30%">
    <el-form
      ref="formRef"
      :model="formModel"
      :rules="rules"
      label-width="100px"
      style="padding-right: 30px"
    >
      <el-form-item label="费用" prop="price">
        <el-input-number
          v-model="formModel.price"
          :precision="2"
          :min="1"
          :max="100"
        >
          <template #suffix>
            <span>RMB</span>
          </template>
        </el-input-number>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="onClose">取消</el-button>
        <el-button type="primary" @click="onSubmit"> 确认 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
