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

const initialData = {
  id: '', // id
  unit: '' // 原因类型
}

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

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

const data = useVModel(props, 'data')
const visible = useVModel(props, 'visible')

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

const rules = { unit: [ { required: true,trigger: 'change',message: '输入单位名称' }]}

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="result">
        <ElInput v-model="data.unit" style="width:100%" placeholder="单位名称" :maxlength="50" show-word-limit />
      </ElFormItem>
      <div class="flex w-full justify-end">
        <ElButton type="primary" @click="onSubmit">保存</ElButton>
      </div>
    </ElForm>
  </ElDialog>
</template>
