<template>
  <div class="app-container">
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      :visible.sync="visible"
      top="4%"
    >
      <el-form ref="dialogForm" v-loading="loading" :model="formItem" label-width="120px">
        <el-row :gutter="15" />
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">{{ $t('ec.table.close') }}</el-button>
        <el-button v-if="method !== 'view'" type="primary" :loading="saveLoading" @click="save">{{ $t('ec.table.confirm') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

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

export default {
  name: 'VueLogOprationLogForm',
  components: {},
  mixins: [...mixin],
  data() {
    return {
      visible: false,
      method: '',
      title: '',
      loading: false, // 页面加载遮罩
      saveLoading: false, //  保存加载
      formItem: {
      }
    }
  },
  created() {
  },
  methods: {
    init(method, id) {
      this.method = method
      if (method === 'add') {
        this.title = this.$t('ec.table.add')
      } else if (method === 'edit') {
        this.title = this.$t('ec.table.edit')
      } else if (method === 'view') {
        this.title = this.$t('ec.table.view')
      }
      this.visible = true
      this.loading = false
      this.$nextTick(() => {
        this.$refs.dialogForm.resetFields()
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          getById(id).then(res => {
            Object.assign(this.formItem, res.data)
            // this.formItem = res.data
            // this.inputForm = this.recover(this.inputForm, res.data)
            this.loading = false
          })
        }
      })
    },
    save() {
      let validFlag
      this.$refs.dialogForm.validate(valid => { validFlag = valid })
      if (!validFlag) {
        return
      }
      this.saveLoading = true
      if (this.method === 'edit') {
        update(this.formItem.id, this.formItem).then(res => {
          this.$notify.success(this.$t('ec.message.saveSuccess'))
          this.$emit('refreshDataList')
          this.visible = false
          this.saveLoading = false
        })
      } else {
        save(this.formItem).then(res => {
          this.$notify.success(this.$t('ec.message.saveSuccess'))
          this.$emit('refreshDataList')
          this.visible = false
          this.saveLoading = false
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
