<template>
  <div class="dicttype-form">
    <el-form ref="ruleForm" :model="ruleForm" size="small" label-width="100px">
      <el-form-item label="名称" prop="name" :rules="[{
                    required: true, message: '名称不能为空', trigger: 'blur'
                  },{min: 2, max: 20, message: '长度在2 到 20 个字符', trigger: 'blur'}]">
        <el-input maxlength="20" clearable v-model="ruleForm.name" size="small" placeholder="请输入名称"/>
      </el-form-item>
      <el-form-item label="标识符" prop="nid" :rules="[{
                    required: true, message: '标识符不能为空', trigger: 'blur'
                  },{min: 1, max: 50, message: '长度在1 到 50 个字符', trigger: 'blur'}]">
        <el-input maxlength="50" clearable v-model="ruleForm.nid" size="small" placeholder="请输入标识符"/>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number size="small" v-model="ruleForm.sort"></el-input-number>
      </el-form-item>
    </el-form>
    <div style="text-align: center">
      <el-button icon="el-icon-document-add" :loading="addBtnLoading" type="primary" size="medium" @click="submitForm('ruleForm')">提交
      </el-button>
      <el-button icon="el-icon-back" v-if="ruleForm.id" style="margin-left: 15px" size="medium"
                 @click="onBack('ruleForm')">返回
      </el-button>
    </div>
  </div>
</template>

<script>
  import dicttypeApi from '@/api/system/dicttype'

  export default {
    name: "dicttype",
    data() {
      return {
        addBtnLoading: false,
      }
    },
    props: {
      ruleForm: {
        type: Object,
        default() {
          return {};
        }
      }
    },
    created() {
    },
    mounted() {
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let method = 'dicttypeAdd'
            if (this.ruleForm.id) {
              method = 'dicttypeEdit'
            }
            this.addBtnLoading = true
            dicttypeApi[method](this.ruleForm).then(res => {
              this.addBtnLoading = false
              this.$refs[formName].resetFields()
              this.$message({
                type: 'success',
                message: this.ruleForm.id ? '修改成功' : '添加成功'
              })
              if (this.ruleForm.id) {
                this.$emit('submit',2)
                this.$emit('onBack')
              }else{
                this.$emit('submit',1)
              }
            }).catch(e => {
              this.addBtnLoading = false
            })
          }
        })
      },
      onBack() {
        this.$refs['ruleForm'].resetFields()
        this.$emit('onBack')
      }
    }
  }
</script>

<style lang="less">
  .dicttype {

  }
</style>
