<template>
  <div class="app-container">
    <h4 class="form-header h4">基本信息</h4>
    <el-form ref="form" :model="title" label-width="120px">
      <el-row>
        <el-col :offset="2" :span="8">
          <el-form-item label="数据接入名称" prop="nickName">
            <el-input v-model="title.dataSourceName" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="系统名称" prop="userName">
            <el-input v-model="title.systemName" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="数据库名称" prop="nickName">
            <el-input v-model="title.databaseName" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="表名称" prop="nickName">
            <el-input v-model="title.tableName" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="表中文名" prop="nickName">
            <el-input v-model="title.tableComment" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="数据量" prop="nickName">
            <el-input v-model="title.tableCount" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-tabs type="border-card" v-model="activeName">
      <el-tab-pane label="部门授权" name="dept">
        <el-table
          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="orderNum" ></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" class-name="small-padding fixed-width" label="操作">
            <template slot-scope="scope">
              <el-switch
                style="display: block"
                v-model="scope.row.isAuth"
                @change="authChange(scope.row)"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="已授权"
                inactive-text="未授权">
              </el-switch>
            </template>
          </el-table-column>
        </el-table>
      </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" >
            <template slot-scope="scope">
              <el-switch
                style="display: block"
                v-model="scope.row.isAuth"
                @change="authChange(scope.row)"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="已授权"
                inactive-text="未授权">
              </el-switch>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="userQueryParams.total> 0" :limit.sync="userQueryParams.pageSize" :page.sync="userQueryParams.pageNum" :total="userQueryParams.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 {addAccredit, deleteSourceAccreditBySourceIds, getSourceAccreditByDataSourceId} from "@/api/etl/sourceAccredit";
import {addAssetAccredit, deleteAssetAccreditByAssetIds, getAssetAccreditByDataAssetId} from "@/api/etl/assetAccredit";

export default {
  watch:{
    'title':{
      handler(val) {
        this.getAssetAccreditByDataAssetId()
      },
      immediate: true
    }
  },
  props:{
    title:{}
  },
  name: "AuthTable",
  data() {
    return {
      refreshTable: true,
      // 遮罩层
      loading: true,
      activeName: "dept",
      // 分页信息
      // 用户查询参数
      userQueryParams: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      // 部门列表
      deptList: [],
      // 用户列表
      userList: [],
      deptAccreditIds:[],
      userAccreditIds:[],
      AssetAccreditReq:{
        deptIds: null,
        userId: null,
        dataAssetId: null
      },
    };
  },
  created() {
  },
  methods: {
    recursion(deptInfo){
      let ids =[]
      ids.push(deptInfo.deptId)
      if (deptInfo.children){
        deptInfo.children.forEach(item => {
          ids.push(item.deptId)
          if (item.children){
            ids.push(...this.recursion(item))
          }
        })
      }
      return ids
    },
    authChange(row) {
      this.loading = true
      this.AssetAccreditReq={
        deptIds:null,
        userId: null,
        dataAssetId: null
      }
      if (row.nickName){
        this.AssetAccreditReq.userId = row.userId
        this.AssetAccreditReq.dataAssetId = this.title.id
      }else{
        let ids=this.recursion(row)
        const uniqueIds = [...new Set(ids)];
        this.AssetAccreditReq.deptIds = uniqueIds
        this.AssetAccreditReq.dataAssetId = this.title.id
      }
      if (row.isAuth){
        addAssetAccredit(this.AssetAccreditReq).then(res => {
          this.getAssetAccreditByDataAssetId()
        })
      }else{
        deleteAssetAccreditByAssetIds(this.AssetAccreditReq).then(res => {
          this.getAssetAccreditByDataAssetId()
        })
      }

    },
    recursionAuth(deptInfo){
      deptInfo.isAuth = this.deptAccreditIds.indexOf(deptInfo.deptId) > -1
      if (deptInfo.children){
        deptInfo.children.forEach(item => {
          item.isAuth = this.deptAccreditIds.indexOf(item.deptId) > -1
          if (item.children){
            this.recursionAuth(item)
          }
        })
      }
    },
    getAssetAccreditByDataAssetId() {
      if (this.title.id != undefined) {
        getAssetAccreditByDataAssetId(this.title.id).then(response => {
          this.deptAccreditIds = response.data.deptAccreditModelIds
          this.userAccreditIds = response.data.userAccreditModelIds
          setTimeout(() => this.getList(), 50)
          setTimeout(() => this.getUserList(), 50)
          this.loading = false
        })
      }
    },
    /** 查询部门列表 */
    getList() {
      listDept(this.queryParams).then(response => {
        this.deptList = this.handleTree(response.data, "deptId");
        this.deptList.forEach(dept => {
          this.recursionAuth(dept)
        })
      });
    },
    /** 查询用户列表 */
    getUserList() {
      listUser(this.addDateRange(this.userQueryParams, [])).then(response => {
        this.userList = response.data.rows;
        this.userList.forEach(user => {
          user.isAuth = this.userAccreditIds.indexOf(user.userId) > -1
        })
          this.userQueryParams.total = response.data.total;
        }
      );
    },
    parseTime,

  },
};
</script>
