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

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

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

const title = computed(() => data.value.id ? (props.disabled ? '查看规格' : '编辑规格') : '添加规格')
const business = ref<any[]>([])
const visible = useVModel(props, 'visible')
const formRef = ref<any | null>(null)
const data = useVModel(props, 'data')


const rules = { spec_name: [{ 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="spec_name">
        <ElInput v-model="data.spec_name" 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>
