<template>
  <div class="app-container">
    <el-page-header :content="isEdit?'编辑文本素材':'添加文本素材'" @back="goBack" />
    <el-card class="form-container" shadow="never">
      <el-form ref="weChatTextFrom" :model="text" :rules="rules" label-width="150px">
        <el-form-item label="标题:" prop="title">
          <el-input v-model="text.title" />
        </el-form-item>
        <el-form-item label="内容:" prop="content">
          <el-input v-model="text.content" type="textarea" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('weChatTextFrom')">提交</el-button>
          <el-button v-if="!isEdit" type="info" @click="resetForm('weChatTextFrom')">重置</el-button>
          <el-button @click="goBack">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import { getText, updateText, createText } from '@/api/wechat/text'
const defaultText = {
  id: '',
  title: '',
  content: '',
  type: ''
}
export default {
  name: 'WeChatTextForm',
  props: {
    // type 类型： 1采美，2呵呵商城
    type: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      rules: {
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }]
      },
      text: Object.assign({}, defaultText),
      isEdit: false
    }
  },
  watch: {
    $route(route) {
      this.getFormData()
    }
  },
  created() {
    this.getFormData()
  },
  methods: {
    goBack() {
      // 调用全局挂载的方法,关闭当前标签页
      this.$store.dispatch('tagsView/delView', this.$route)
      // 返回上一步路由，返回上一个标签页
      this.$router.go(-1)
    },
    getFormData() {
      if (this.$route.query.id) {
        this.text.id = this.$route.query.id
        this.isEdit = true
        getText(this.text.id).then(response => {
          this.text.title = response.data.title
          this.text.content = response.data.content
        })
      } else {
        this.text.id = ''
        this.isEdit = false
        this.text = Object.assign({}, defaultText)
      }
      this.text.type = this.type
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.text = Object.assign({}, defaultText)
      this.getFormData()
    },
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        console.log(this.text)
        if (valid) {
          this.$confirm('是否提交数据', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            const self = this
            if (this.isEdit) {
              updateText(this.$route.query.id, this.text).then(response => {
                this.$message({
                  message: '修改成功',
                  type: 'success',
                  duration: 1000
                })
                self.goBack()
              })
            } else {
              createText(this.text).then(response => {
                this.$refs[formName].resetFields()
                this.resetForm(formName)
                this.$message({
                  message: '提交成功',
                  type: 'success',
                  duration: 1000
                })
                self.goBack()
              })
            }
          })
        } else {
          this.$message({
            message: '验证失败',
            type: 'error',
            duration: 1000
          })
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
