<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="saveWord" :priv="ID === 'new' ? 'Search.DicManager.Add' : 'Search.DicManager.Edit'">
        <i class="fa fa-save"></i>保 存
      </el-button>
    </el-toolbar>
    <div class="detail-panel form-panel">
      <el-form :model="tmpSearchWord" status-icon ref="form" label-width="128px">
        <el-form-item label="搜索词：" prop="word" :verify="['NotNull']">
          <el-input v-model="tmpSearchWord.word"></el-input>
        </el-form-item>
        <el-form-item label="搜索次数：">
          <el-input-number v-model="tmpSearchWord.searchCount" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="置顶：">
          <el-switch v-model="tmpSearchWord.topFlag" active-value="Y" inactive-value="N">
          </el-switch>
        </el-form-item>
        <el-form-item label="置顶结束时间：" v-if="tmpSearchWord.topFlag=='Y'">
          <el-date-picker v-model="tmpSearchWord.topTime" type="datetime" placeholder="选择置顶时间" :picker-options="pickerOptions">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注：">
          <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="tmpSearchWord.memo">
          </el-input>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>

import util from '@src/common/util.js'
export default {
  props: {
    ID: {
      type: [Number, String],
      default: 0
    }
  },
  data() {
    return {
      btnLoading: false,
      tmpSearchWord: {}, //  临时存放用户表单填写的搜索词选项
      pickerOptions: {
        //  今天之前的日期不能点击
        disabledDate(time) {
          let timeSpace = time.getTime() < Date.now()
          return timeSpace
        }
      }
    }
  },
  created() {
    this.loadData()
  },
  methods: {
    async loadData() {
      if (this.ID && this.ID !== 'new') {
        let res = await axios.get(`/ui/searchword/${this.ID}`)
        if (res.data.status === 1) {
          this.tmpSearchWord = res.data.data
        } else {
          this.tmpSearchWord = {}
        }
      } else {
        this.tmpSearchWord = {}
      }
    },
    /* 提交通用词添加或编辑请求 */
    async saveWord() {
      this.btnLoading = true
      if (this.tmpSearchWord.topFlag !== 'Y') {
        this.tmpSearchWord.topTime = ''
        this.tmpSearchWord.topIcon = ''
      } else {
        this.tmpSearchWord.topIcon = 'fa fa-arrow-up'
        if (this.tmpSearchWord.topTime) {
          this.tmpSearchWord.topTime = util.formatDate(
            new Date(this.tmpSearchWord.topTime),
            'yyyy-MM-dd HH:mm:ss'
          )
        }
      }
      try {
        await util.validateForm(this.$refs['form'])
        let res = this.ID && this.ID !== 'new'
          ? await axios.put(`/ui/searchword/${this.ID}`, this.tmpSearchWord)
          : await axios.post('/ui/searchword', this.tmpSearchWord)
        this.btnLoading = false
        util.showResponseMessage(res.data)
      } catch (e) {
        this.btnLoading = false
        util.showErrorMessageBox(e)
      }
    }
  }
}
</script>
<style scoped>
</style>
