<template>
  <div class="Tab5DialogThree">
    <el-dialog
      :title="ruleForm.id ? '修改' : '新增'"
      :visible="isShow"
      @close="btnX()"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="年份：" prop="year">
          <el-input v-model="ruleForm.year" style="width:500px"></el-input>
        </el-form-item>
        <!-- 图片和附件 -->
        <el-form-item label="进度图片：">
          <i class="biaoshi"></i>
          <el-upload
            class="avatar-uploader"
            :action="baseURL + '/cms/site/img/upload'"
            :headers="{
              token,
            }"
            :show-file-list="false"
            :before-upload="beforethumbUpload"
            :on-success="upload_thumb_success"
          >
            <div v-if="ruleForm.thumb" class="imgdiv">
              <img
                style="width: 100%; height: 100%"
                :src="baseURL + ruleForm.thumb"
              />
              <i
                class="el-icon-circle-close"
                @click.stop="ruleForm.thumb = ''"
              ></i>
            </div>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <span>格式要求：支持png、jpg、gif和jpeg的图片格式；最大支持5M。</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="btnX">取 消</el-button>
        <el-button type="primary" @click="btnOk">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { videoDetailById3, siteImgSave } from '@/apis/tab5'

import axios from '@/utils/request'
export default {
  name: 'Tab5DialogThree',
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  components: {},
  data () {
    return {
      // 服务器前缀地址
      baseURL: '',
      token: '',
      ruleForm: {
        id: '',
        thumb: '',
        year: ''
      },
      rules: {
        year: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  watch: {

  },
  methods: {
    // 点击关闭
    btnX () {
      this.$emit('update:isShow', false)
      // 清空表单
      this.ruleForm = {
        id: '',
        thumb: '',
        year: ''
      }
      // 关闭验证
      this.$refs.ruleForm.resetFields()
    },
    // 点击确定
    async btnOk () {
      if (this.ruleForm.thumb === '') { return this.$message.warning('封面图片不能为空') }
      if (this.ruleForm.year.trim() === '') { return this.$message.warning('年份不能为空') }
      const obj = { ...this.ruleForm }
      const res = await siteImgSave(obj)
      if (res.code === 0) {
        this.$message.success('操作成功')
        // 通知父组件刷新页面
        this.$emit('refresh')
        this.btnX()
      } else return this.$message.warning(res.msg)
      // console.log(998, res)
    },
    // 上传图片
    beforethumbUpload (file) {
      // console.log(998, file)
      // 限制图片大小和格式
      const sizeOk = file.size / 1024 / 1024 < 5
      const typeOk =
        file.type === 'image/png' ||
        file.type === 'image/jpeg' ||
        file.type === 'image/gif'

      return new Promise((resolve, reject) => {
        if (!typeOk) {
          this.$message.error('照片格式有误！')
          reject(file)
        } else if (!sizeOk) {
          this.$message.error('照片大小超过5M！')
          reject(file)
        } else if (file.name.length > 32) {
          this.$message.error('照片名字不能超过32个字！')
          reject(file)
        } else {
          this.$message.success('上传成功')
          resolve(file)
        }
      })
    },
    upload_thumb_success (data) {
      // console.log('图片上传成功', data.data.urlPath)
      this.ruleForm.thumb = data.data.urlPath
    },
    // 通过id获取详情---让父组件调用
    async videoDetailById3 (id) {
      const res = await videoDetailById3(id)
      this.ruleForm = res.data
      // console.log(998, res)
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {
    // 获取服务器前缀地址
    this.baseURL = axios.defaults.baseURL
    // 获取用户token
    this.token = localStorage.getItem('CQLJXU_token')
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {},
  beforeCreate () {}, // 生命周期 - 创建之前
  beforeMount () {}, // 生命周期 - 挂载之前
  beforeUpdate () {}, // 生命周期 - 更新之前
  updated () {}, // 生命周期 - 更新之后
  beforeDestroy () {}, // 生命周期 - 销毁之前
  destroyed () {}, // 生命周期 - 销毁完成
  activated () {} // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang='less' scoped>
.biaoshi::before {
  top: 1px;
}
.biaoshi2::before {
  top: -10px;
  left: -64px;
}
/deep/.el-upload-list {
  width: 500px;
}
/deep/.el-icon-plus {
  border: 1px dashed #ccc;
}
.avatar-uploader .el-upload {
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #3e5eb3;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
/deep/.el-icon-circle-close {
  font-size: 20px;
}
.imgdiv {
  max-width: 400px;
}
</style>
