<template>
  <div class="app-container">
    <el-form ref="form" :model="baseInfo" label-width="120px">
      <el-row>
        <el-col :offset="2" :span="8">
          <el-form-item label="数据接入名称" prop="nickName">
            <el-input v-model="baseInfo.accessSourceName" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="系统名称" prop="userName">
            <el-input v-model="baseInfo.dataSourceSystemName" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="数据库名称" prop="nickName">
            <el-input v-model="baseInfo.databaseName" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <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"
                @change="(val)=>authChange(val,scope.row,scope.row.userId)"
                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-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: "AuthDataSource",
  props:{
    baseInfo:{
      type:Object,
      default:{}
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      idType: "dept",
      // 分页信息
      total: 0,
      pageNum: 1,
      pageSize: 1,
      deptData: [],
      // 部门列表
      deptList: [],
      // 用户列表
      userList: [],
      authType:'dataSource',
      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
          data.forEach(x => {
            response.data.rows.forEach(y => {
              console.log("y",y)
              if (this.idType === 'dept' && x.deptId === y.authId && this.baseInfo.databaseName==y.authData) {
                console.log("y1是等于什么=?",y)
                this.$set(x, 'isAuth', true);
                this.$set(x, 'assetAuthId', y.databaseName);
              } else if (this.idType === 'user' && x.userId === y.authId && this.baseInfo.databaseName==y.authData) {
                console.log("y2是等于什么=?",y)
                this.$set(x, 'isAuth', true);
                this.$set(x, 'assetAuthId', y.databaseName);
              }
            })
          })
        } else {
          data = this.userList;
          data.forEach(x => {
            response.data.rows.forEach(y => {
              if (this.idType === 'dept' && x.deptId === y.authId && this.baseInfo.databaseName==y.authData) {
                console.log("y1是等于什么=?",y)
                this.$set(x, 'isAuth', true);
                this.$set(x, 'assetAuthId', y.databaseName);
              } else if (this.idType === 'user' && x.userId === y.authId && this.baseInfo.databaseName==y.authData) {
                console.log("y2是等于什么=?",y)
                this.$set(x, 'isAuth', true);
                this.$set(x, 'assetAuthId', y.databaseName);
              }
            })
          })
        }
        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
          , authType: this.authType
        }).then(response => {
          this.$message.success("授权成功");
          this.setAuthInfo();
        });
      } else {
        //删
        console.log("val",val)
        console.log("row",row)
        console.log("id",id)

        this.argumentsList={}

        this.argumentsList.authId=id
        this.argumentsList.authData=this.baseInfo.databaseName
        this.argumentsList.authType=this.authType
        this.argumentsList.idType=this.idType

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