<template>
  <el-dialog v-model.trim="addShow" :title="title" width="620px" draggable :close-on-click-modal="false">
    <el-form :model="form" ref="ruleFormRef" label-width="88px" :rules="formRules" style="padding: 0 0px 0px 0;">
      <el-form-item label="标题：" prop="carName">
        <el-input v-model.trim="form.carName" placeholder="请输入标题" clearable maxlength="30" />
      </el-form-item>
      <el-form-item label="图片：" prop="carUrl">
        <uploadCom :maxLimit="1" listType="picture-card" :acceptTypeList="['.jpg', '.jpeg', '.png']" width="120px"
          height="120px" :autoUpload="true" :successResultCallBack="successResultCallBackFn" v-model:fileList="fileList">
          <template v-slot:default>
            <el-icon size="48" color="#aaa">
              <Plus />
            </el-icon>
          </template>
        </uploadCom>

      </el-form-item>
      <el-form-item label="跳转链接：" prop="openUrl">
        <el-input v-model.trim="form.openUrl" 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"
import { apiBmCarouselSetsById, apiBmCarouselSetsAddorupdate } from "http/api/config"
 
const emits = defineEmits(['send'])
const addShow = ref(false)
let title: any = ref("新增")
const ruleFormRef: any = ref()
const form = reactive({
  carName: '',
  carUrl: '',
  openUrl: '',
  id: ''
})

const fileList: any = ref([])
// const validatorLink = (rule: any, value: any, callback: any) => {
//   if (linkReg.test(value)) {
//     callback()
//   } else {
//     callback(new Error("请输入正确格式的跳转链接"))
//   }
// }
const formRules = reactive<FormRules<any>>({
  carName: [
    { required: true, message: '请输入标题', trigger: 'blur' },
  ],
  carUrl: [
    { required: true, message: '请上传图片', trigger: 'blur' },
  ],
  // openUrl: [
  //   {
  //     required: true,
  //     message: '请输入跳转链接',
  //     trigger: 'blur',
  //   },
  //   {
  //     validator: validatorLink,
  //     trigger: 'blur'
  //   }
  // ],

})

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

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

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

const edit = (id: any) => {
  title.value = "编辑"
  addShow.value = true
  form.id = id
  initApiBmCarouselSetsById(id)
}

const initApiBmCarouselSetsById = async (id: any) => {
  const res: any = await apiBmCarouselSetsById(id)
  Object.assign(form, res.data)
  const fileItem = {
    url: res.data.carUrl,
    name: ""
  }
  fileList.value = [fileItem]
  // console.log("res-apiBmCarouselSetsById22222", fileList.value)

}
const successResultCallBackFn = (val: any) => {
  console.log("successResultCallBackFnval", val)
  form.carUrl = val.msg
}

const updateFn = async () => {
  let params = Object.assign({}, form)
  const res = await apiBmCarouselSetsAddorupdate(params)
  console.log("apiBmCarouselSetsAddorupdate", res)
  ElMessage({
    type: 'success',
    message: title.value + '成功'
  })
  addShow.value = false
  emit("send")
}

defineExpose({
  open,
  edit
})

</script>

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