<template>
    <div class="app-container pd10">
        <el-row :gutter="40">
            <el-col :span="24" :xs="24">
                <el-form ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
                    <el-form-item>
                        <el-button icon="el-icon-check" type="primary" @click="handleAuditSubmit">审核用户</el-button>
                    </el-form-item>
                </el-form>

                <el-table
                    stripe
                    highlight-current-row
                    element-loading-text="数据载入中"
                    tooltip-effect="dark"
                    @selection-change="handleSelectionChange"
                    header-cell-class-name="table_header"
                    :height="vh + 'px'"
                    border
                    :data="tables"
                >
                    <el-table-column align="center" type="selection" width="65" prop="userIndex"></el-table-column>
                    <el-table-column align="center" label="名称" prop="name" />
                    <el-table-column align="center" label="登录名" prop="loginName" />
                    <el-table-column align="center" label="角色" :formatter="gName" prop="role" />
                    <el-table-column align="center" label="部门" prop="deptId" />
                    <el-table-column align="center" label="电话" prop="phone" />
                    <el-table-column align="center" label="注册时间" :show-overflow-tooltip="true" prop="registerTime">
                        <template slot-scope="scope">{{ scope.row.registerTime | formatDate }}</template>
                    </el-table-column>
                    <el-table-column align="center" label="类型" prop="casualUser" width="100">
                        <template slot-scope="scope">
                            <span v-if="scope.row.casualUser === 0" type="primary" size="small">临时用户</span>
                            <span v-if="scope.row.casualUser === 1" type="success" size="small">长期用户</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="状态" prop="state" width="150">
                        <template>
                            <span type="warning" size="small">待审核</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="操作" width="260">
                        <template slot-scope="scope">
                            <el-button
                                type="primary"
                                icon="el-icon-check"
                                size="mini"
                                @click.stop="handleAudit(scope.row)"
                            >
                                审核通过
                            </el-button>
                            <el-button type="primary" size="mini" @click.stop="handleDelete(scope.row)">
                                审核不通过
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination
                    background
                    class="mt10"
                    align="right"
                    v-show="page.total > 0"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="page.current"
                    :page-sizes="[5, 10, 15]"
                    :page-size="page.size"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="page.total"
                ></el-pagination>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import { listsh, checkUser } from '@/api/admin/user'
import { gongkey } from '@/api/user'
import { sm3Digest } from '@/assets/commjs/sm3'
import { sm2, encrypt } from '@/assets/commjs/sm2.js'
export default {
    computed: {
        // 实时监听表格
        tables: function () {
            const search = this.search
            if (search) {
                return this.pageList.filter(dataNews => {
                    return Object.keys(dataNews).some(key => {
                        return String(dataNews[key]).toLowerCase().indexOf(search) > -1
                    })
                })
            }
            return this.pageList
        }
    },

    filters: {
        formatDate: function (value) {
            //这里的 value 就是需要过滤的数据
            var date = new Date(value)
            var year = date.getFullYear()
            var month = date.getMonth() + 1
            var day = date.getDate()
            var hours = date.getHours()
            var minutes = date.getMinutes()
            var seconds = date.getSeconds()
            return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
        }
    },
    data() {
        return {
            // 遮罩层
            vh: null,
            loading: true,
            queryParams: {
                casualUser: 0,
                orderBy: '',
                searchBeginTime: '',
                searchEndTime: '',
                searchState: 0,
                searchValue: '',
                sortBy: ''
            },
            page: {
                current: 1,
                size: 15,
                total: 0
            },
            pageList: [],
            multipleSelection: []
        }
    },
    created() {
        this.loadData()
    },
    mounted() {
        this.setHeight()
        window.addEventListener('resize', this.setHeight)
    },
    beforeDestroy() {
        window.removeEventListener('resize', this.setHeight)
    },
    methods: {
        setHeight() {
            this.vh = window.sessionStorage.getItem('appheight') - 106
        },
        gName(row, column) {
            var gNamelist = []
            for (var i = 0; i < row.role.length; i++) {
                gNamelist.push(row.role[i])
            }
            return gNamelist.join()
        },
        loadData() {
            this.handleQuery()
        },
        handleQuery() {
            this.queryParams.pageNum = this.page.current
            this.queryParams.pageSize = this.page.size
            listsh(this.queryParams).then(response => {
                if (response.code === 'A0000') {
                    const { data } = response
                    this.pageList = data.records
                    this.page.total = data.total
                    this.page.size = data.pageSize
                    this.loading = false
                }
                if (response.code === 'A0110') {
                    this.$message.success('暂无用户')
                    const { data, total } = response
                    this.pageList = []
                    this.loading = false
                }
            })
        },
        handleSizeChange(val) {
            this.page.size = val
            this.handleQuery()
        },
        handleCurrentChange(val) {
            this.page.current = val
            this.handleQuery()
        },
        handleUnlock(row) {},
        handleReset() {},
        submint() {},
        handleSelectionChange(val) {
            this.multipleSelection = val
            // alert(val)
            console.log(val)
        },
        handleAudit(row) {
            // alert('审核')
            let arr = []
            let id = row.id
            arr.push(id)
            checkUser(arr).then(response => {
                if (response.code === 'A0000') {
                    this.$message({ message: response.message, type: 'success' })
                }
                this.handleQuery()
            })
        },
        handleDelete() {
            // alert('删除')
        },
        handleAuditSubmit() {
            // alert('批量提交')
            var arr = []
            for (var i = 0; i < this.multipleSelection.length; i++) {
                arr.push(this.multipleSelection[i].id)
            }
            checkUser(arr).then(response => {
                if (response.code === 'A0000') {
                    this.$message({ message: response.message, type: 'success' })
                }
                this.handleQuery()
            })
        }
    }
}
</script>
<style lang="less" scoped>
@import url('../../../../styles/comStyle.less');
</style>
