<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 :formList="formData" v-model="listQuery" 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 Xslist from '@/components/ChooseList/ChooseXs/index.vue'
import { ref, defineExpose, reactive, defineEmits, getCurrentInstance } from "vue";
const emit = defineEmits(["updateDate"]);
const props = defineProps({
  dic: Object
});
const { proxy } = getCurrentInstance();

const dialogForm = ref(false); //弹窗

const rules = reactive({
  jymc: [
    { required: true, message: "请输入经验名称", trigger: "blur" }
  ],
  fbnr: [
    { required: true, message: "请输入经验内容", trigger: "blur" }
  ]
});
const formData = ref([

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

// 初始化数据
const init = (type, row) => {
  dialogForm.value = true;
  title.value = type == "add" ? "新增" : "编辑";
  // 根据id查询详情
  if (row) {
    console.log(row,"xxxxx");
    formData.value = [
      { label: "经验名称", prop: "jymc", type: "input", width: "100%" },
      { label: "发布时间", prop: "fbsj", type: "input", width: "50%",disabled:true },
      { label: "发布人", prop: "fbr", type: "input", width: "50%",disabled:true },
    { label: "经验内容", prop: "fbnr", type: "textarea", width: "100%" },
    ]
    listQuery.value = row;
  } else {
    formData.value =   [
  { label: "经验名称", prop: "jymc", type: "input", width: "100%" },
  { label: "任务内容", prop: "fbnr", type: "textarea", width: "100%" },
]
  }
};

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




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

defineExpose({ init });
</script>

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

.boxlist {
  width: 99%;
  margin-top: 10px;
}
::v-deep .el-textarea__inner {
    min-height: 550px !important;
}
</style>
