<template>
  <div class="app-container">
    <h4 class="form-header h4">基本信息</h4>
    55555555
      {{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-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"
                @change="addDepartment(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">
        {{userList}}
        <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="permissionsAdd(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 { addDepartment, permissionsUserAdd } from '@/api/kvt/kvt'

export default {
  name: "AuthDataSource",
  props: [
      "deptlists",
    "DataSource",
    "userList"
    ],
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "dept",
      // 分页信息
      total: 0,
      pageNum: 1,
      pageSize: 1,
      // 基本信息
      baseInfo: {},
      // 部门列表
      deptList: [],
      // // 用户列表
      // userList: []
    };
  },
  created() {
    this.loading = true;
    setTimeout(() => this.loading = false, 200)
  },
  watch: {

    'activeName.status'(newVal) {
      if (newVal === 0) {
        alert("闭关")

        // 开关为激活状态，执行相关逻辑
      } else if (newVal === 1) {
        alert("打开")
        // 开关为未激活状态，执行相关逻辑
      }
    },
    'userList.status'(newVal) {
      if (newVal === 0) {
        alert("闭关")

        // 开关为激活状态，执行相关逻辑
      } else if (newVal === 1) {
        alert("打开")
        // 开关为未激活状态，执行相关逻辑
      }
    }
  },
  methods: {
    //添加部门中以及部门下的子类及用户
    addDepartment(row){
      console.log("rrrow",row)
      const deptId =row.deptId
      console.log("Tht",this.DataSource)
      let data = {
        tbaleName:this.DataSource.databaseName,
        kvtId:this.DataSource.id,
        deptId :deptId
      }
      addDepartment(data).then(res => {
        this.$message.success(res.data)
      })
    },
    permissionsAdd(row) {
      console.log("rrrow", row)
      const userId =row.userId
      const deptId =row.dept.deptId
      console.log("Tht",this.DataSource)
      let data = {
        tbaleName:this.DataSource.databaseName,
        kvtId:this.DataSource.id,
        userId: userId,
        deptId :deptId
      }
      console.log("dataaaa",data)
      permissionsUserAdd(data).then(res => {
        this.$message.success(res.data)
      })
    },
    click(row){
      console.log("rrrrrrrrrrr",row)
    },
    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 = [];
    //     listDept().then(res => {
    //       console.log(res)
    //       this.deptList = res.date
    //     })

        // "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": []
        //   },
        //   {
        //     "createBy": 1,
        //     "createTime": "2023-09-29 11:47:27",
        //     "updateBy": null,
        //     "updateTime": null,
        //     "deptId": 103,
        //     "parentId": 101,
        //     "ancestors": "0,100,101",
        //     "deptName": "研发部门",
        //     "orderNum": 1,
        //     "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": "测试部门"
      //     }
      //   }
      // ]
    // }
  },

};
</script>
