<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' ? 'DimensionManage.Add':'DimensionManage.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="tmpDimens" :rules="dimensRules" ref="dimensForm" label-width="100px">
        <el-form-item label="名称：" prop="name">
          <el-input v-model="tmpDimens.name" @change="setAlias()"></el-input>
        </el-form-item>
        <el-form-item label="别名：" prop="code">
          <el-input v-model="tmpDimens.code"></el-input>
        </el-form-item>
        <el-form-item label="是否可用：" prop="isOpen">
          <el-switch v-model="tmpDimens.isOpen" active-value="Y" inactive-value="N"></el-switch>
        </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,
      // 验证规则
      dimensRules: {
        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: /^([\w]+)$/,
            message: '只能是数字或字母组成',
            trigger: 'blur,change'
          }
        ]
      },
      tmpDimens: {name: '', code: '', isOpen: 'Y'}
    }
  },
  async mounted() {
    if (this.ID === 'new') {
      return
    }
    let res = await axios.get(`/ui/dimens/${this.ID}`)
    this.tmpDimens = res.data.data
  },
  methods: {
    // 别名设置：取名称首字母
    setAlias() {
      if (this.tmpDimens.code.trim()) {
        return
      }
      this.tmpDimens.code = window.util.getSpell(
        this.tmpDimens.name,
        true
      )
    },
    // 验证维度信息
    async confirmClickHandler() {
      try {
        this.confirmLoading = true
        await util.validateForm(this.$refs['dimensForm'])
        let res = ''
        if (this.ID === 'new') {
          res = await axios.post('/ui/dimens', this.tmpDimens)
        } else {
          res = await axios.put(`/ui/dimens/${this.ID}`, this.tmpDimens)
        }
        this.confirmLoading = false
        util.showResponseMessage(res.data)
      } catch (e) {
        this.confirmLoading = false
        util.showErrorMessageBox(e)
      }
    }
  }
}
</script>
<style scoped>
</style>
