<template>
  <div class="app-container">
    <h4 class="form-header h4">基本信息</h4>
    <el-form ref="form" :model="baseInfo" label-width="120px">
      <el-row>
        <el-col :offset="2" :span="8">
          <el-form-item label="数据接入名称" prop="name">
            <el-input v-model="baseInfo.dataSource.name" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="系统名称" prop="userName">
            <el-input v-model="baseInfo.dataSource.fromSystem" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="数据库名称" prop="nickName">
            <el-input v-model="baseInfo.dataSource.dataSourceDatabaseName" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="表名称" prop="nickName">
            <el-input v-model="baseInfo.dataTable.tableName" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="表中文名" prop="nickName">
            <el-input v-model="baseInfo.dataTable.tableAnnotation" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="数据量" prop="nickName">
            <el-input v-model="baseInfo.dataTable.recordCount" 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
          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
                v-model="scope.row.havePermissions"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="已授权"
                inactive-text="未授权"
                active-value='Y'
                inactive-value='N'
                @change="changeStatus($event,scope.row)">
              </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="havePermissions" >
            <template slot-scope="scope">
              <el-switch
                style="display: block"
                v-model="scope.row.havePermissions"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="已授权"
                inactive-text="未授权"
                active-value="Y"
                inactive-value="N"
                @change="changeStatus($event,scope.row)">
              </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 {insertPermissions} from "@/api/dataSource/Permissions";

export default {
  name: "AuthTable",
  data() {
    return {
      permissionsReq: {},
      // 遮罩层
      loading: true,
      activeName: "dept",
      // 分页信息
      total: 0,
      pageNum: 1,
      pageSize: 1,
      // 基本信息
      baseInfo: {},
      // 部门列表
      deptList: [],
      // 用户列表
      userList: []
    };
  },
  props: {
    userAndDeptDataPermissionsListResp: {
      type: Object
    }
  },
  watch: {
    userAndDeptDataPermissionsListResp: {
      handler(newValue,oldValue){
        this.baseInfo = newValue.dataInfo
        this.deptList = this.handleTree(newValue.deptDataPermissionList,"deptId")
        console.log("前台deptList:",this.deptList)
        this.userList = newValue.userDataPermissionsList
      }
    }
  },
  created() {
    this.loading = true;
    setTimeout(() => this.loading = false, 200)
  },
  methods: {
    parseTime,
    changeStatus(data,b){
      this.permissionsReq.newPermissions = data
      this.permissionsReq.userOrDeptType = "user"
      if (b.deptName){
        this.permissionsReq.userOrDeptType = "dept"
      }
      if (this.permissionsReq.userOrDeptType == "dept"){
        this.permissionsReq.userOrDeptId = b.deptId
      }else {
        this.permissionsReq.userOrDeptId = b.userId
      }

      this.permissionsReq.dataId = this.baseInfo.dataTable.id
      this.permissionsReq.dataType = "dataTable"
      console.log("传输到后台的PermissionsReq对象",this.permissionsReq)
      insertPermissions(this.permissionsReq).then(
        res => {
          console.log("权限改变的结果:",res.msg)
        }
      )
    }
  },
};
</script>
