<template>
  <div class="dialog" v-if="dialogForm">
    <div class="head_box">
      <span class="title">情报语义分析{{ title }} </span>
      <div>
        <el-button type="primary" size="small" :loading="loading" @click="submit" >保存</el-button>
        <el-button size="small" @click="close">关闭</el-button>
      </div>
    </div>
    <div class="form_cnt">
      <FormMessage v-model="listQuery" :formList="formData" ref="elform" :rules="rules"></FormMessage>
    </div>
  </div>
</template>

<script setup>
import FormMessage from '@/components/aboutTable/FormMessage.vue'
import { qcckGet, qcckPost, qcckPut } from "@/api/qcckApi.js";
import { ref, defineExpose, reactive, onMounted, defineEmits, getCurrentInstance, nextTick } from "vue";
const emit = defineEmits(["updateDate"]);
const props = defineProps({
  dic: Object
});
const { proxy } = getCurrentInstance();
const dialogForm = ref(false); //弹窗
const formData = ref([
  { label: "语义名称", prop: "yymc", type: "input", },
  { label: "要素类型", prop: "yslx",  type: "input"},
  { label: "要素名称", prop: "ysmc",  type: "input"},
  { label: "要素描述", prop: "ysms",  type: "textarea", width: "100%"  },
]);
const rules = reactive({
  yymc: [{ required: true, message: "请输入标签名称", trigger: "blur" }],
  yslx: [{ required: true, message: "请输入要素类型", trigger: "blur" }],
  ysmc: [{ required: true, message: "请输入要素名称", trigger: "blur" }],
  ysms: [{ required: true, message: "请输入要素描述", trigger: "blur" }],
});
const listQuery = ref({}); //表单
const loading = ref(false);
const elform = ref();
const title = ref("");
onMounted(() => {});
// 初始化数据
const init = (type, id) => {
  dialogForm.value = true;
  title.value = type == 'add' ? "新增" : "编辑";
  if (id) getDataById(id);
};
// 根据id查询详情
const getDataById = (id) => {
  qcckGet({id}, '/mosty-gsxt/qbyy/selectByid').then((res) => {
    listQuery.value = res;
  });
};

// 提交
const submit = () => {
  elform.value.submit((data)=>{
    let url =  title.value == "新增" ? '/mosty-gsxt/qbyy/add':'/mosty-gsxt/qbyy/update';
    let params = { ...data }
    loading.value = true;
    qcckPost(params, url).then((res) => {
      loading.value = false;
      proxy.$message({ type: "success", message: title.value + "成功" });
      emit("updateDate");
      close();
    }).catch(()=>{
      loading.value = false;
    })
  });
};

// 关闭
const close = () => {
  listQuery.value = {};
  dialogForm.value = false;
  loading.value = false;
};
defineExpose({ init });
</script>

<style lang="scss" scoped>
@import "~@/assets/css/layout.scss";
@import "~@/assets/css/element-plus.scss";
</style>
