<template>
  <el-dialog v-model.trim="addShow" :title="title" width="30%" draggable  :close-on-click-modal="false" >
    <el-form :model="form" ref="ruleFormRef" label-width="88px" :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="picture">
 
          <uploadCom :maxLimit="3" listType="picture-card" :acceptTypeList="['.jpg','.jpeg','.png']" width="72px" height="72px">
            <template v-slot:default>
              <el-icon size="48" color="#aaa"><Plus /></el-icon>
            </template>
          </uploadCom>

 
      </el-form-item>
      <el-form-item label="跳转链接：" prop="linkUrl">
        <el-input v-model.trim="form.linkUrl" clearable />
      </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 uploadCom from "components/com/uploadCom.vue"
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { linkReg } from "utils/reg"
const emits = defineEmits(['send'])
const addShow = ref(false)
let title = ref("新增")
const ruleFormRef: any = ref()
const form = reactive({
  title: '',
  picture: '',
  linkUrl: ''
})
// const validatorLink = (rule: any, value: any, callback: any) => {
//   if (linkReg.test(value)) {
//     callback()
//   } else {
//     callback(new Error("请输入正确格式的跳转链接"))
//   }
// }
const formRules = reactive<FormRules<any>>({
  title: [
    { required: true, message: '请输入标题', trigger: 'blur' },
  ],
  picture: [
    { required: true, message: '请上传图片', trigger: 'blur' },
  ],
  // linkUrl: [
  //   {
  //     required: true,
  //     message: '请输入跳转链接',
  //     trigger: 'blur',
  //   },
  //   {
  //     validator: validatorLink,
  //     trigger: 'blur'
  //   }
  // ],

})

const publishFn = () => {
  if (!ruleFormRef) return
  ruleFormRef.value.validate((valid: any) => {
    if (valid) {
      if (form.linkUrl&&linkReg.test(form.linkUrl)) {
        ElMessage({
          type:'warning',
          message:"请输入正确格式的跳转链接"
        })
      }
      console.log('submit!')
    } else {
      console.log('error submit!')
      return false
    }
  })
}

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

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

const edit = () => {
  title.value = "编辑"
  addShow.value = true
}

defineExpose({
  open,
  edit
})

</script>

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