<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="120px" :rules="formRules" style="padding: 0 80px 0px 0;">
      <el-form-item label="版本号：" prop="versionName">
        <el-input v-model.trim="form.versionName" placeholder="请输入版本号" clearable maxlength="30" />
      </el-form-item>
      <el-form-item label="系统类型：" prop="systemType">
        <el-select v-model.trim="form.systemType" placeholder="请选择系统类型" clearable filterable>
          <el-option v-for="item in systemTypeList.list" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="更新内容：" prop="editorText">
        <editorWang ref="editorWangRef"></editorWang>
      </el-form-item>

    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancelFn">取消</el-button>
        <el-button type="primary" @click="publishFn">
          确认发布
        </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"
const emits = defineEmits(['send'])
const addShow = ref(false)
let title = ref("新增")
const editorWangRef = ref()

const ruleFormRef: any = ref()
const form = reactive({
  versionName: '',
  systemType: '',
  editorText: ''
})
const systemTypeList = reactive({
    list: [{

        value: 'Option1',
        label: 'Option1',
    },
    {
        value: 'Option2',
        label: 'Option2',

    }]
})
 
const formRules = reactive<FormRules<any>>({
  versionName: [
    { required: true, message: '请输入版本号', trigger: 'blur' },
  ],
  systemType: [
    { required: true, message: '请选择系统类型', trigger: 'change' },
  ],
  editorText:[
    { required: true, message: '请输入更新内容', trigger: 'blur' },
  ]

})

const publishFn = () => {
  if (!ruleFormRef) return
  ruleFormRef.value.validate((valid: any) => {
    if (valid) {
      const editVal= editorWangRef.value.getHtmlByFather()
      console.log("editVal",editVal)
 
      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 open = (val: any) => {
  title.value = "新增"
  addShow.value = true
}

const edit = (val:any) => {
  title.value = "编辑"
  addShow.value = true
  nextTick(()=>{
    editorWangRef.value.setHtmlByFather("<p>head</p><p>hello <strong>word</strong></p>")

  })
}
const show = (val:any) => {
  title.value =val.title
  addShow.value = true
  nextTick(()=>{
    editorWangRef.value.setHtmlByFather("<p>head</p><p>hello <strong>word</strong></p>")

  })
}


defineExpose({
  open,
  edit,
  show
})

</script>

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