<template>
  <div class="app-container">
		<el-row :gutter="20">
			<!--部门数据-->
      <el-col :span="4" :xs="24">
				<div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
			</el-col>
			<!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="钉钉名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入钉钉名称"
              clearable
              size="small"
              style="width: 140px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号码" prop="mobile">
            <el-input
              v-model="queryParams.mobile"
              placeholder="请输入手机号码"
              clearable
              size="small"
              style="width: 140px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="cyan"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="3.5">
            <el-button
              type="primary"
              icon="el-icon-upload2"
              size="mini"
              :loading="deptIdLoading"
              @click="handleDingTalkDept"
              >同步所有部门</el-button
            >
          </el-col>
          <el-col :span="3.5">
            <el-button
              type="primary"
              icon="el-icon-upload2"
              size="mini"
              @click="handleSynchro"
              :loading="dingdingLoading"
              >同步所有钉钉人员信息</el-button
            >
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>

        <el-table
          v-loading="loading"
          :data="dingUserList"
        >
          <el-table-column label="用户编号" prop="id" />
          <el-table-column
            label="钉钉用户"
            prop="name"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <div style="display: flex; align-items: center; height: 30px">
                <el-avatar shape="square" size="small" :src="scope.row.avatar"></el-avatar>
                <span style="margin-left: 5px">{{scope.row.name}}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="手机号码"
            align="center"
            prop="mobile"
          />
          
          <el-table-column
            label="操作"
            align="center"
            width="240"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-upload2"
                @click="handleUpdate(scope.row)"
                >同步信息</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
			</el-col>
		</el-row>
	</div>
</template>

<script>
import { listDingUser, dingTalkDept, dingTalkUser, dingTalk, getDingList } from "@/api/system/dingUser"
export default {
  name: "DingdingUser",
  data() {
    return {
      talkLoading: false,
      dingdingLoading: false,
      deptIdLoading: false,
      total: 0,
      showSearch: true,
			// 遮罩层
      loading: true,
      // 部门树选项
      deptOptions: undefined,
			defaultProps: {
        children: "children",
        label: "name",
        value: 'deptId'
      },
			// 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        mobile: undefined,
        deptId: undefined
      },
      dingUserList: []
    };
  },
  created() {
    this.getList()
    this.getTreeselect();
  },
	methods: {
    /**  */
    handleUpdate(row) {
      dingTalk(row.userid).then(res => {
        this.msgSuccess("同步成功");
      })
    },
    /** 同步所有钉钉 */
    handleSynchro() {
      this.dingdingLoading = true;
      dingTalkUser().then(res => {
        this.dingdingLoading = false;
        this.msgSuccess("同步成功");
      })
    },
    /** 同步所有部门 */
    handleDingTalkDept() {
      this.deptIdLoading = true
      dingTalkDept().then(res => {
        this.deptIdLoading = false
        this.msgSuccess("同步成功");
      })
    },
		/** 查询钉钉用户列表 */
    getList() {
      this.loading = true;
      listDingUser(this.queryParams).then(
        response => {
          this.dingUserList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.deptId = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      getDingList().then(response => {
        this.deptOptions = this.changeTree(response.data);
      });
    },
    changeTree(tree) {
      if (Array.isArray(tree) && tree.length > 0) {
        let tre = tree.map(item => {
          let { children, ...ite } = item
          if (children && children.length > 0) {
            let ee = this.changeTree(children)
            return {
              children: ee,
              ...ite
            }
          } else {
            return ite
          }
        })
        return tre
      } else {
        return tree
      }
    },
		// 节点单击事件
    handleNodeClick(data) {
      console.log(data);
      this.queryParams.deptId = data.deptId;
      this.getList();
    },
		// 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
	}
};
</script>