<template>
  <div>
    <el-dialog
      v-bind="$attrs"
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      @open="onOpen"
      @close="onClose"
    >
      <el-row :gutter="0">
        <el-form
          ref="elFormRef"
          :model="formData"
          :rules="rules"
          size="default"
          label-width="100px"
        >
          <el-col :span="24">
            <el-form-item label="选项名" prop="label">
              <el-input
                v-model="formData.label"
                placeholder="请输入选项名"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="选项值" prop="value">
              <el-input
                v-model="formData.value"
                placeholder="请输入选项值"
                clearable
              >
                <el-select
                  slot="append"
                  v-model="dataType"
                  :style="{ width: '100px' }"
                >
                  <el-option
                    v-for="(item, index) in dataTypeOptions"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                    :disabled="item.disabled"
                  />
                </el-select>
              </el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer">
        <el-button type="primary" @click="handleConfirm"> 确定 </el-button>
        <el-button @click="close"> 取消 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { isNumberStr } from "@/utils/index";
import { reactive, ref, toRefs, watch } from "vue";
const emit = defineEmits(["update:modelValue", "commit"]);
// const props = defineProps(["modelValue"]);
const id = ref(100);
const dataType = ref("string");
let inheritAttrs = false;
const dataTypeOptions = ref([
  {
    label: "字符串",
    value: "string",
  },
  {
    label: "数字",
    value: "number",
  },
]);
const data = reactive({
  formData: {
    label: undefined,
    value: undefined,
  },
  rules: {
    label: [
      {
        required: true,
        message: "请输入选项名",
        trigger: "blur",
      },
    ],
    value: [
      {
        required: true,
        message: "请输入选项值",
        trigger: "blur",
      },
    ],
  },
});
const { rules, formData } = toRefs(data);

const onOpen = () => {
  formData.value = {
    label: undefined,
    value: undefined,
  };
};
const onClose = () => {};

const close = () => {
  emit("update:modelValue", false);
};

const elFormRef = ref();
const handleConfirm = () => {
  elFormRef.value.validate((valid) => {
    if (!valid) return;
    if (dataType.value === "number") {
      formData.value.value = parseFloat(formData.value.value);
    }
    formData.value.id = id.value++;
    emit("commit", formData.value);
    close();
  });
};

watch(formData.value, (val) => {
  dataType.value = isNumberStr(val) ? "number" : "string";
});
</script>
