<template>
  <el-dialog
    :title="dataForm.deptId === null ? '新增部门信息' : '修改部门信息'"
    :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="code">
        <el-input v-model="dataForm.code" placeholder="部门编码"></el-input>
      </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="deptSort">
        <el-input-number
          v-model.number="dataForm.deptSort"
          :min="0"
          :max="999"
          controls-position="right"
        />
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="顶级部门" prop="isTop">
            <el-radio-group v-model="dataForm.isTop" style="width: 140px">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="isUsed">
            <el-switch
              v-model="dataForm.isUsed"
              active-color="#409EFF"
              inactive-color="#F56C6C"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item v-if="dataForm.isTop === '0'" label="上级部门" prop="pid">
        <treeselect
          v-model="dataForm.pid"
          :load-options="loadDepts"
          :options="depts"
          :normalizer="normalizer"
          placeholder="选择部门类目"
        />
      </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 Treeselect, { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  export default {
    data () {
      return {
        visible: false,
        id: 0,
        dataForm: {
          deptId: null,
          code: null,
          name: null,
          deptSort: 999,
          isTop: '1',
          isUsed: true
          // ext_id: null,
          // sub_count: 0,
          // pid: null
        },
        depts: [],
        dataRule: {
          name: [
            { required: true, message: '参数值不能为空', trigger: 'blur' }
          ],
          deptSort: [
            { required: true, message: '参数值不能为空', trigger: 'blur', type: 'number' }
          ]
        }
      }
    },
    components: {
      Treeselect
    },
    methods: {
      init (row) {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (row) {
            this.dataForm = {...row}
            // if (this.dataForm.pid !== null) {
            //   this.dataForm.isTop = '0'
            // } else {
            //   this.dataForm.isTop = '1'
            // }
            if (this.dataForm.pid != null) {
              this.getSupDepts(this.dataForm.pid)
            } else {
              this.getDepts()
            }
          }
        })
      },
      getSupDepts(ids) {
        const data = ids.length || ids.length === 0 ? ids : Array.of(ids)
        this.$http({
          url: this.$http.adornUrl('/api/dept/superior'),
          method: 'post',
          params: this.$http.adornParams(data)
        }).then(({res}) => {
          const date = res.content
          this.buildDepts(date)
          this.depts = date
        })
      },
      buildDepts(depts) {
        depts.forEach(data => {
          if (data.children) {
            this.buildDepts(data.children)
          }
          if (data.has_children && !data.children) {
            data.children = null
          }
        })
      },
      getDepts() {
        this.$http({
          url: this.$http.adornUrl('/api/dept/vo'),
          method: 'get',
          params: this.$http.adornParams({is_used: true})
        }).then(({data}) => {
          if (data && data.code === 200) {
            this.depts = data.content.map(function(obj) {
              if (obj.has_children) {
                obj.children = null
              }
              return obj
            })
          }
        })
      },
      // 获取弹窗内部门数据
      loadDepts({ action, parentNode, callback }) {
        if (action === LOAD_CHILDREN_OPTIONS) {
          this.$http({
            url: this.$http.adornUrl('/api/dept/vo'),
            method: 'get',
            params: this.$http.adornParams({is_used: true, pid: parentNode.dept_id})
          }).then(({res}) => {
            parentNode.children = res.content.map(function(obj) {
              obj.children = null
              return obj
            })
            setTimeout(() => {
              callback()
            }, 100)
          })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl('/api/dept'),
              method: this.dataForm.deptId !== null ? 'put' :'post',
              data: this.$http.adornData(this.dataForm)
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
