<template>
  <div class="dict-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: 50, message: '长度在2 到 50 个字符', trigger: 'blur'}]">
        <el-input maxlength="50" clearable v-model="ruleForm.name" size="small" placeholder="请输入名称"/>
      </el-form-item>
      <el-form-item label="所属分类" prop="typeId" :rules="{
                    required: true, message: '所属分类不能为空', trigger: 'change'
                  }">
        <el-select filterable clearable v-model="ruleForm.typeId" size="small" placeholder="-请选择-">
          <el-option v-for="item in dicttypeList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="父级" prop="pid">
        <el-cascader
          filterable
          placeholder="无"
          v-model="ruleForm.pid"
          ref="refHandle"
          :options="pDictList"
          :props="{ checkStrictly: true ,value:'id',label:'name'}"
          clearable></el-cascader>
      </el-form-item>
      <el-form-item label="标识名" prop="nid" :rules="[{
                    required: true, message: '标识名不能为空', trigger: 'blur'
                  },{min: 2, max: 50, message: '长度在2 到 50 个字符', trigger: 'blur'}]">
        <el-input maxlength="50" clearable v-model="ruleForm.nid" size="small" placeholder="请输入标识名"/>
      </el-form-item>
      <el-form-item label="参数值" prop="value" :rules="{
                    min: 0, max: 1000, message: '不超过1000 个字符', trigger: 'blur'
                  }">
        <el-input maxlength="1000" clearable v-model="ruleForm.value" size="small" placeholder="请输入参数值"/>
      </el-form-item>
      <el-form-item label="备注" prop="remark" :rules="{
                    min: 0, max: 200, message: '不超过200 个字符', trigger: 'blur'
                  }">
        <el-input maxlength="200" clearable v-model="ruleForm.remark" type="textarea" rows="5" size="small" placeholder="请输入内容"/>
      </el-form-item>
      <el-form-item label="有无子级">
        <span v-show="ruleForm.hasChild == 1" style="color: #666">有</span>
        <span v-show="ruleForm.hasChild == 2" style="color: #666">无</span>
        <el-switch v-model="ruleForm.hasChild" :active-value="1" :inactive-value="2" size="small"/>
      </el-form-item>
      <el-form-item label="状态">
        <span v-show="ruleForm.status == 1" style="color: #666">显示</span>
        <span v-show="ruleForm.status == 2" style="color: #666">隐藏</span>
        <el-switch v-model="ruleForm.status" :active-value="1" :inactive-value="2" size="small"/>
      </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  v-if="ruleForm.id||ruleForm.disable" icon="el-icon-back" style="margin-left: 15px" size="medium"
                 @click="onBack">返回
      </el-button>
    </div>
  </div>
</template>

<script>
  import dictApi from '@/api/system/dict'

  export default {
    name: "dict",
    data() {
      return {
        addBtnLoading: false,
      }
    },
    props: {
      ruleForm: {
        type: Object,
        default() {
          return {};
        }
      },
      dicttypeList:{
        type:Array,
        default(){
          return []
        }
      },
      pDictList:{
        type:Array,
        default(){
          return []
        }
      }
    },
    created() {
    },
    mounted() {
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let method = 'dictAdd'
            if (this.ruleForm.id) {
              method = 'dictEdit'
            }
            let ruleForm = JSON.parse(JSON.stringify(this.ruleForm))
            if (ruleForm.pid) {
              if (Array.isArray(ruleForm.pid) && ruleForm.pid.length > 0) {
                ruleForm.pid = ruleForm.pid[ruleForm.pid.length - 1]
              } else if (ruleForm.pid.length == 0) {
                ruleForm.pid = 0
              }
            } else {
              if (!ruleForm.pid) {
                ruleForm.pid = 0
              }
            }
            this.addBtnLoading = true
            dictApi[method](ruleForm).then(res => {
              this.addBtnLoading = false
              this.$refs[formName].resetFields()
              this.$message({
                type: 'success',
                message: this.ruleForm.id ? '修改成功' : '添加成功'
              })
              if (this.ruleForm.id||this.ruleForm.disable) {
                this.$emit('submit',2,this.ruleForm.hasChild)
                this.$emit('onBack')
              }else{
                this.$emit('submit',1,this.ruleForm.hasChild)
              }
            }).catch(e => {
              this.addBtnLoading = false
            })
          }
        })
      },
      onBack() {
        this.$refs['ruleForm'].resetFields()
        this.$emit('onBack')
      }
    }
  }
</script>

<style lang="less">
  .dict-form {

  }
</style>
