<template>
  <div class="protocol" v-loading="loading">
    <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 label="服务费合同模板上传" prop="shopFeeContractUrl">
        <el-upload
        style="width: 50%;"
          :action="uploadUrl"
          :on-success="onSuccess"
          ref="upload"
          :headers="{ 'X-AIYANGNIU-SIGNATURE': token }"
          :on-exceed="handleExceed"
          v-model:file-list="fileList"
          :on-preview="handlePreview"
          :limit="1"
        >
          <el-button type="primary">点击上传</el-button></el-upload
        >
      </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, onMounted } from 'vue'
import BasicEditor from '@/components/wangEditor/BasicEditor.vue'
import { cow } from '@/request/api'
import { useRouter } from 'vue-router'
import { genFileId } from 'element-plus'
import api from '@/request/apiConfig' //真实接口配置
const router = useRouter()
const token = localStorage.aynUserToken
const uploadUrl = api.apiManager + '/manager/images/upload'
const fileList = ref([])

const onSuccess = (res, file) => {
  console.log(res)
  fileList.value = []
  fileList.value.push(file)
  form.value.shopFeeContractUrl = res.data
}
const upload = ref()
const handleExceed = (files, fileList) => {
  upload.value.clearFiles()
  const file = files[0]
  file.uid = genFileId()
  upload.value.handleStart(file)
  upload.value.submit()
}
const handlePreview = (file) => {
  console.log(file.url)
  window.open(file.url)
}
const loading = ref(false)
const editotTool = ref()
const ruleFormRef = ref()
const form = ref({})

const getProtocol = async () => {
  loading.value = true
  await cow.getProtocol().then(res => {
    if (res.code == 101 && res.data) {
      form.value = res.data
      editotTool.value.valueHtml = res.data.agreeUnlockInfo
      fileList.value.push({
        name: res.data.shopFeeContractUrl,
        url: res.data.shopFeeContractUrl,
      })
    }
  })
  loading.value = false
}

onMounted(() => {
  getProtocol()
})

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

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('更新成功')
          getProtocol()
        })
        .catch(err => {
          loading.value = false
        })
    } else {
      console.log('error submit!', fields)
    }
  })
}
</script>

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