<template>
  <el-dialog v-model.trim="addShow" :title="title" width="1200px" draggable :close-on-click-modal="false">
    <el-form :model="form" ref="ruleFormRef" label-width="88px" :rules="formRules" style="padding: 0 80px 0px 0;" :disabled="readFlag">
      <el-form-item label="标题：" prop="topicName">
        <el-input v-model.trim="form.topicName" placeholder="请输入标题" clearable maxlength="30" />
      </el-form-item>
      <el-form-item label="分类" prop="type">
        <el-select v-model.trim="form.type" placeholder="Select" clearable filterable>
          <el-option v-for="item in categoryList.list" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item >
        <editorWang ref="editorWangRef"></editorWang>
      </el-form-item>

    </el-form>
    <template #footer>
      <span class="dialog-footer" v-if="!readFlag">
        <el-button @click="cancelFn">取消</el-button>
        <el-button type="primary" @click="publishFn">
          确认发布
        </el-button>
      </span>
      <span class="dialog-footer" v-else> 
        <el-button @click="closeFn">关闭</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import editorWang from "components/com/editorWang.vue"
import { apiBmTopicInfoAddorupdate,apiBmTopicInfoById } from "http/api/config"
import { Console } from 'console';
const emits = defineEmits(['send'])
const addShow = ref(false)
let title = ref("新增")
const editorWangRef = ref()
const readFlag = ref(false)
const ruleFormRef: any = ref()
const form = reactive({
  topicName: '',
  type: '1',
  content: '',
  id:''
})
const categoryList = reactive({
  list: [{

    value: '1',
    label: '专题',
  },
  {
    value: '2',
    label: '咨询',
  }]
})

const formRules = reactive<FormRules<any>>({
  topicName: [
    { required: true, message: '请输入标题', trigger: 'blur' },
  ],
  type: [
    { required: true, message: '请选择分类', trigger: 'change' },
  ]

})

const publishFn = () => {
  if (!ruleFormRef) return
  ruleFormRef.value.validate((valid: any) => {
    if (valid) {
      form.content = editorWangRef.value.getHtmlByFather()
      InfoAddorupdate()
      console.log('submit!')
    } else {
      console.log('error submit!')
      return false
    }
  })
}

const cancelFn = (formEl: FormInstance | undefined) => {
  if (!ruleFormRef) return
  ruleFormRef.value.resetFields()
  editorWangRef.value.editorClear()
  addShow.value = false
}

const InfoAddorupdate = async () => {
  let params = {
    ...form
  }
  const res = await apiBmTopicInfoAddorupdate(params)
  console.log("res=========apiBmTopicInfoAddorupdate", res)
  ElMessage({
    type: "success",
    message: title.value + '成功'
  })
  addShow.value = false
  form.content =''
  ruleFormRef.value.resetFields()
  editorWangRef.value.editorClear()

}


const getTopicInfoById=async(id:any)=>{
  const res:any = await apiBmTopicInfoById(id)
  console.log("resapiBmTopicInfoById",res)
  const data = res.data
  editorWangRef.value.setHtmlByFather(data.content)
  Object.assign(form,data)
}


const closeFn =()=>{
  addShow.value = false
}


const open = (val: any) => {
  title.value = "新增"
  addShow.value = true
}

const edit = (id:any) => {
  title.value = "编辑"
  addShow.value = true
  getTopicInfoById(id)

}

const show = (id: any) => {
  title.value = "详情"
  addShow.value = true
  readFlag.value=true
  getTopicInfoById(id)
}



defineExpose({
  open,
  edit,
  show
})

</script>

<style scoped lang="scss"></style>
