<template>
  <div>
    <el-form :model="dataForm"
             label-width="100px"
             :rules="rules"
             ref="articleForm">
      <el-form-item label="类型">
        <el-select v-model="dataForm.type"
                   placeholder="请选择">
          <el-option v-for="item in options"
                     :key="item.id"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标题"
                    prop="name">
        <el-col :span="12">
          <el-input placeholder="标题"
                    v-model="dataForm.name"
                    clearable></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="内容"
                    prop="content">
        <el-col :span="12">
          <el-input placeholder="内容"
                    v-model="dataForm.content"
                    type="textarea"
                    clearable></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="状态"
                    prop="status">
        <el-col :span="12">
          <el-switch  v-model="dataForm.status"></el-switch >
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   @click="saveArticle()">保存</el-button>
        <el-button>重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import MavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'
import marked from 'marked'
export default {
  components: {
    'mavon-editor': MavonEditor.mavonEditor
  },
  data () {
    return {
      dataForm: {
        type: 1,
        title: '',
        content: '',
        status: 0,
        createdDate: ''

      },
      options: [{
        id: 1,
        name: '可回收垃圾',
        value: 1
      }, {
        id: 2,
        name: '厨余垃圾',
        value: 2
      }, {
        id: 3,
        name: '有害垃圾',
        value: 4
      }, {
        id: 4,
        name: '其他垃圾',
        value: 5
      }],
      url: '',
      file: [],
      rules: {
        name: { required: true, message: '请输入标题', trigger: 'change' }
      }
    }
  },
  created () {
    this.init()
  },
  methods: {
    init () {
      this.$nextTick(() => {
        this.$refs['articleForm'].resetFields()
      })
      let id = this.$route.params.id
      if (id) {
        this.$http({
          url: this.$http.adornUrl('/api/getarticle'),
          method: 'get',
          params: this.$http.adornParams({
            'id': id
          })
        }).then(({ data }) => {
          if (data && data.successful) {
            var content = data.resultValue
            this.dataForm = content
          }
        })
      }
    },
    // 上传之前
    beforeUploadHandle (file) {
      if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
        this.$message.error('只支持jpg、png、gif格式的图片！')
        return false
      }
    },
    // 上传成功
    successHandle (response) {
      if (response && response.code === 200) {
        this.article.cover = response.resource.url
        this.file = [response.resource]
        this.$message.success('上传成功！')
      }
    },
    // 移除上传文件
    handleRemove (file, fileList) {
      this.file = []
      this.article.cover = ''
    },
    // 保存文章
    saveArticle () {
      this.$refs['articleForm'].validate((valid) => {
        var textbookte = {
          'id': this.dataForm.id || undefined,
          'type': this.dataForm.type,
          'name': this.dataForm.name,
          'content': this.dataForm.content,
          'status': this.dataForm.status,
          'createdDate': this.dataForm.createdDate || undefined
        }
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/api/saveorupdate'),
            method: 'post',
            data: this.$http.adornData(textbookte)
          }).then(({ data }) => {
            if (data && data.successful) {
              this.$message.success('保存成功')
              // 跳转到list
              this.$router.push({ name: 'patternsen' })
            } else {
              this.$message.error(data.msg)
            }
          })
        } else {
          return false
        }
      })
    },
    mavonChangeHandle (context, render) {
      this.article.contentFormat = marked(context)
    }
  }
}
</script>
