<template>
  <el-dialog :model-value="visible" @update:model-value="(val) => emit('update:visible', val)" :title="title"
    width="500">
    <el-input v-model.trim="ivalue" :maxlength="100" show-word-limit type="textarea" :placeholder="placeholder" />
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="emit('update:visible', false)">取消</el-button>
        <el-button type="primary" @click="submit"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ElMessage } from "element-plus";
import { ref, watchEffect } from "vue";

const props = withDefaults(
  defineProps<{
    visible: boolean;
    value: string;
    title: string;
    name?: string;
    placeholder: string;
    isAllowEmpty?: boolean;
    updateValue: (value: string) => void;
  }>(),
  {
    placeholder: "请输入",
  }
);
const emit = defineEmits<{
  (e: "update:visible", visible: boolean): void;
}>();

const ivalue = ref(props.value);
watchEffect(() => {
  ivalue.value = props.value;
});

const submit = async () => {
  if (ivalue.value.length === 0 && !props.isAllowEmpty) {
    return ElMessage.error(`${props.name || "点位"}名称不能为空！`);
  }
  await props.updateValue(ivalue.value);
  emit("update:visible", false);
};
</script>
