<template>
    <div>
        <el-card class="box-card" shadow="always">
          <div slot="header" class="clearfix">
            <span>{{ title }}</span>
            <el-button-group style="float: right;">
              <el-button size="mini" icon="el-icon-back" round @click="showCard">返回</el-button>
            </el-button-group>
          </div>
          <el-form ref="form" :model="form" label-width="80px" disabled>
            <el-form-item label="上级部门" prop="parentId">
              <treeselect v-model="form.parentId" :options="deptOptions" :normalizer="normalizer" disabled/>
            </el-form-item>
            <el-form-item label="部门名称" prop="deptName">
              <el-input v-model="form.deptName" />
            </el-form-item>
            <el-form-item label="部门编码" prop="deptNo">
              <el-input v-model="form.deptNo" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in statusOptions"
                  :key="dict.id"
                  :label="dict.itemText"
                >{{dict.itemValue}}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" />
            </el-form-item>
          </el-form>
        </el-card>
    </div>
</template>

<script>
import { getDept, listDept } from '@/api/system/dept'
import { construct } from '@/utils/json-tree'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'DeptDetail',
  components: { Treeselect },
  props: {
    data: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      title: '部门详情',
      // 展示切换
      showOptions: {
        data: {},
        showList: true,
        showAdd: false,
        showEdit: false,
        showDetail: false
      },
      // 表单参数
      form: {},
      // 状态数据字典
      statusOptions: [],
      // 部门树选项
      deptOptions: []
    }
  },
  created () {
    console.log('id:' + this.data.id)
    this.getDicts('sys_common_status').then(response => {
      if (response.success) {
        this.statusOptions = response.data
      }
    })
    this.getDeptTreeSelect()
    this.getDept(this.data.id)
  },
  methods: {
    showCard () {
      this.$emit('showCard', this.showOptions)
    },
    getDeptTreeSelect: function () {
      listDept().then(response => {
        if (response.success) {
          const { data } = response
          this.deptOptions = []
          const dept = { id: '0', deptName: '主类目', children: [] }
          dept.children = construct(data, {
            id: 'id',
            pid: 'parentId',
            children: 'children'
          })
          this.deptOptions.push(dept)
        }
      })
    },
    /** 转换部门数据结构 */
    normalizer (node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.deptName,
        children: node.children
      }
    },
    /** 获取详情 */
    getDept: function (id) {
      getDept(id).then(response => {
        if (response.success) {
          this.form = response.data
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
