<template>
    <div class="JNPF-common-layout">
        <div class="JNPF-common-layout-left">
            <div class="JNPF-common-title">
                <h2>部门</h2>
                <span class="options">
                    <el-dropdown>
                        <el-link icon="icon-ym icon-ym-mpMenu" :underline="false" />
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                                @click.native="getOrganizeList()">刷新数据</el-dropdown-item>
                            <el-dropdown-item
                                @click.native="toggleExpand(true)">展开全部</el-dropdown-item>
                            <el-dropdown-item
                                @click.native="toggleExpand(false)">折叠全部</el-dropdown-item>
                            <el-dropdown-item @click.native="showDiagram">架构图</el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </span>
            </div>
            <div class="JNPF-common-tree-search-box">
                <el-input placeholder="请输入部门" v-model="filterText" suffix-icon="el-icon-search"
                    clearable />
            </div>
            <el-scrollbar class="JNPF-common-el-tree-scrollbar" v-loading="treeLoading">
                <el-tree ref="treeBox" :data="treeData" :props="defaultProps"
                    :default-expand-all="expands" highlight-current :expand-on-click-node="false"
                    node-key="id" @node-click="handleNodeClick" class="JNPF-common-el-tree"
                    v-if="refreshTree" :filter-node-method="filterNode">
                    <span class="custom-tree-node" slot-scope="{ data, node }"
                        :title="data.fullName">
                        <i :class="data.icon" />
                        <span class="text" :title="data.fullName">{{ node.label }}</span>
                    </span>
                </el-tree>
            </el-scrollbar>
        </div>
        <div class="JNPF-common-layout-center JNPF-flex-main">
            <el-row class="JNPF-common-search-box" :gutter="16">
                <el-form @submit.native.prevent>
                    <el-col :span="6">
                        <el-form-item :label="$t('common.keyword')">
                            <el-input v-model="listQuery.keyword"
                                :placeholder="$t('common.enterKeyword')" clearable
                                @keyup.enter.native="search()" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search" @click="search()">
                                {{ $t("common.search") }}
                            </el-button>
                            <el-button icon="el-icon-refresh-right"
                                @click="reset()">{{ $t("common.reset") }}
                            </el-button>
                            <el-button icon="el-icon-refresh-right"
                                @click="synchronizeBureauSystemId()">同步厅局系统ID
                            </el-button>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>
            <div class="JNPF-common-layout-main JNPF-flex-main">
                <JNPF-table v-loading="listLoading" :data="tableData">
                    <el-table-column prop="account" label="账号" width="100" />
                    <el-table-column prop="realName" label="姓名" width="100" />
                    <el-table-column prop="gender" label="性别" width="90" align="center">
                        <template slot-scope="scope" sortable>
                            <span>{{
                scope.row.gender == 1
                  ? "男"
                  : scope.row.gender == 2
                  ? "女"
                  : "保密"
              }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="mobilePhone" label="手机号" width="120" />
                    <el-table-column prop="organize" label="所属组织" min-width="200"
                        show-overflow-tooltip />
                    <el-table-column prop="creatorTime" label="创建时间"
                        :formatter="jnpf.tableDateFormat" width="120" />
                    <el-table-column label="操作" width="150">
                        <template slot-scope="scope">
                            <div @click="showForm(scope.row)" style="color:#1890ff;cursor:pointer">
                                编辑系统权限
                            </div>
                        </template>
                    </el-table-column>
                </JNPF-table>
                <pagination :total="total" :page.sync="listQuery.currentPage"
                    :limit.sync="listQuery.pageSize" @pagination="initData" />
            </div>
        </div>

        <!-- 编辑 -->
        <Form v-show="formVisible" ref="Form" @close="removeForm" />

        <!-- 架构图 -->
        <Diagram v-if="diagramVisible" ref="Diagram" @close="diagramVisible = false" />
    </div>
</template>
<script>
import { getDepartmentSelectorByAuth } from "@/api/permission/department";
import { getUserList } from "@/api/permission/user";
import Form from "./Form";
import Diagram from "./Diagram";

import {
    synchronizeOrganizeRelation
} from "@/api/governmentCloud/officeSystemPermissions/officeSystemPermissions";

export default {
    name: "permission-user",
    components: {
        Form,
        Diagram
    },
    data() {
        return {
            keyword: "",
            treeData: [],
            tableData: [],
            treeLoading: false,
            listLoading: true,
            listQuery: {
                organizeId: "",
                keyword: "",
                currentPage: 1,
                pageSize: 20
            },
            defaultProps: {
                children: "children",
                label: "fullName"
            },
            total: 0,
            type: "",
            formVisible: false,
            diagramVisible: false,
            expands: true,
            refreshTree: true,
            filterText: ""
        };
    },
    watch: {
        filterText(val) {
            this.$refs.treeBox.filter(val);
        }
    },
    computed: {},
    created() {
        this.getOrganizeList(true);
    },
    methods: {
        // 多选框选中数据
        handleSelectionChange(selection) {
            console.log(selection);
            this.multipleSelection = selection;
            this.$nextTick(() => {
                // this.changePageCoreRecordData();
            });
        },
        showDiagram() {
            this.diagramVisible = true;
            this.$nextTick(() => {
                this.$refs.Diagram.init();
            });
        },
        showForm(row) {
            this.formVisible = true;
            this.$nextTick(() => {
                this.$refs.Form.init(row);
            });
        },
        search() {
            this.listQuery.currentPage = 1;
            this.listQuery.pageSize = 20;
            this.listQuery.sort = "desc";
            this.initData();
        },
        reset() {
            this.listQuery.keyword = "";
            this.search();
        },
        toggleExpand(expands) {
            this.refreshTree = false;
            this.expands = expands;
            this.$nextTick(() => {
                this.refreshTree = true;
                this.$nextTick(() => {
                    this.$refs.treeBox.setCurrentKey(this.companyId);
                });
            });
        },
        filterNode(value, data) {
            if (!value) return true;
            return data.fullName.indexOf(value) !== -1;
        },
        // 同步厅局系统ID
        synchronizeBureauSystemId() {
            synchronizeOrganizeRelation().then(res => {
                if (res.code == 200) {
                    this.$message({
                        type: "success",
                        message: res.msg,
                        duration: 1000
                    });
                }
            })
        },
        getOrganizeList(isInit) {
            this.filterText = "";
            this.treeLoading = true;
            getDepartmentSelectorByAuth()
                .then(res => {
                    this.treeData = res.data.list;
                    this.treeLoading = false;
                    if (isInit) this.initData();
                })
                .catch(() => {
                    this.treeLoading = false;
                });
        },
        initData() {
            this.listLoading = true;
            console.log(this.listQuery);
            getUserList(this.listQuery)
                .then(res => {
                    this.tableData = res.data.list;
                    this.total = res.data.pagination.total;
                    this.listLoading = false;
                })
                .catch(() => {
                    this.listLoading = false;
                });
        },
        handleNodeClick(data) {
            if (this.listQuery.organizeId === data.id) return;
            this.listQuery.organizeId = data.id;
            this.type = data.type;
            this.reset();
        },

        removeForm(isRefresh) {
            this.formVisible = false;
            if (isRefresh) {
                this.keyword = "";
                this.initData();
            }
        }
    }
};
</script>
