<template>
  <div class="app-container">
    <el-descriptions title="基本信息" direction="vertical" :column="4" border>
      <el-descriptions-item label="数据接入名称">{{baseInfo.accessSourceName}}</el-descriptions-item>
      <el-descriptions-item label="系统名称">{{baseInfo.dataSourceSystemName}}</el-descriptions-item>
      <el-descriptions-item label="数据库名称" :span="2">{{baseInfo.databaseName}}</el-descriptions-item>
      <el-descriptions-item label="表名称">{{asset2.name}}</el-descriptions-item>
      <el-descriptions-item label="表中文名称">{{asset2.as}}</el-descriptions-item>
      <el-descriptions-item label="数据量">{{asset2.dataTotal}}</el-descriptions-item>
    </el-descriptions>

    <el-tabs type="border-card" v-model="idType" style="margin-top: 20px">
      <el-tab-pane label="部门授权" name="dept">
        <el-table
          ref="deptTable"
          v-loading="loading"
          :data="deptList"
          :default-expand-all="true"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          row-key="deptId"
        >
          <el-table-column label="部门名称" prop="deptName" ></el-table-column>
          <el-table-column label="部门负责人" prop="leader" ></el-table-column>
          <el-table-column label="邮箱" prop="email" ></el-table-column>
          <el-table-column align="center" label="创建时间" prop="createTime" >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" prop="createTime" >
            <template slot-scope="scope">
              <el-switch
                style="display: block"
                v-model="scope.row.isAuth"
                @change="(val)=>authChange(val,scope.row,scope.row.deptId)"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="已授权"
                inactive-text="未授权">
              </el-switch>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total> 0" :limit.sync="pageSize" :page.sync="pageNum" :total="total"/>
      </el-tab-pane>
      <el-tab-pane label="用户授权" name="user">
        <el-table ref="table" v-loading="loading" :data="userList">
          <el-table-column align="center" label="用户名称" prop="userName"/>
          <el-table-column align="center" label="用户昵称" prop="nickName"/>
          <el-table-column align="center" label="用户部门" prop="dept.deptName"/>
          <el-table-column align="center" label="用户邮箱" prop="email"/>
          <el-table-column align="center" label="用户手机号" prop="phonenumber"/>
          <el-table-column align="center" label="创建时间" prop="createTime" />
          <el-table-column align="center" label="操作" prop="createTime" width="200px" >
            <template slot-scope="scope">
              <el-switch
                style="display: block"
                v-model="scope.row.isAuth"
                active-color="#13ce66"
                inactive-color="#ff4949"
                @change="(val)=>authChange(val,scope.row,scope.row.userId)"
                active-text="已授权"
                inactive-text="未授权">
              </el-switch>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total> 0" :limit.sync="pageSize" :page.sync="pageNum" :total="total"/>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>

import { parseTime } from '@/utils/muyu'
import {listDept} from "@/api/system/dept";
import {listUser} from "@/api/system/user";
import {addAuth, delAuth, listAuth} from "@/api/accredit/auth";

export default {
  name: "AuthTable",
  props:{
    baseInfo:{
      type:Object,
      default:{}
    },
    asset2:{
      type:Object,
      default:{}
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      idType: "dept",
      // 分页信息
      total: 0,
      pageNum: 1,
      pageSize: 1,
      deptData:[],
      // 部门列表
      deptList: [],
      // 用户列表
      userList: [],
      authType:'table',
      argumentsList:{
        authId:"",
        idType:"",
        authData:"",
        authType:"",
      }
    };
  },
  watch:{
    baseInfo:{
      handler(){
        this.loading = true;
        setTimeout(() => this.loading = false, 200)
        this.init();
      },
      immediate:true,
      deep:true
    },
    idType:{
      handler(){
        this.setAuthInfo();
      },
      immediate:true
    },
  },
  methods: {
    parseTime,
    init(){
      listDept(null).then(response => {
        this.deptData = response.data;
        this.deptList = this.handleTree(response.data, "deptId");
      });
      listUser(null).then(response => {
          this.userList = response.data.rows;
        }
      );
      setTimeout(()=>{
        this.setAuthInfo();
      },600);
    },
    setAuthInfo(){
      listAuth({idType:this.idType,authType:this.authType}).then(response=>{
        let data;
        if(this.idType==='dept'){
          data=this.deptData;
        }else {
          data=this.userList;
        }
        data.forEach(x=>{
          response.data.rows.forEach(y=>{
            let id=y.authData.split(",")[1];
            if(this.idType==='dept'&&x.deptId===y.authId&&this.baseInfo.databaseName +","+this.asset2.name==y.authData){
              console.log(this.baseInfo)
              this.$set(x,'isAuth',true);
              this.$set(x,'assetAuthId',y.id);
            }else if(this.idType==='user'&&x.userId===y.authId&&this.baseInfo.databaseName +","+this.asset2.name==y.authData){
              this.$set(x,'isAuth',true);
              this.$set(x,'assetAuthId',y.id);
            }
          })
        })
        if(this.idType === 'dept'){
          this.deptList = this.handleTree(this.deptData, "deptId");
        }
      })
    },
    authChange(val,row,id){
      if(val){
        //增
        addAuth({authId:id,idType:this.idType,
          authData:this.baseInfo.databaseName+','+this.asset2.name
          ,authType:this.authType}).then(response=>{
          this.$message.success("授权成功");
          this.setAuthInfo();
        });
      }else {
        this.argumentsList={}

        this.argumentsList.authId=id
        this.argumentsList.authData=this.baseInfo.databaseName +","+ this.asset2.name
        this.argumentsList.authType=this.authType
        this.argumentsList.idType=this.idType

        //删
        delAuth(this.argumentsList).then(response=>{
          this.$message.success("取消授权成功");
          this.setAuthInfo();
        });
      }
    }
  },
};
</script>
