<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="102px" :rules="formRules" style="padding: 0 80px 0px 0;">
      <el-form-item label="模版编号：" prop="title">
        <el-input v-model.trim="form.title" placeholder="请输入模版编号" clearable maxlength="30" />
      </el-form-item>
      <el-form-item label="应用模块" prop="applyModel">
        <el-select v-model.trim="form.applyModel" placeholder="Select" clearable filterable style="width: 100%;">
          <el-option v-for="item in applyModelList.list" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="协议附件：" prop="fileUrl">
        <uploadCom :maxLimit="3" listType="picture-card" :acceptTypeList="['.jpg','.jpeg','.png']">
          <template v-slot:uploadBtn>
            <el-icon size="48" color="#aaa"><Plus /></el-icon>
          </template>
        </uploadCom>
      </el-form-item>
      <el-form-item label="协议内容："  style="width:1000px;"  prop="editHtml">
        <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({
  code: '',
  applyModel: '',
  fileUrl: '',
  editHtml:""
})
const applyModelList = reactive({
    list: [{

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

    }]
})
 
const formRules = reactive<FormRules<any>>({
  applyModel: [
    { required: true, message: '请选择应用模块', trigger: 'blur' },
  ],
  fileUrl: [
    { required: true, message: '请上传协议附件', trigger: 'change' },
  ],
  editHtml: [
    { 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 = () => {
  title.value = "编辑"
  addShow.value = true
  editorWangRef.value.setHtmlByFather("<p>head</p><p>hello <strong>word</strong></p>")
}
const showFn  =(val: any) => {
  title.value =val.title
  addShow.value = true
}
defineExpose({
  open,
  edit,
  showFn
})

</script>

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