<template>
  <div class="app-container calendar-list-container">
    <div style='margin-left:50px;color: red;font-size: x-small; '>
      图片建议尺寸 750*1200 像素
    </div>
    <div status-icon label-position="left"  label-width="100px" style='margin-left:50px;'>
      <el-tooltip content="上传投稿宣传图片" placement="top-start">
        <el-upload
          class="ad-avatar-uploader"
          v-model="dataForm.image"
          :action="fileImgUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <template v-if="dataForm.image">
            <img :src="dataForm.image" class="ad-avatar" width="100%" height="100%">
          </template>
          <i v-else class="el-icon-plus ad-avatar-uploader-icon"></i>
        </el-upload>
      </el-tooltip>

      <el-button type="primary" round @click="createData" style="margin-top: 10px;margin-left: 90px;">保存</el-button>
    </div>

  </div>
</template>

<style>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 200px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
  }
</style>

<script>

import { createContribute } from '@/api/questions'
import waves from '@/directive/waves' // 水波纹指令

export default {
  name: 'contribute',
  directives: {
    waves
  },
  data() {
    return {
      dataForm: {
        image: undefined
      },
      dialogStatus: '',
      fileImgUrl: this.upLoadUrl,
      downloadLoading: false
    }
  },
  created() {

  },
  methods: {
    createData() {
      console.log(this.dataForm.image)
        if (this.dataForm.image) {
          createContribute(this.dataForm).then(response => {
            this.$notify({
              title: '成功',
              message: '提交成功',
              type: 'success',
              duration: 2000
            })
          })
        } else {
          this.$notify({
            title: '警告提示',
            message: '请选择上传图片',
            type: 'warning'
          })
        }
    },

    handleAvatarSuccess(res) {
      console.log(res.data.url)
      this.dataForm.image = res.data.url
    },

    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>
<style>
  .ad-avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 250px;
    height: 400px;
  }
  .ad-avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .ad-avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 500px;
    text-align: center;
  }
  .ad-avatar {
    display: block;
  }
</style>
