<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="nickName">
            <el-input v-model="baseInfo.name" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="系统名称" prop="userName">
            <el-input v-model="baseInfo.systemName" 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-col :offset="2" :span="8">
          <el-form-item label="表名称" prop="nickName">
            <el-input v-model="baseInfo.tableName" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="表中文名" prop="nickName">
            <el-input v-model="baseInfo.asas" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="数据量" prop="nickName">
            <el-input v-model="baseInfo.dataTotal" 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'}">
          <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 label="创建时间" prop="createTime" ></el-table-column>
          <el-table-column align="center" label="操作" prop="statusId" >
            <template slot-scope="scope">
              <el-switch
                style="display: block"
                v-model="scope.row.statusId"
                @change="updDept(scope.row)"
                active-value= "Y"
                inactive-value= "N"
                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="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="statusId" >
            <template slot-scope="scope">
              <el-switch
                style="display: block"
                v-model="scope.row.statusId"
                @change="updUser(scope.row)"
                active-value= "Y"
                inactive-value= "N"
                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 { listMiddle, listUserMiddle ,updateUserMiddle, updateMiddle } from "@/api/system/accredit";

export default {
  props: {
    title: {
      type: Object
    },
    stats: {
      type: Object
    }
  },
  name: "AuthTable",
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "dept",
      // 分页信息
      total: 0,
      pageNum: 1,
      pageSize: 1,
      // 基本信息
      baseInfo: {
        name: "",
        systemName: "",
        databaseName: "",
        tableName: "",
        asas: "",
        dataTotal: "",
      },
      // 部门列表
      deptList: [],
      // 用户列表
      userList: []
    };
  },
  created() {
    this.loading = true;
    setTimeout(() => this.loading = false, 200)
    this.init()
  },
  methods: {
    updDept(row) {
      console.log(row.statusId)
      updateMiddle(row.mid,row.statusId).then(res => {
        this.$message.success(res.data)
      })

    },
    updUser(row) {
      console.log(row.statusId)
      updateUserMiddle(row.mid,row.statusId).then(res => {
        this.$message.success(res.data)
      })
    },
    init(){
      this.baseInfo = this.stats
      //部门
      listMiddle(this.title.tableName,this.title.databaseName).then(res => {
        this.deptList = res.data
      })
      //用户
      listUserMiddle(this.title.tableName,this.title.databaseName).then(res => {
        this.userList = res.data
        console.log(res.data)
      })
    }
  },
};
</script>
