<template>
  <div class="flexCard">
    <!-- 表单 ref 和 prop绑定  用于重置表单 -->
    <el-card class="form-container" shadow="never" >
      <div class="form">
        <el-form ref="resetFormData" :model="form" label-width="auto" size="large" >
          <div>
            <el-form-item label="添加活动图片">
              <ImgSingleUpload @update:goodImgArr="form.image = $event"/>
            </el-form-item>
          </div>
          <el-form-item label="资讯标题">
            <el-input v-model="form.title" maxlength="30" minlength="2" show-word-limit style="width: 260px;" />
          </el-form-item>
          <el-form-item label="文章发布时间">
            <el-date-picker
                v-model="form.publishTime"
                type="date"
                placeholder="选择发布日期"
                value-format="YYYY-MM-DD"
                :disabled-date="disabledDate"
                size="large"
            />
          </el-form-item>
          <el-form-item label="公众号链接">
            <el-input v-model="form.url" show-word-limit style="width: 260px;" />
          </el-form-item>
          <div class="btnBox">
            <el-form-item>
              <el-button type="primary" @click="onSubmit" :disabled="saveBtn">保存</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="resetForm">重置</el-button>
            </el-form-item>
          </div>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, inject } from 'vue';
import { SuccessInfo, WarnInfo, CommInfo } from '@/utils/messageInfo';   //封装ElMessage提示
import myAxios from "@/api/myAxios";
import { saveBtn, invInput } from '@/utils/entityProduct/globalVar';
import ImgSingleUpload from "@/utils/imgUpload/ImgSingleUpload.vue";


const resetFormData = ref()
const reload: any = inject("reload")  //导入组件刷新
const form : any = ref({   //添加实体类商品的表单
  title: '',
  image: '',
  publishTime: '',
  url: '',
})

onMounted(() => {

})

const disabledDate = (time : any ) => {
  // console.log('time---->',time.getTime())
  return time.getTime() > Date.now()
}

//提交表单
const onSubmit = async () => {
  console.log('表单--->',form.value)
  const values = Object.values(form.value );
  // 使用some()方法来检查是否有任何值为空
  if (values.some((value: any) => value === null || value === undefined || value === '' )) {
    WarnInfo('请检查表单数据是否完整填写')
    return;  //空返回结束函数
  }
  const res = await myAxios.post('/article/add', {
    title: form.value.title,
    image: form.value.image,
    publishTime: form.value.publishTime.replace(/(.+?)\-(.+?)\-(.+)/,"$1年$2月$3日"),
    url: form.value.url
  })
  if (res.data.code === 1) {
    SuccessInfo('提交成功')
    form.value = {}  //12.18改
    reload() //上传完后重置表单
  } else {
    WarnInfo('服务错误')
    return;
  }
}

//清除表单
const resetForm = () => {
  resetFormData.value.resetFields()
}

</script>

<style scoped>
.flexCard {
  display: flex;
  height: 100%;
  min-width: 800px;
  margin: 0 auto;
}


.form-container {
  width: 700px;
  margin: 0 auto;
}
.form {
  /* width: 500px; */
  margin: 0 auto;
}


.btnBox {
  display: flex;
  justify-content: space-around;
}
</style>