<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
        <el-form-item label="banner" prop="mainPicture">
            <el-upload
                    :limit="1"
                    ref="upload1"
                    class="upload-demo"
                    :on-change="handleChange"
                    action="werwer"
                    :name="'file'"
                    :auto-upload="false"
                    :before-upload="beforeAvatarUpload"
                    :before-remove="beforeRemove"
                    list-type="picture-card">
                <el-button>{{"上传文件"}}</el-button>
                <div slot="tip" class="" style="width: 272px;font-size: 13px">{{"支持：.jpg/png"}}</div>
            </el-upload>
        </el-form-item>
        <el-form-item label="案例标题" prop="caseTitle">
          <el-input v-model="dataForm.caseTitle" placeholder="请输入40字以内" maxlength="40"></el-input>
        </el-form-item>
          <el-form-item label="摘要" prop="typicalAbstract">
          <el-input  v-model="dataForm.typicalAbstract" placeholder="文本输入框，限制200字内" maxlength="200"
                     type="textarea" :rows="5"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button type="primary" @click="dataFormSubmitHandle()">{{"发布案例"}}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      fileList: [],
      visible: false,
      dataForm: {
        id: '',
        caseTitle: '',
        caseNumber: '',
        state: '',
        mainPicture: '',
        typicalAbstract: '',
        revision: '',
        delFlag: '',
        createdBy: '',
        createdTime: '',
        updatedBy: '',
        updatedTime: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        caseTitle: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        caseNumber: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        state: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        mainPicture: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        typicalAbstract: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        revision: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        delFlag: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createdBy: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createdTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updatedBy: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updatedTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {

    handleChange (file) {
      this.beforeAvatarUpload(file)
    },

    beforeRemove (file, fileList) {
      console.log(file)
      file = null
      this.file1 = file
      this.dataForm.mainPicture = null
      console.log(file)
      // this.canUpload = false;
    },
    // 限制上传类型和大小
    beforeAvatarUpload (file) {
      var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
      // alert(testmsg)
      const extension = testmsg === 'png'
      const extension2 = testmsg === 'jpg'
      if (!extension && !extension2) {
        this.$message.error('请上传jpg或png格式图片')
        this.$refs.upload1.clearFiles()
        file = null
        this.dataForm.mainPicture = null
      } else {
        this.dataForm.mainPicture = file.name
        this.canUpload = true
      }
      this.file1 = file
    },

    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/news/typicalcase/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/typicalcase/addcase', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
