<template>
  <div class="app-container">
    <div class="btn-group">
      <div class="pull-right">
        <el-button type="primary" :loading="saveLoading" @click="save">保存</el-button>
        <el-button type="default" @click="returnBack">关闭</el-button>
      </div>
    </div>
    <el-form ref="form" v-loading="loading" :model="formItem" :rules="rules" :label-width="'110px'">
      <el-form-item
        label="模版名称"
        prop="name"
        :rules="[
          {required: true, message: '模版名称不能为空', trigger: 'blur'}
        ]"
      >
        <el-input v-model="formItem.name" type="text" placeholder="模版名称" />
      </el-form-item>
      <el-form-item
        label="模版主题"
        prop="templateSubject"
        :rules="[
          {required: true, message: '模版内容不能为空', trigger: 'blur'}
        ]"
      >
        <el-input v-model="formItem.templateSubject" type="textarea" :rows="5" :autosize="{minRows: 1, maxRows: 5}" />
      </el-form-item>
      <el-form-item
        label="模版内容"
        prop="templateContent"
        :rules="[
          {required: true, message: '模版内容不能为空', trigger: 'blur'}
        ]"
      >
        <el-input v-model="formItem.templateContent" type="textarea" :rows="5" :autosize="{minRows: 1, maxRows: 5}" />
      </el-form-item>
      <el-form-item
        label="备注信息"
        prop="remarks"
        :rules="[
          {required: false, message: '备注信息不能为空', trigger: 'blur'}
        ]"
      >
        <el-input v-model="formItem.remarks" type="textarea" :rows="5" :autosize="{minRows: 1, maxRows: 5}" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import mixin from 'ecip-web/utils/common-mixin'
import { getById, save, update } from 'ecip-web/api/vue/push/pushTemplateApi'

export default {
  name: 'ModulesPushTemplateForm',
  components: {},
  mixins: [...mixin],
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: false, // 页面加载遮罩
      saveLoading: false, //  保存加载
      id: this.$route.params.id,
      formItem: {
      },
      rules: {}
    }
  },
  created() {
    this.getData()
  },
  methods: {
    getData() {
      if (this.id) { //  编辑
        this.loading = true //  显示遮罩层
        getById(this.id).then(res => {
          this.formItem = res.data
          this.loading = false //  关闭遮罩层
        })
      } else {
        this.formItem = {
        }
      }
    },
    save() {
      let validFlag
      this.$refs.form.validate(valid => { validFlag = valid })
      if (!validFlag) {
        return
      }
      if (this.isEdit) {
        this.saveLoading = true
        update(this.id, this.formItem).then(res => {
          this.saveLoading = false
          this.returnBack()
          this.$notify.success('保存成功')
        })
      } else {
        this.saveLoading = true
        save(this.formItem).then(res => {
          this.saveLoading = false
          this.returnBack()
          this.$notify.success('保存成功')
        })
      }
    },
    returnBack() {
      this.$store.dispatch('tagsView/delView', this.$route).then(({ visitedViews }) => {
        this.$router.push(`/pushTemplateView`)
      })
    }
  }
}
</script>

<style scoped>

</style>
