<template>
  <div class="protocol">
    <el-form :model="form" :rules="rules" ref="ruleFormRef">
      <el-form-item label="协议名称" prop="agreeUnlockName">
        <el-input v-model="form.agreeUnlockName" style="width: 600px" placeholder="请输入协议名称" />
      </el-form-item>
      <!-- <el-form-item label="协议介绍" prop="explain">
        <el-input type="textarea" v-model="form.explain" style="width: 600px" placeholder="请输入协议介绍" />
      </el-form-item> -->
      <el-form-item label="协议内容" prop="agreeUnlockInfo">
        <BasicEditor ref="editotTool"></BasicEditor>
      </el-form-item>
      <el-form-item>
        <div style="width: 80%; text-align: center">
          <el-button style="margin-right: 3rem" @click="$router.go(-1)">返回</el-button>
          <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 更新协议 </el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { Message } from 'view-ui-plus'
import { ref, reactive } from 'vue'
import BasicEditor from '@/components/wangEditor/BasicEditor.vue'
import { cow } from '@/request/api'
import { useRouter } from 'vue-router'
const router = useRouter()

const editotTool = ref()
const ruleFormRef = ref()
const form = ref({})
const rules = reactive({
  agreeUnlockName: [{ required: true, message: '请输入协议名称', trigger: 'blur' }],
  // explain: [{ required: true, message: '请输入协议介绍', trigger: 'blur' }],
  agreeUnlockInfo: [{ required: true, message: '请输入协议内容', trigger: 'blur' }],
})
const loading = ref(false)

const submitForm = async formEl => {
  if (!formEl) return
  form.value.agreeUnlockInfo = editotTool.value.valueHtml
  await formEl.validate((valid, fields) => {
    console.log(editotTool.value.valueHtml)
    if (valid) {
      loading.value = true
      cow
        .addProtocol(form.value)
        .then(res => {
          loading.value = false
          Message.success(res.message)
        })
        .catch(err => {
          loading.value = false
        })
    } else {
      console.log('error submit!', fields)
    }
  })
}
</script>

<style lang="scss" scoped>
.protocol {
  padding: 20px;
}
</style>
