<template>
  <div class="app-container">
    <h4 class="form-header h4">基本信息</h4>
2222
    {{DataSource}}
    <el-form ref="form" :model="DataSource" label-width="120px">
      <el-row>
        <el-col :offset="2" :span="8">
          <el-form-item label="数据接入名称" prop="nickName">
            <el-input v-model="DataSource.name" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="系统名称" prop="userName">
            <el-input v-model="DataSource.systemName" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="数据库名称" prop="nickName">
            <el-input v-model="DataSource.databaseName" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="表名称" prop="nickName">
            <el-input v-model="DataSource.tableName" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="表中文名" prop="nickName">
            <el-input v-model="DataSource.tableAsName" disabled/>
          </el-form-item>
        </el-col>
        <el-col :offset="2" :span="8">
          <el-form-item label="数据量" prop="nickName">
            <el-input v-model="DataSource.total" 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="deptlists"
          :normalizer="normalizer"
          :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.status"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="已授权"
                inactive-text="未授权"
                :active-value="1"
                :inactive-value="0"
              >
              </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" >
            <template slot-scope="scope">
              <el-switch
                style="display: block"
                v-model="scope.row.status"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="已授权"
                inactive-text="未授权"
                :active-value="1"
                :inactive-value="0"
                @change="updateUserPermissions(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 { listDept } from '@/api/system/dept'
import { permissionsUserAdd, permissionsUserDel } from '@/api/kvt/kvt'

export default {
  name: "AuthTable",
  props: [
    "DataSource",
    "deptlists",
    "userList",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "dept",
      // 分页信息
      total: 0,
      pageNum: 1,
      pageSize: 1,
      // 基本信息
      baseInfo: {},
      // 部门列表
      deptList: [],
      // // 用户列表
      // userList: []
    };
  },
  watch: {
    //监听部门
    'deptlists.status'(newVal) {
      if (newVal === 0) {
        console.log("ree",newVal)
        // 开关为激活状态，执行相关逻辑
      } else if (newVal === 1) {
        alert("打开")
        // 开关为未激活状态，执行相关逻辑
      }
      deep:true
    },
    //监听用户
    'userList.status'(row,newVal) {
      if (newVal === 0) {
          console.log("reeeee",newVal)
        console.log("rowwwwwww",row)
        // 开关为激活状态，执行相关逻辑
      } else if (newVal === 1) {
        alert("打开")
        // 开关为未激活状态，执行相关逻辑
      }
      deep:true
    }
  },
  created() {
    this.loading = true;
    setTimeout(() => this.loading = false, 200)
    // this.init()
  },
//   mounted() {
// this.selectDept()
//   },
  methods: {
    updateUserPermissions(row){
      console.log("rrrow", row)
      const userId =row.userId
      const deptId =row.dept.deptId
      console.log("Tht",this.DataSource)
      let data = {
        tbaleName:this.DataSource.tableName,
        kvtId:this.DataSource.id,
        userId: userId,
        deptId :deptId
      }

        permissionsUserAdd(data).then(res => {
          this.$message.success(res.data)
        })
    },
    parseTime,
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    // init(){
    //   let response = {
    //     "code": 200,
    //     "msg": "操作成功",
    //     "data": [
    //       // {
    //       //   "createBy": 1,
    //       //   "createTime": "2023-09-29 11:47:27",
    //       //   "updateBy": null,
    //       //   "updateTime": null,
    //       //   "deptId": 100,
    //       //   "parentId": 0,
    //       //   "ancestors": "0",
    //       //   "deptName": "muyu牧鱼科技",
    //       //   "orderNum": 0,
    //       //   "leader": "muyu牧鱼",
    //       //   "phone": "15888888888",
    //       //   "email": "ry@qq.com",
    //       //   "status": "0",
    //       //   "delFlag": "0",
    //       //   "parentName": null,
    //       //   "children": []
    //       // }
    //      ]
    //   }
    //   this.deptList = this.handleTree(response.data, "deptId");
    //   this.userList = [
    //     {
    //       "createBy": "admin",
    //       "createTime": "2023-04-23 16:11:38",
    //       "updateBy": null,
    //       "updateTime": null,
    //       "remark": "管理员",
    //       "userId": 1,
    //       "deptId": 103,
    //       "userName": "admin",
    //       "nickName": "智能车联",
    //       "email": "ry@163.com",
    //       "phonenumber": "15888888888",
    //       "isAuth": true,
    //       "dept": {
    //         "deptName": "研发部门"
    //       }
    //     },
    //     {
    //       "createBy": "admin",
    //       "createTime": "2023-04-23 16:11:38",
    //       "updateBy": null,
    //       "updateTime": null,
    //       "remark": "测试员",
    //       "userId": 2,
    //       "deptId": 105,
    //       "userName": "ry",
    //       "nickName": "智能车联",
    //       "email": "ry@qq.com",
    //       "phonenumber": "15666666666",
    //       "dept": {
    //         "deptName": "测试部门"
    //       }
    //     }
    //   ]
    // },
    // selectDept(){
    //   listDept().then(res => {
    //     console.log(res)
    //     this.deptList = res.date
    //   })
    // },


  },
};
</script>
