
<template>
  <div class="app-container calendar-list-container">
    <basic-container>
      <div class="filter-container">
        <el-button-group>
          <el-button
            v-if="deptManager_btn_add"
            type="primary"
            icon="plus"
            :disabled='form.name==""'
            @click="handlerAdd">添加
          </el-button>
          <el-button
            v-if="deptManager_btn_edit"
            type="primary"
            icon="edit"
            :disabled='form.name==""'
            @click="handlerEdit">编辑
          </el-button>
          <el-button
            v-if="deptManager_btn_del"
            type="primary"
            icon="delete"
            :disabled='form.name==""'
            @click="handleDelete">删除
          </el-button>
        </el-button-group>
      </div>

      <el-row>
        <el-col
          :span="8"
          style="margin-top:15px;">
          <el-tree
            :data="treeData"
            :props="defaultProps"
            :filter-node-method="filterNode"
            class="filter-tree"
            node-key="aid"
            highlight-current
            default-expand-all
            @node-click="getNodeData"/>
        </el-col>
        <el-col
          :span="16"
          style="margin-top:15px;">
          <el-card class="box-card">
            <el-form
              ref="form"
              :label-position="labelPosition"
              :rules="rules"
              :model="form"
              label-width="80px">
              <el-form-item
                label="组织名称"
                prop="name">
                <el-input
                  v-model="form.name"
                  :disabled="formEdit"
                  placeholder="请输入组织名称"/>
              </el-form-item>
              <el-form-item
                label="角色描述"
                prop="description">
                <el-input
                  v-model="form.description"
                  type="textarea"
                  :disabled="formEdit"
                  placeholder="请输入角色描述"/>
              </el-form-item>
              <el-form-item v-if="formStatus == 'update'">
                <el-button
                  type="primary"
                  @click="update">更新
                </el-button>
                <el-button @click="onCancel">取消</el-button>
              </el-form-item>
              <el-form-item v-if="formStatus == 'create'">
                <el-button
                  type="primary"
                  @click="create">保存
                </el-button>
                <el-button @click="onCancel">取消</el-button>
              </el-form-item>
            </el-form>
          </el-card>
        </el-col>
      </el-row>
    </basic-container>
  </div>
</template>

<script>
import { addObj, delObj, fetchTree, getObj, putObj } from '@/api/admin/dept'
import { mapGetters } from 'vuex'

export default {
  name: 'Dept',
  data() {
    return {
      list: null,
      total: null,
      formEdit: true,
      formAdd: true,
      formStatus: '',
      typeOptions: ['0', '1'],
      methodOptions: ['GET', 'POST', 'PUT', 'DELETE'],
      listQuery: {
        name: undefined
      },
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      rules: {
        name: [
          { required: true, message: '请输入组织名称', trigger: 'blur' },
          { min: 3, max: 32, message: '长度在 3 到 32 个字符', trigger: 'blur'}
        ],
      },
      labelPosition: 'right',
      form: {
        name: '',
        pid: '',
        description:'',
        id:'',
        orgCode:'',
      },
      currentId: 0,
      deptManager_btn_add: false,
      deptManager_btn_edit: false,
      deptManager_btn_del: false
    }
  },
  created() {
    // this.getList()
    this.deptManager_btn_add = this.permissions['sys_dept_add']
    this.deptManager_btn_edit = this.permissions['sys_dept_edit']
    this.deptManager_btn_del = this.permissions['sys_dept_del']
    this.getTreeDataList()//获取部门树
  },
  computed: {
    ...mapGetters([
      'elements',
      'permissions'
    ])
  },
  methods: {
      getTreeDataList(){
      this.$api.api.userDeptTree()
      .then(res=>{
        this.treeData=res.data
        // this.orgCode=res.data[0].orgCode
        // console.log(this.orgCode);
      //   this.$nextTick(() => {
      //   this.$refs.tree.setCurrentKey(0)
      // })
        // this.getList()
      })
    },
    getList() {
      fetchTree(this.listQuery).then(response => {
        this.treeData = response.data
      })

    },
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    getNodeData(data) {
      if (!this.formEdit) {
        this.formStatus = 'update'
      }
      console.log(data);
      this.form={
        name:data.name,
        pid:data.aid,
        description:data.description,
        id:data.aid,
        orgCode:data.orgCode
      }
      // getObj(data.id).then(response => {
      //   this.form = response.data
      // })
      this.currentId = data.id
    },
    handlerEdit() {
      console.log(this.form);
      if (this.form.id) {
        this.formEdit = false
        this.formStatus = 'update'
      }
    },
    handlerAdd() {
      // this.resetForm()
      this.$refs.form.resetFields()
      // console.log(this.form);
      this.formEdit = false
      this.formStatus = 'create'
    },
    handleDelete() {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // delObj(this.currentId)
        this.$api.api.deptDelOrg(this.form)
        .then(() => {
          this.getTreeDataList()
          // this.resetForm()
          this.onCancel()
          this.$notify.success('删除成功')
        })
      })
    },
    update() {
      this.$refs.form.validate((valid) => {
        if (!valid) return
        // putObj(this.form)
        this.$api.api.deptUpdateOrgTree(this.form)
        .then(() => {
          this.getTreeDataList()
          this.onCancel()
          this.$notify.success('更新成功')
        })
      })
    },
    create() {
      this.$refs.form.validate((valid) => {
        if (!valid) return
        // console.log(this.form);
        this.$api.api.deptAddOrg(this.form)
        .then(() => {
          this.getTreeDataList()
          this.onCancel()
          this.$notify.success('创建成功')
        })
      })
    },
    onCancel() {
      this.formEdit = true
      this.formStatus = ''
    },
    resetForm() {
      this.form = {
        parentId: this.currentId
      }
    }
  }
}
</script>

