<template>
  <div class="app-container">
    <div class="btn-group">
      <div class="pull-right">
        <el-button type="primary" :loading="saveLoading" @click="save">{{ $t('ec.dialog.save') }}</el-button>
        <el-button type="default" @click="returnBack">{{ $t('ec.dialog.close') }}</el-button>
      </div>
    </div>
    <el-form ref="form" v-loading="loading" :model="formItem" :rules="rules" :label-width="'110px'" />
  </div>
</template>

<script>

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

export default {
  name: 'ModulesMessageMessageSendLogForm',
  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(this.$t('ec.message.saveSuccess'))
        })
      } else {
        this.saveLoading = true
        save(this.formItem).then(res => {
          this.saveLoading = false
          this.returnBack()
          this.$notify.success(this.$t('ec.message.saveSuccess'))
        })
      }
    },
    returnBack() {
      this.$store.dispatch('tagsView/delView', this.$route).then(({ visitedViews }) => {
        this.$router.push(`/smsSendLogView`)
      })
    }
  }
}
</script>

<style scoped>

</style>
