<template>
  <div class="deptForm">
    <el-form ref="infoForm" :model="infoForm" size="small" label-width="100px">
      <el-form-item label="部门名称" prop="name" :rules="[{
                    required: true, message: '部门名称名称不能为空', trigger: 'blur'
                  },
                  {min: 1, max: 20, message: '部门名称长度在1 到 20 个字符', trigger: 'blur'}]">
        <el-input maxlength="20" clearable v-model="infoForm.name" size="small" placeholder="请输入部门名称"/>
      </el-form-item>
      <el-form-item prop="pid" label="上级部门">
        <el-cascader
          filterable
          :disabled="infoForm.disable"
          placeholder="请选择上级部门"
          v-model="infoForm.pid"
          ref="refHandle"
          :options="filterAgencyList"
          :props="{ checkStrictly: true ,value:'id',label:'name',children:'childrenList'}"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number size="small" v-model="infoForm.sort"></el-input-number>
      </el-form-item>
      <el-form-item label="备注" prop="note" :rules="[
            {min: 0, max: 50, message: '备注长度不超过 50 个字符', trigger: 'blur'}]">
        <el-input show-word-limit maxlength="50" clearable v-model="infoForm.note" type="textarea" rows="3" size="small" placeholder="请输入内容"/>
      </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('infoForm')">提交
      </el-button>
      <el-button icon="el-icon-back" style="margin-left: 15px" size="medium" @click="closeWin('infoForm')">关闭
      </el-button>
    </div>


  </div>
</template>

<script>
  import deptApi from '@/api/permission/dept'

  export default {
    name: "deptForm",
    data() {
      return {
        addBtnLoading: false,
      }
    },
    props: {
      // 参数接收
      infoForm: {
        type: Object,
        default() {
          return {};
        }
      },
      // 参数接收
      filterAgencyList:  {
        type: Array,
        default() {
          return [];
        }
      },
    },
    created() {

    },
    mounted() {
    },
    methods: {
      // 提交表单
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let method = 'add'
            if (this.infoForm.id) {
              method = 'edit'
            }
            let infoForm=JSON.parse(JSON.stringify(this.infoForm))
            if(infoForm.pid){
              if(infoForm.pid instanceof Array){
                if(infoForm.pid.length>0){
                  infoForm.pid=infoForm.pid[infoForm.pid.length-1]
                }else{
                  infoForm.pid=''
                }
              }
            }else{
              infoForm.pid=''
            }
            this.addBtnLoading = true
            deptApi[method](infoForm).then(res => {
              this.addBtnLoading = false
              this.$refs[formName].resetFields()
              this.$message({
                type: 'success',
                message: this.infoForm.id ? '修改成功' : '添加成功'
              })
              if (this.infoForm.id) {
                // 编辑
                this.$emit('submit',1)
              }else{
                // 添加
                this.$emit('submit',0)
              }
              // 调动关闭窗体方法
              this.$emit('closeWin')
            }).catch(e => {
              this.addBtnLoading = false
            })
          }
        })
      },
      // 关闭窗体
      closeWin() {
        this.$refs['infoForm'].resetFields()
        this.$emit('closeWin')
      }
    }
  }
</script>

<style lang="less">
  .deptForm{

  }
</style>
