<script setup lang="ts">
import { reactive, computed, ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { addQuota, getQuotaSourceList } from "@/api/indexDefine";
import {
  getSelectDictListtApi,
  select,
  getFromSelectDictListtApi,
  selectFrom
} from "@/api/select";
// import dayjs from "dayjs";
const emit = defineEmits(["closeDialog"]);
const visible = ref(false);
const ruleFormRef = ref(null);
const form = reactive({
  name: "",
  categoryName: "",
  define: "",
  caliber: "",
  source: "",
  statue: null
});
const sourceDataList = ref([]);
const getQuotaSourceListApi = async () => {
  const { data, code } = await getQuotaSourceList();
  console.log("1112132", data);
  if (code == 200) {
    sourceDataList.value = data;
  }
};
onMounted(() => {
  getQuotaSourceListApi();
  getSelectDictListtApi();
  getFromSelectDictListtApi();
});
const msgName = ref("");
const addQuotaApi = async () => {
  ruleFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      const { code, msg } = await addQuota(form);
      if (code === 200) {
        ElMessage({
          message: "添加成功",
          type: "success"
        });
        emit("closeDialog");
      }
      if (code === 10027) {
        msgName.value = msg;
        // rules.name[0].message = msg; // 将 msg 设置为 name 字段的错误信息
        // ruleFormRef.value.validateField("name"); // 触发验证以显示错误消息
      }
    }
  });
};
const rules = reactive({
  name: [{ required: true, message: "请填写指标名称", trigger: "blur" }],
  define: [{ required: true, message: "请填写指标分类", trigger: "blur" }],
  categoryName: [
    { required: true, message: "请填写指标定义", trigger: "blur" }
  ],
  caliber: [{ required: true, message: "请填写指标口径", trigger: "blur" }],
  source: [{ required: true, message: "来源", trigger: "blur" }],
  statue: [{ required: true, message: "请填写选择状态", trigger: "blur" }]
});
const close = (formEl: FormInstance | undefined) => {
  visible.value = false;
  formEl.resetFields();
  emit("closeDialog");
};
const save = () => {
  addQuotaApi();
};
const open = () => {
  console.log(1111);
  getQuotaSourceListApi();
  // 打开弹框
  Object.assign(form, {
    name: "",
    categoryName: "",
    define: "",
    caliber: "",
    source: "",
    statue: null
  });
  msgName.value = "";
  visible.value = true;
};
defineExpose({
  open
});
const handleClose = (done: () => void) => {
  close(ruleFormRef.value);
  done();
};
</script>

<template>
  <el-dialog
    v-model="visible"
    title="新建指标"
    width="480"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <div class="m-auto w-full">
      <el-form
        ref="ruleFormRef"
        :model="form"
        label-position="right"
        label-width="auto"
        :rules="rules"
        style="max-width: 600px"
      >
        <el-form-item label="指标名称" prop="name">
          <el-input
            v-model="form.name"
            autocomplete="off"
            placeholder="请输入"
          />
          <el-text v-if="msgName" class="absolute top-6 msgName" type="warning">
            指标名称已存在，请更换一个名称
          </el-text>
        </el-form-item>
        <el-form-item label="指标分类">
          <el-select
            v-model="form.categoryName"
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="(item, index) in select"
              :key="index"
              :label="item.dictValue"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="指标定义" prop="define">
          <el-input
            v-model="form.define"
            type="textarea"
            autocomplete="off"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="指标口径" prop="caliber">
          <el-input
            v-model="form.caliber"
            type="textarea"
            autocomplete="off"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="来源">
          <el-select
            v-model="form.source"
            placeholder="请选择"
            clearable
            filterable
          >
            <el-option
              v-for="item in sourceDataList"
              :key="item.id"
              :label="item.sourceValue"
              :value="item.sourceValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="statue">
          <el-select v-model="form.statue" placeholder="请选择">
            <el-option label="草稿" value="0" />
            <el-option label="上架" value="1" />
            <el-option label="下架" value="2" />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="close(ruleFormRef)">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scope>
.msgName {
  font-size: 10px;
}
</style>
