<template>
  <div class="layout-no-flex">
    <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 type="button" class="el-button el-button--default save" :loading="confirmLoading" @click="confirmClickHandler" :priv="this.id === 'new' ? 'SocialMedia.AccountType.Add':'SocialMedia.AccountType.Edit'"><span><i class="fa fa-save"></i>保存</span></el-button>
    </el-toolbar>
    <div class="detail-panel">
      <div class="panel-header">
        <div class="title">{{id === 'new' ? '新建标签' : '修改标签'}}</div>
      </div>
      <el-form :model="tmpFansTag" :rules="fansTagRules" ref="fansTagForm" label-width="100px">
        <el-form-item label="标签名称：" prop="tagName">
          <el-input v-model="tmpFansTag.tagName"></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]
    }
  },
  data() {
    return {
      confirmLoading: false,
      // 验证规则
      fansTagRules: {
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur,change' },
          {
            pattern: /^([\u4E00-\u9FA50-9a-zA-Z]*)$/,
            message: '只能输入汉字、字母或数字',
            trigger: 'blur,change'
          }
        ]
      },
      tmpFansTag: {
        tagName: this.$route.query.tagName,
        aid: this.$route.query.aid
      }
    }
  },
  created() {
    this.$parent.activeMenu = '粉丝管理'
  },
  methods: {
    async confirmClickHandler() {
      try {
        this.confirmLoading = true
        await util.validateForm(this.$refs['fansTagForm'])
        let res = ''
        if (this.id === 'new') {
          res = await axios.post('/ui/fanstags', this.tmpFansTag)
        } else {
          res = await axios.put(`/ui/fanstags/${this.id}`, this.tmpFansTag)
        }
        this.confirmLoading = false
        util.showResponseMessage(res.data)
      } catch (e) {
        this.confirmLoading = false
        util.showErrorMessageBox(e)
      }
    }
  }
}
</script>
