<template>
  <el-dialog
    :title="!dataForm.dictId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="dataForm.firstId">
          <el-radio v-for="(type, index) in dataForm.typeList" :label="index" :key="index">{{ type }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="上级字典" prop="parentName">
        <el-popover
          ref="dictListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            class="flow-tree"
            :data="dictList"
            :props="dictListTreeProps"
            node-key="dictId"
            ref="dictListTree"
            @current-change="dictListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.parentName" v-popover:dictListPopover :readonly="true" placeholder="点击选择上级字典" class="dict-list__input"></el-input>
      </el-form-item>
      <el-form-item label="排序号" prop="orderNum">
        <el-input-number v-model="dataForm.orderNum" controls-position="right" :min="0" label="排序号"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        dataList: [],
        dataForm: {
          dictId: 0,
          firstId: 0,
          typeList: ['立项字典', '合同字典', '人员字典', '支出字典'],
          name: '',
          parentId: 0,
          parentName: '',
          orderNum: 0
        },
        dataRule: {
          parentId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        },
        dictList: [],
        dictListTreeProps: {
          label: 'name',
          children: 'children'
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.dictId = id || 0
        this.$http({
          url: this.$http.adornUrl('/business/businessdict/select'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.dictList = treeDataTranslate(data.dictList, 'dictId')
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }).then(() => {
          if (!this.dataForm.dictId) {
            // 新增
            this.dictListTreeSetCurrentNode()
          } else {
            // 修改
            this.$http({
              url: this.$http.adornUrl(`/business/businessdict/info/${this.dataForm.dictId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              this.dataForm.dictId = data.data.dictId
              this.dataForm.firstId = data.data.firstId - 1
              this.dataForm.name = data.data.name
              this.dataForm.parentId = data.data.parentId
              this.dataForm.orderNum = data.data.orderNum
              this.dictListTreeSetCurrentNode()
            })
          }
        })
      },
      // 菜单树选中
      dictListTreeCurrentChangeHandle (data, node) {
        this.dataForm.parentId = data.dictId
        this.dataForm.parentName = data.name
      },
      // 菜单树设置当前选中节点
      dictListTreeSetCurrentNode () {
        this.$refs.dictListTree.setCurrentKey(this.dataForm.parentId)
        this.dataForm.parentName = (this.$refs.dictListTree.getCurrentNode() || {})['name']
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/businessdict/${!this.dataForm.dictId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'dictId': this.dataForm.dictId || undefined,
                'firstId': this.dataForm.firstId + 1,
                'firstName': this.dataForm.typeList[this.dataForm.firstId],
                'parentId': this.dataForm.parentId,
                'name': this.dataForm.name,
                'status': 0,
                'orderNum': this.dataForm.orderNum
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style lang="scss">
.mod-menu {
  .menu-list__input {
    > .el-input__inner {
      cursor: pointer;
    }
  }
}
.flow-tree{
  overflow: auto;
  flex: 1;
  height: 400px;
  width: 350px;
  margin: 0px;
  padding: 0px;
  >>>.el-tree-node{
    >.el-tree-node__children{
      overflow: visible !important;
    }
  }
}
</style>
