<template>
  <div class="admin-department">
    <el-form :inline="true">
      <el-form-item v-if="$permission('/admin/department/check')">
        <el-input v-model="queryParams.name" size="small" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="" size="small" @click="getList" v-if="$permission('/admin/department/check')">查询</el-button>
        <el-button type="primary" size="small" @click="handleAdd(0)" v-if="$permission('/admin/department/add')">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
        row-key="id"
        v-loading="loading"
        :data="tableData"
        border
        @expand-change="getNode"
        header-row-class-name="custom-table-header"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="name" label="部门名称" align="" show-overflow-tooltip></el-table-column>
      <el-table-column prop="tenantName" label="所属租户" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column label="角色" align="center" show-overflow-tooltip>
         <template slot-scope="scope">
          <span v-for="item in scope.row.roleList">{{item.name}}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-dropdown split-button type="primary" size="small" @click="handleEdit(scope.row)">
            <span v-if="$permission('/admin/department/modify')" @click="handleEdit(scope.row)">编辑</span>
            <span v-else :disabled="true">编辑</span>
            <el-dropdown-menu slot="dropdown" class="text-align_center">
              <el-dropdown-item @click.native="handleAdd(scope.row.id,scope.row)" v-if="$permission('/admin/department/add')">新增</el-dropdown-item>
              <el-dropdown-item class="color-red" @click.native="handleDelete(scope.row)" divided v-if="$permission('/admin/department/delete')">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
       <div class="pagination">
      <el-pagination :current-page.sync="queryParams.current" :page-size="queryParams.size" :total="total" @current-change="handleCurrentChange" layout="total, prev, pager, next, jumper" background></el-pagination>
    </div>
    <el-dialog :title="title" :visible.sync="visible" width="500px" :before-close="close">
      <el-form v-if="visible" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" label-position="left">
           <el-form-item label="所属租户" prop="tenant">
              <el-tooltip
                  class="item"
                  effect="dark"
                  :disabled="!userinfo.tenantName"
                  :content="userinfo.tenantName"
                  placement="top-start"
                >
                <el-input v-model="userinfo.tenantName" placeholder="请输入所属租户" :readonly="true" ></el-input>
                </el-tooltip>
            </el-form-item>
       
        <el-form-item label="所属部门" prop="updepartment" v-if="show==true">
            <!-- <el-select v-model="ruleForm.updepartment" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>  -->
               <el-input v-model="lastName" placeholder="请输入所属部门" :disabled="true"></el-input>
        </el-form-item>
         
        
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="ruleForm.name" placeholder="请输入部门名称"></el-input>
        </el-form-item>
            <el-form-item label="角色" prop="role">
            <el-select v-model="ruleForm.role" 
            filterable
            multiple
            collapse-tags
            placeholder="请选择角色"
            :disabled="disabled"
            style="display:block">
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="ruleForm.remark" placeholder="请输入备注" type="textarea" maxlength="100" show-word-limit></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: {
    Treeselect
  },
  data() {
    return {
      visible: false,
      loading: false,
      btnLoading: false,
      total: 0,
      tableData: [],
      title: '',
      show:false,
      disabled:false,
      lastName:'',
      queryParams: {
        size: 10,
        current: 1,
        name: '',
        tenant:localStorage.tenant
      },
      ruleForm: {},
      roleList:[],
      rules: {
        name: [{ required: true, message: '请输入部门名称', trigger: 'blur' },],
        parentId: [{ required: true, message: '请选择所属部门', trigger: 'change' },],
      },
    }
  },
  mounted() {
    this.getList()
    this.getRoleList()
  },
  computed: {
    userinfo() {
      return this.$store.state.admin.userinfo
    }
  },
  methods: {
    getNode(node){
      console.log(node)

    },
    normalizer(node) {
      return {
        id: node.id,
        label: node.name,
      }
    },
    handleAdd(updepartmentId,row) {
      console.log(row)
      this.title = '新增部门'
      this.visible = true
      if(updepartmentId==0){
       
        this.show=false
       this.ruleForm = {
       updepartment:updepartmentId
      }
      }else{
        this.show=true
        if(row.hasOwnProperty('last')){
          this.lastName=row.last+'/'+row.name
        }else{
          this.lastName=row.name
        }
        
        this.ruleForm = {
       updepartment:updepartmentId,
      updepartmentName:row.name,
      path:row.path
      }
      }
    },
    handleEdit(row) {
      this.title = '编辑部门'
      this.visible = true
       this.ruleForm = { ...row }
      if(row.path==0){
        this.show=false
      }else{
        this.show=true
        if(row.hasOwnProperty('last')){
          this.lastName=row.last
        }else{
          this.lastName=row.name
        }
      }
     
    },
    close() {
      this.visible = false
    },
     handleCurrentChange(val) {
      this.queryParams.current = val;
      this.getList();
    },
    getList() {
      this.loading = true
      // this.queryParams.tenant=this.userinfo.tenantId
      this.$api.getDepartmentList(this.queryParams).then(response => {
        
        this.tableData = response.data
              var a = response.data
      for(var j = 0;j<a.length;j++){
        function l(data){
         if(data.hasOwnProperty('children')){
        for(var i = 0;i<data.children.length;i++){
         
          if(data.hasOwnProperty('last')){
            data.children[i].last=data.last+'/'+data.name
          }else{
            data.children[i].last=data.name
          }
          l(data.children[i])
        }
      }
      }
     l(a[j])
      }
      
        this.total=response.pageColumn.total
        this.loading = false
      }).catch(error => {
        this.loading = false
      })
    },
    getRoleList(){
        this.$api.getRoleList().then(response => {
        this.roleList = response.data
      })
    },
    submit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.btnLoading = true
          if (this.ruleForm.id) {
            this.$api.editDepartment(this.ruleForm).then(response => {
              this.$message.success('编辑成功')
              this.btnLoading = false
              this.getList()
              this.close()
            }).catch(error => {
              this.btnLoading = false
            })
          } else {
            this.ruleForm.tenant=this.userinfo.tenantId
            console.log(this.ruleForm)
            this.$api.addDepartment(this.ruleForm).then(response => {
              this.$message.success('新增成功')
              this.btnLoading = false
              this.getList()
              this.close()
            }).catch(error => {
              this.btnLoading = false
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleDelete(row) {
      this.$confirm('确认删除本条数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.$api.deleteDepartment({ id: row.id }).then(response => {
          this.$message.success('删除成功')
          this.loading = false
          this.getList()
        }).catch(error => {
          this.loading = false
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.admin-department {

}
</style>