<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="tmpAccountType" :rules="accountTypeRules" ref="accountTypeForm" label-width="100px">
        <el-form-item label="名称：" prop="name">
          <el-input v-model="tmpAccountType.name" @change="setAlias()"></el-input>
        </el-form-item>
        <el-form-item label="别名：" prop="code">
          <el-input v-model="tmpAccountType.code"></el-input>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import util from '../../common/util.js'
export default {
  props: {
    id: {
      type: [String, Number]
    }
  },
  data() {
    return {
      confirmLoading: false,
      // 验证规则
      accountTypeRules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur,change' },
          {
            pattern: /^([\u4E00-\u9FA50-9a-zA-Z]*)$/,
            message: '只能输入汉字、字母或数字',
            trigger: 'blur,change'
          }
        ],
        code: [
          { required: true, message: '请输入别名', trigger: 'blur,change' },
          {
            pattern: /^([a-zA-Z0-9]+)$/,
            message: '只能是数字或字母组成',
            trigger: 'blur,change'
          }
        ]
      },
      tmpAccountType: { name: '', code: '' }
    }
  },
  created() {
    this.$parent.activeMenu = '账号分类'
  },
  async mounted() {
    if (this.id === 'new') {
      return
    }
    let res = await axios.get(`/ui/accounttypes/${this.id}`)
    if (res.data.data) {
      this.tmpAccountType = { name: res.data.data.name, code: res.data.data.code }
    }
  },
  methods: {
    // 别名设置：取名称首字母
    setAlias() {
      if (!this.tmpAccountType.name.trim()) {
        return
      }
      this.tmpAccountType.code = window.util.getSpell(this.tmpAccountType.name, true)
    },
    async confirmClickHandler() {
      try {
        this.confirmLoading = true
        await util.validateForm(this.$refs['accountTypeForm'])
        let res = ''
        if (this.id === 'new') {
          res = await axios.post('/ui/accounttypes', this.tmpAccountType)
        } else {
          res = await axios.put(`/ui/accounttypes/${this.id}`, this.tmpAccountType)
        }
        this.confirmLoading = false
        util.showResponseMessage(res.data)
      } catch (e) {
        this.confirmLoading = false
        util.showErrorMessageBox(e)
      }
    }
  }
}
</script>
