<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" :formData="formData" ref="elform" :rules="rules"></FormMessage>
    </div>
  </div>
</template>

<script setup>
import * as rule from "@/utils/rules.js";
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: "bqMc", type: "input", required: true },
  { label: "标签代码", prop: "bqDm", type: "input", required: true },
  { label: "标签说明", prop: "bqSm", type: "input", required: true },
  { label: "组合标签名称", prop: "bqmc", type: "input", required: true },
  { label: "标签大类", prop: "bqLb", type: "select", options: props.dic.D_GS_BQ_LB },
  { label: "标签细类", prop: "bqLx", type: "select", options: props.dic.D_GS_BQ_LX  },
  { label: "标签等级", prop: "bqDj", type: "select", options: props.dic.D_GS_BQ_DJ },
  { label: "标签颜色", prop: "bqYs", type: "select", options: props.dic.D_GS_SSYJ },
  { label: "标签分值", prop: "bqFz", type: "input" },
]);
const rules = reactive({
  bqmc: [{ required: true, message: "请输入标签名称", trigger: "blur" }]
});

const listQuery = ref({}); //表单
const loading = ref(false);
const elform = ref();
const title = ref("");

onMounted(() => {});
// 初始化数据
const init = (type, row,) => {
  dialogForm.value = true;
  title.value = row ? "编辑" : "新增";
  if (row) getDataById(row.id);
};

// 根据id查询详情
const getDataById = (id) => {
  qcckGet({}, '/mosty-gsxt/tbGsxtBqgl/'+id).then((res) => {
    listQuery.value = res;
  });
};

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

// 关闭
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>
