<template>
  <div class="layout-no-cols">
    <el-toolbar>
      <el-button @click="$router.back()" type="primary" size="medium" round class="shadow-primary">
        <i class="fa fa-chevron-left"></i> 返回上页
      </el-button>
      <span class="button-separator"></span>
      <el-button :loading="btnLoading" @click="saveGeneralWord" :priv="ID === 'new' ? 'Search.DicManager.AddGeneralWord' : 'Search.DicManager.EditGeneralWord'">
        <i class="fa fa-save"></i>保 存
      </el-button>
    </el-toolbar>
    <div class="detail-panel form-panel">
      <el-form :model="tmpGeneralWord" status-icon ref="form" label-width="128px">
        <el-form-item label="根词：" prop="rootWord" :verify="['NotNull','Length<30']">
          <el-input v-model="tmpGeneralWord.rootWord"></el-input>
        </el-form-item>
        <el-form-item label="同义词：">
          <el-input v-model="tmpGeneralWord.synWords" type="textarea" :rows="2"></el-input>
        </el-form-item>
        <el-form-item label="名词解释：">
          <el-input v-model="tmpGeneralWord.info" type="textarea" :rows="2"></el-input>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>

import util from '@src/common/util.js'
export default {
  props: {
    ID: {
      type: [String, Number],
      default: 0
    }
  },
  data() {
    return {
      btnLoading: false,
      tmpGeneralWord: {} //  临时存放用户表单填写的通用词选项
    }
  },
  created() {
    this.loadData()
  },
  methods: {
    async loadData() {
      if (this.ID && this.ID !== 'new') {
        let res = await axios.get(`/ui/generalword/${this.ID}`)
        if (res.data.status === 1) {
          this.tmpGeneralWord = res.data.data
        } else {
          this.tmpGeneralWord = {}
        }
      } else {
        this.tmpGeneralWord = {}
      }
    },
    /* 提交通用词添加或编辑请求 */
    async saveGeneralWord() {
      try {
        await util.validateForm(this.$refs['form'])
        this.btnLoading = true
        let res = this.ID && this.ID !== 'new'
          ? await axios.put(
            `/ui/generalword/${this.ID}`,
            this.tmpGeneralWord
          )
          : await axios.post('/ui/generalword', this.tmpGeneralWord)
        this.btnLoading = false
        util.showResponseMessage(res.data)
      } catch (e) {
        this.btnLoading = false
        util.showErrorMessageBox(e)
      }
    }
  }
}
</script>
<style>
</style>
