<template>
    <div class="JNPF-common-layout">
        <div class="JNPF-common-layout-left">
            <div class="JNPF-common-title">
                <h2>{{$t('common.organization')}}</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="6">
                        <el-form-item label="性别">
                            <el-select v-model="listQuery.gender" placeholder="请选择性别">
                                <el-option v-for="item in genderTreeData" :key="item.enCode"
                                    :label="item.fullName" :value="item.enCode" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="状态">
                            <el-select v-model="listQuery.enabledMark" placeholder="请选择状态">
                                <el-option label="启用" :value="1"></el-option>
                                <el-option label="禁用" :value="0"></el-option>
                                <el-option label="锁定" :value="2"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <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-form-item>
                    </el-col>
                </el-form>
            </el-row>
            <div class="JNPF-common-layout-main JNPF-flex-main">
                <div class="JNPF-common-head">
                    <topOpts @add="addOrUpdateHandle()">
                        <el-button type="text" icon="el-icon-download"
                            @click="exportForm">导出</el-button>
                        <el-button type="text" icon="el-icon-upload2"
                            @click="uploadForm">导入</el-button>
                        <el-button type="text" icon="icon-ym icon-ym-synForThird"
                            @click="handleSync">第三方同步</el-button>
                    </topOpts>
                    <div class="JNPF-common-head-right">
                        <el-tooltip effect="dark" :content="$t('common.refresh')" placement="top">
                            <el-link icon="icon-ym icon-ym-Refresh JNPF-common-head-icon"
                                :underline="false" @click="initData()" />
                        </el-tooltip>
                    </div>
                </div>
                <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" />
                    <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="140" />
                    <el-table-column prop="sortCode" label="排序" width="70" align="center" />
                    <el-table-column prop="enabledMark" label="状态" width="70" align="center">
                        <template slot-scope="scope">
                            <el-tag type="success" disable-transitions
                                v-if="scope.row.enabledMark == 1">启用
                            </el-tag>
                            <el-tag type="warning" disable-transitions
                                v-else-if="scope.row.enabledMark == 2">锁定
                            </el-tag>
                            <el-tag type="danger" disable-transitions v-else>禁用</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="syncItsm" label="ITSM账号" width="100" align="center">
                        <template slot-scope="scope">
                            <el-tag type="success" disable-transitions v-if="scope.row.syncItsm">有
                            </el-tag>
                            <el-tag type="danger" disable-transitions v-else>无
                            </el-tag>
                            <!-- <el-tag type="danger" disable-transitions v-else>禁用</el-tag> -->
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="150">
                        <template slot-scope="scope" v-if="!scope.row.isAdministrator">
                            <tableOpts @edit="addOrUpdateHandle(scope.row.id)"
                                @del="handleDel(scope.row.id)">
                                <el-dropdown hide-on-click>
                                    <span class="el-dropdown-link">
                                        <el-button size="mini" type="text">
                                            {{$t('common.moreBtn')}}<i
                                                class="el-icon-arrow-down el-icon--right"></i>
                                        </el-button>
                                    </span>
                                    <el-dropdown-menu slot="dropdown">
                                        <el-dropdown-item
                                            @click.native="handleResetPwd(scope.row.id, scope.row.account)">
                                            {{$t('user.resetPassword')}}
                                        </el-dropdown-item>
                                        <el-dropdown-item @click.native="unlockUser(scope.row.id)"
                                            v-if="scope.row.enabledMark == 2">解除锁定</el-dropdown-item>
                                        <el-dropdown-item @click.native="socialsBindBtn(scope.row)"
                                            v-if="useSocials">
                                            绑定管理
                                        </el-dropdown-item>
                                        <el-dropdown-item @click.native="workHandover(scope.row)"
                                            :disabled="scope.row.enabledMark == 1 || scope.row.enabledMark == 2">
                                            工作交接
                                        </el-dropdown-item>
                                        <el-dropdown-item
                                            @click.native="checkPermissions(scope.row)">
                                            查看权限</el-dropdown-item>
                                        <el-dropdown-item
                                            v-if="scope.row.mobilePhone && !scope.row.syncItsm"
                                            @click.native="generateITSMAccount(scope.row)">
                                            ITSM账号同步</el-dropdown-item>
                                        <el-dropdown-item @click.native="assignTags(scope.row)">
                                            分配标签</el-dropdown-item>
                                    </el-dropdown-menu>
                                </el-dropdown>
                            </tableOpts>
                        </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" />
        <ResetPwdForm v-if="resetFormVisible" ref="ResetPwdForm" @refreshDataList="initData" />
        <ExportForm v-if="exportFormVisible" ref="exportForm" />
        <ImportForm v-if="importFormVisible" ref="importForm" @refresh="reset()" />
        <SocialsBind v-if="socialsVisible" ref="SocialsBind" @close="socialsVisible=false" />
        <WorkHandoverForm v-if="workHandoverVisible" ref="WorkHandoverForm" @complete="initData" />

        <!--分配标签弹框  -->
        <TagForm v-if="tagFormVisible" ref="TagForm" />

        <CheckPermissions v-if="checkPermissionsVisible" ref="checkPermissions"
            @close="checkPermissionsVisible=false" />
    </div>
</template>
<script>
import { getDepartmentSelectorByAuth } from '@/api/permission/department'
import { getUserList, updateUserState, unlockUser, delUser, addItsmUser } from '@/api/permission/user'
import Form from './Form'
import Diagram from './Diagram'
import ResetPwdForm from './ResetPassword'
import ImportForm from './ImportForm'
import ExportForm from './ExportForm'
import TagForm from './TagForm'
import SocialsBind from './SocialsBind'
import WorkHandoverForm from './WorkHandoverForm.vue'
import { mapGetters } from "vuex"
import CheckPermissions from '@/components/CheckPermissions'
export default {
    name: 'permission-user',
    components: {
        Form,
        Diagram,
        ResetPwdForm,
        ExportForm,
        ImportForm,
        SocialsBind,
        WorkHandoverForm,
        CheckPermissions,
        TagForm
    },
    data() {
        return {
            genderTreeData: [],
            keyword: '',
            treeData: [],
            tableData: [],
            treeLoading: false,
            listLoading: true,
            listQuery: {
                organizeId: '',
                keyword: '',
                gender: '',
                enabledMark: '',
                currentPage: 1,
                pageSize: 20
            },
            defaultProps: {
                children: 'children',
                label: 'fullName'
            },
            total: 0,
            type: '',
            formVisible: false,
            diagramVisible: false,
            resetFormVisible: false,
            authorizeFormVisible: false,
            importFormVisible: false,
            exportFormVisible: false,
            expands: true,
            refreshTree: true,
            filterText: '',
            socialsVisible: false,
            workHandoverVisible: false,
            organizeIdTree: [],
            checkPermissionsVisible: false,

            // 是否展示分配标签弹框
            tagFormVisible: false
        }
    },
    watch: {
        filterText(val) {
            this.$refs.treeBox.filter(val)
        }
    },
    computed: {
        ...mapGetters(['userInfo']),
        useSocials() {
            return localStorage.getItem('useSocials') && localStorage.getItem('useSocials') != '0'
        }
    },
    created() {
        this.getOrganizeList(true)
    },
    methods: {

        // 打开分配标签弹框
        assignTags(row) {
            this.tagFormVisible = true
            this.$nextTick(() => {
                this.$refs.TagForm.init(row)
            })
        },
        workHandover(data) {
            this.workHandoverVisible = true
            this.$nextTick(() => {
                this.$refs.WorkHandoverForm.init(data)
            })
        },
        showDiagram() {
            this.diagramVisible = true
            this.$nextTick(() => {
                this.$refs.Diagram.init()
            })
        },
        checkPermissions(item) {
            this.checkPermissionsVisible = true
            this.$nextTick(() => {
                item = { ...item, type: 'user', fullName: item.realName }
                this.$refs.checkPermissions.init(item, 2)
            })
        },
        //同步ITSM账号
        generateITSMAccount(row) {
            console.log(row)
            let params = {
                mobile: row.mobilePhone,
                name: row.realName
            }
            this.listLoading = true
            addItsmUser(params).then(res => {
                if (res.code == 200) {
                    this.$message.success('同步成功')
                } else {
                    this.$message.warning(res.msg)
                }
                this.initData()
            })
        },
        search() {
            this.listQuery.currentPage = 1
            this.listQuery.pageSize = 20
            this.listQuery.sort = 'desc'
            this.initData()
        },
        reset() {
            this.listQuery.keyword = ''
            this.listQuery.gender = ''
            this.listQuery.enabledMark = ''
            this.search()
        },
        toggleExpand(expands) {
            this.filterText = ''
            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;
        },
        getOrganizeList(isInit) {
            this.filterText = ''
            this.treeLoading = true
            // 获取性别
            this.$store.dispatch('base/getDictionaryData', { sort: 'sex' }).then(res => {
                this.genderTreeData = res
            })
            getDepartmentSelectorByAuth().then(res => {
                this.treeData = res.data.list
                this.treeLoading = false
                if (isInit) this.initData()
            }).catch(() => {
                this.treeLoading = false
            })
        },
        initData() {
            this.listLoading = true
            getUserList(this.listQuery).then(res => {
                this.tableData = res.data.list
                this.total = res.data.pagination.total
                this.listLoading = false
            }).catch(() => {
                this.listLoading = false
            })
        },
        getNodePath(node) {
            let fullPath = []
            const loop = (node) => {
                if (node.level) fullPath.unshift(node.data)
                if (node.parent) loop(node.parent)
            }
            loop(node)
            return fullPath
        },
        handleNodeClick(data, node) {
            if (this.listQuery.organizeId === data.id) return
            this.listQuery.organizeId = data.id
            this.type = data.type
            const nodePath = this.getNodePath(node)
            this.organizeIdTree = nodePath.map(o => o.id)
            this.reset()
        },
        addOrUpdateHandle(id) {
            this.formVisible = true
            this.$nextTick(() => {
                this.$refs.Form.init(id, this.organizeIdTree || [])
            })
        },
        removeForm(isRefresh) {
            this.formVisible = false
            if (isRefresh) {
                this.keyword = ''
                this.initData()
            }
        },
        removeAuthorizeForm(isRefresh) {
            this.authorizeFormVisible = false
            if (isRefresh) {
                this.keyword = ''
                this.initData()
            }
        },
        handleUpdateState(row) {
            const txt = row.enabledMark ? '禁用' : '开启'
            this.$confirm(`您确定要${txt}当前用户吗, 是否继续?`, '提示', {
                type: 'warning'
            }).then(() => {
                updateUserState(row.id).then(res => {
                    this.$message({
                        type: 'success',
                        message: res.msg,
                        duration: 1000,
                        onClose: () => {
                            row.enabledMark = row.enabledMark ? 0 : 1
                        }
                    })
                })
            }).catch(() => { })
        },
        handleDel(id) {
            this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                type: 'warning'
            }).then(() => {
                delUser(id).then(res => {
                    this.$message({
                        type: 'success',
                        message: res.msg,
                        duration: 1500,
                        onClose: () => {
                            this.initData()
                        }
                    })
                })
            }).catch(() => { })
        },
        unlockUser(id) {
            this.$confirm('此操作将解除该账户锁定, 是否继续?', '解除锁定', {
                type: 'warning'
            }).then(() => {
                unlockUser(id).then(res => {
                    this.$message({
                        type: 'success',
                        message: res.msg,
                        duration: 1500,
                        onClose: () => {
                            this.initData()
                        }
                    })
                })
            }).catch(() => { })
        },
        exportForm() {
            this.exportFormVisible = true
            this.$nextTick(() => {
                this.$refs.exportForm.init(this.listQuery)
            })
        },
        uploadForm() {
            this.importFormVisible = true
            this.$nextTick(() => {
                this.$refs.importForm.init()
            })
        },
        handleResetPwd(id, account) {
            this.resetFormVisible = true
            this.$nextTick(() => {
                this.$refs.ResetPwdForm.init(id, account)
            })
        },
        socialsBindBtn(data) {
            this.socialsVisible = true
            this.$nextTick(() => {
                this.$refs.SocialsBind.init(data)
            })
        },
        handleSync() {
            this.$router.push({ path: `/system/sysConfig?type=1` })
        }
    }
}
</script>
<style lang="scss" scoped>
>>> .icon-ym {
    font-size: 12px !important;
}
</style>
