<template>
  <div>
    <el-dialog :title="!tmpHotWord.ID ? '新建热词链接' : '编辑热词链接'" :visible.sync="isShow" class="hot-word-dialog">
      <el-form :model="tmpHotWord" :rules="hotWordRules" ref="hotWordForm" label-width="100px">
        <el-form-item prop="hotWord" label="热词：" :verify="['NotNull','Length<50']">
          <el-input type="text" v-model="tmpHotWord.hotWord" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item prop="linkUrl" label="链接地址：" :verify="['NotNull','Length<200']">
          <el-input type="text" v-model="tmpHotWord.linkUrl" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item prop="linkTarget" label="打开方式：" :verify="['Length<200']">
          <el-radio-group v-model="tmpHotWord.linkTarget">
            <el-radio :label="'_blank'">新窗口</el-radio>
            <el-radio :label="'_self'">当前窗口</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item prop="linkAlt" label="链接说明：" :verify="['NotNull','Length<200']">
          <el-input type="text" v-model="tmpHotWord.linkAlt" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="confirmHotWordClick">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import util from '@src/common/util.js'

export default {
  data() {
    return {
      // 热词列表form表单验证
      hotWordRules: {
        linkUrl: [
          {
            validator: (rule, value, callback) => {
              if (value === 'http://') {
                callback(new Error('请输入链接地址'))
              } else if (!value.startsWith('http://')) {
                callback(new Error('请输入以 http:// 开头的链接地址'))
              } else {
                callback()
              }
            },
            trigger: 'blur',
            required: true
          }
        ]
      }
    }
  },
  computed: {
    isShow: {
      get() {
        return this.addHotWordDialog
      },
      set(val) {
        this.$emit('update:addHotWordDialog', val)
      }
    }
  },
  methods: {
    // 验证热词列表事件
    async confirmHotWordClick() {
      try {
        await util.validateForm(this.$refs['hotWordForm'])
      } catch (e) {
        util.showErrorMessageBox(e)
        return
      }

      if (!this.tmpHotWord.hotWord || !this.tmpHotWord.hotWord.trim()) {
        util.showErrorMessageBox('热词不能为空 ！')
        return
      }

      if (this.tmpHotWord.linkUrl === 'http://' || !this.tmpHotWord.linkUrl.startsWith('http://')) {
        util.showErrorMessageBox('请输入以 http:// 开头的链接地址')
        return
      }
      if (!this.tmpHotWord.ID) {
        let res = await axios.post('/ui/hotwords', this.tmpHotWord)
        if (res.data.status === 1) {
          this.isShow = false
          this.$emit('callback')
          util.showMessage(res)
        } else {
          util.showErrorMessageBox(res.data.message)
        }
      } else {
        let res = await axios.put(`/ui/hotwords/${this.tmpHotWord.ID}`, this.tmpHotWord)
        if (res.data.status === 1) {
          this.isShow = false
          this.$emit('callback')
          util.showMessage(res)
        } else {
          util.showErrorMessageBox(res.data.message)
        }
      }
    }
  },

  props: {
    tmpHotWord: {
      type: Object,
      required: true
    },
    addHotWordDialog: {
      type: Boolean,
      required: true
    }
  }
}
</script>

<style>
/* 在一般情况下dialog宽为450像素，在逻辑分辨率480像素以下dialog宽为96% */
.hot-word-dialog .el-dialog {
  width: 450px;
}
@media (max-width: 480px) {
  .hot-word-dialog .el-dialog {
    width: 96%;
  }
}
</style>
<style scoped>

</style>
