<script setup lang="ts">
import { httpBusinesslist } from "/@/api/interest/organize";
import { useVModel } from "@vueuse/core"
import { computed, onMounted, ref } from "vue";

const initialData = {
  order_use: '',
  id: ''
}

const emit = defineEmits(['submit'])
const props = withDefaults(
  defineProps<{ data: any; visible: boolean; disabled:boolean }>(),
  { data: {  order_use: '', id:'' } }
)

const business = ref<any[]>([])
const title = computed(() => data.value.id ? (props.disabled ? '查看订单用途' : '编辑订单用途') : '添加订单用途')
const visible = useVModel(props, 'visible')
const data = useVModel(props, 'data')

const formRef = ref<any | null>(null)

const rules = {
  order_use: [ { required: true,message: '请输入订单用途', trigger: 'input' }]
}

async function requestBusiness(){
  const res = await httpBusinesslist({ size:100 })
  business.value = res.code === 0 ? res.data.list : []
}

function handleClose(){
  data.value = {...initialData}
  visible.value = false
}

async function onSubmit(){
  try {
    await formRef.value.validate();
    emit("submit");
  } catch (err) {
    console.log(err);
  }
}

onMounted(requestBusiness)
</script>


<template>
  <ElDialog v-model="visible" :title="title" center @close="handleClose">
    <ElForm :model="data" :rules="rules" size="small" ref="formRef" :disabled="disabled">
      <ElFormItem label="订单用途" prop="order_use">
        <ElInput v-model="data.order_use" style="width:100%" placeholder="订单用途" />
      </ElFormItem>
      <div class="w-full flex justify-end">
        <ElButton type="primary" @click="onSubmit">保存</ElButton>
      </div>
    </ElForm>
  </ElDialog>
</template>
