<template>
    <div class="container-accountability userManagerPage">
        <el-form ref="formData" :model="formData" :inline="true" class="search-bm-boder">
            <el-form-item label="单位名称:" label-width="72px">
                <el-cascader v-model='formData.orgCode' :options="orgList" clearable :props='props' v-if="areaLevel == '120000'" class="inputCompatible"></el-cascader>
                <el-select placeholder="请选择" v-model="formData.orgCode" clearable class="inputCompatible" v-else>
                    <el-option v-for="item in orgList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="状态:" label-width="44px">
                    <el-select v-model="formData.isDisavled" clearable placeholder="请选择" class="inputCompatible">
                        <el-option v-for="(item,index) in typeData" :key="index" :label="item.name" :value="item.status"></el-option>
                    </el-select>
                </el-form-item>
            <el-form-item>
                <div class="search-button">
                    <span class="search-line"></span>
                    <el-button type="primary" class="iconfont iconchaxun" size="small"  @click="queryFun">查询</el-button>
                    <el-button type="primary" size="small" class="iconfont iconzhongzhi" @click="resetFun">重置</el-button>
                </div>
            </el-form-item>
        </el-form>
        <el-row class="addBtnBg">
            <el-button type="primary" size="small" class="iconfont iconxinzeng" @click="goAdd">新增</el-button>
        </el-row>
        <el-row>
            <el-col :span="24">
                <!-- @row-click="tableRowClick" -->
                <el-table
                    :data="tableList"
                    v-loading="loading"
                    border
                    stripe
                    highlight-current-row
                >
                    <el-table-column align="center" label="序号" width="60">
                        <template slot-scope="scope">
                            <span>{{scope.$index+1}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="orgName" label="单位名称" align="center" sortable :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="areaName" label="所属地区" align="center" sortable :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="userLoginName" label="帐号" align="center" sortable :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="createDate" label="创建时间" align="center" sortable :show-overflow-tooltip="true" width="200">
                        <template slot-scope="scope">
                            <span>{{ dateFormat(scope.row.createDate) }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="isDisavled" label="状态" align="center" sortable :show-overflow-tooltip="true" width="200">
                        <template slot-scope="scope">
                            <span v-if="scope.row.isDisavled == 0">已启用</span>
                            <span v-if="scope.row.isDisavled == 1">已禁用</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" width="150">
                        <template slot-scope="scope">
                            <el-tooltip class="item" effect="light" content="查看" placement="top">
                                <el-button
                                    type="primary"
                                    size="small"
                                    class="iconfont iconchakan2 operate_show_btn"
                                    @click.stop="goSee(scope.row)"
                                ></el-button>
                            </el-tooltip>
                            <el-tooltip class="item" effect="light" content="编辑" placement="top" v-if="buttonNameList.indexOf('编辑') > -1">
                                <el-button
                                    type="primary"
                                    size="small"
                                    class="iconfont iconxiugai1 operate_edit_btn"
                                    @click.stop="goEdit(scope.row)"
                                ></el-button>
                            </el-tooltip>
                            <!-- iconzhongzhimima -->
                            <el-tooltip class="item" effect="light" content="重置密码" placement="top" v-if="buttonNameList.indexOf('重置密码') > -1">
                                <el-button
                                    type="primary"
                                    size="small"
                                    class="iconfont iconzhongzhimima operate_issued_btn"
                                    @click.stop="goReset(scope.row)"
                                ></el-button>
                            </el-tooltip>
                            <el-tooltip class="item" effect="light" content="启用" placement="top" v-if="buttonNameList.indexOf('启用') > -1 && scope.row.isDisavled == 1">
                                <el-button
                                    type="primary"
                                    size="small"
                                    class="iconfont iconqiyong1 operate_withdraw_btn"
                                    :disabled='scope.row.isDisabled'
                                    @click="goEnable(scope.row,scope.$index,'use')"
                                ></el-button>
                            </el-tooltip>
                            <el-tooltip class="item" effect="light" content="禁用" placement="top" v-if="buttonNameList.indexOf('禁用') > -1 && scope.row.isDisavled == 0">
                                <!-- v-if="scope.row.STATUS == 10" -->
                                <el-button
                                    type="primary"
                                    size="small"
                                    class="iconfont iconjinyong operate_withdraw_btn"
                                    :disabled='scope.row.isDisabled'
                                    @click="godisabled(scope.row,scope.$index)"
                                ></el-button>
                            </el-tooltip>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pag-right clearFix">
                    <el-pagination
                        class="right"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page.sync="page.page"
                        :page-sizes="[10,20,30,40]"
                        :page-size="page.size"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="page.total"
                        v-if="paginationShow"
                    ></el-pagination>
                </div>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import { authorityControl } from "@/api/login/login"; 
import * as management from "@/api/headResponsibility/infoManager"
import { searchBranch } from "@/api/headResponsibility/selfAudit"
export default {
    data() {
        return {
            formData: {
                orgCode: null,
                isDisavled: null,//状态
                userType: '3',
                countyCode: localStorage.getItem('countyCode') == '120000' ? '' : localStorage.getItem('countyCode')
            },
            typeData: [
                {
                    name: '已启用',
                    status: '0'
                },
                {
                    name: '已禁用',
                    status: '1'
                }
            ],
            paginationShow: true,
            tableList: [],
            loading: false,
            page: {
                page: 1,
                size: 10,
                total: 0
            },
            orgList: [],
            areaLevel: '',
            props: { value: 'id',label: 'name',children: 'orgList',emitPath: false },
            buttonNameList: []
        }
    },
    created() {
        this.areaLevel = sessionStorage.getItem('countyCode')
        this.getOrgList()
        this.getButtonList(sessionStorage.getItem("resourceCode"),sessionStorage.getItem("userCode")) 
        this.getList('start')
    },
    methods: {
        getButtonList(menuCode,userCode) {
          let codeData = {
              resourceCode:menuCode,
              userId:userCode,
          }
          authorityControl(codeData).then(res => {
             let buttonNameList = []
             res.data.forEach(item => {
                 this.buttonNameList.push(item.resourceName)
             })
          })
        },
        getList(type) {
            if (type != "start") {//非初始化调用：查询、分页
                sessionStorage.setItem('searchData', JSON.stringify(this.formData))
                sessionStorage.setItem('pageData', JSON.stringify(this.page))
            } else {//初始化分两种情况，详情页等返回后的初始化，以及第一次进入此菜单的初始化
                if(JSON.parse(sessionStorage.getItem('searchData'))){//详情页返回后，将对应查询条件和分页的字段赋值，页面回显参数
                    this.formData = JSON.parse(sessionStorage.getItem('searchData'))
                    this.page = JSON.parse(sessionStorage.getItem('pageData'))
                }else {//第一次进去此菜单时，保存上方默认的data
                    sessionStorage.setItem('searchData', JSON.stringify(this.formData))
                    sessionStorage.setItem('pageData', JSON.stringify(this.page))
                }
            }
            this.loading = true
            management.userManagerList(this.formData,this.page).then(res => {
                if(res.infoCode == 200) {
                    this.tableList = res.data.content
                    this.page.total = res.data.totalElements
                }else {
                    this.$message({
                        type: 'error',
                        message: res.msg,
                        duration: 2000
                    })
                }
                this.loading = false
            })
        },
        queryFun() {//查询
            this.paginationShow = false;
            this.handleCurrentChange(1);
            this.$nextTick(() => {
                this.paginationShow = true;
            });
        },
        resetFun() {//重置
            this.formData = {
                orgCode: null,
                isDisavled: null,//状态
                userType: '3',
                countyCode: localStorage.getItem('countyCode') == '120000' ? '' : localStorage.getItem('countyCode')
            }
            this.queryFun()
        },
        getOrgList() {
            if(this.areaLevel == '120000') {//市级
                this.getOrgTree()
            }else {
                searchBranch("0",sessionStorage.getItem('countyCode')).then(res => {
                    if (res.infoCode == 200) {
                        this.orgList = res.data;
                    }else {
                        this.$message({
                            type: 'error',
                            message: res.msg,
                            duration: 2000
                        })
                    }
                });
            }
        },
        getOrgTree() {//市级时获取所有的机构
            management.allOrg().then(res => {
                if(res.infoCode == 200) {
                    this.orgList = res.data
                }else {
                    this.$message({
                        type: 'error',
                        message: res.msg,
                        duration: 2000
                    })
                }
            })
        },
        tableRowClick(row, column, event) {
            if (event.label != "操作") {
                // this.goSee(row)
            }
        },
        goAdd() {//新增
            this.$router.push({
                path: '/accountability/userManagerAdd',
                query: {
                    state: 'add'
                }
            })
        },
        goSee(row) {//查看
            this.$router.push({
                path: '/accountability/userManagerAdd',
                query: {
                    userCode: row.userCode,
                    state: 'show'
                }
            })
        },
        goEdit(row) {//修改
            this.$router.push({
                path: '/accountability/userManagerAdd',
                query: {
                    userCode: row.userCode,
                    orgCode: row.orgCode,
                    state: 'upDate'
                }
            })
        },
        goReset(row) {//重置密码
            management.resetPwd(row.userCode).then(res => {
                if(res.infoCode == 200) {
                    this.$message({
                        type: 'success',
                        message: '重置密码成功',
                        duration: 2000
                    })
                    this.getList()
                }else {
                    this.$message({
                        type: 'error',
                        message: res.msg,
                        duration: 2000
                    })
                }
            })
        },
        goEnable(row,ind,isUse) {//启用
            this.freezeCommon(row.userCode,ind,isUse)
        },
        godisabled(row,ind) {//禁用
            this.freezeCommon(row.userCode,ind)
        },
        freezeCommon(code,ind,isUse) {
            this.tableList[ind].isDisabled = true
            management.freezeSelUser(code).then(res => {
                if(res.infoCode == 200) {
                    this.$message({
                        type: 'success',
                        message: isUse == 'use' ? '启用成功' : '禁用成功',
                        duration: 2000
                    })
                    this.getList()
                }else {
                    this.$message({
                        type: 'error',
                        message: res.msg,
                        duration: 2000
                    })
                    this.tableList[ind].isDisabled = false
                }
            })
        },
        handleSizeChange(val) {//条数改变
            this.page.size = val
            this.getList()
        },
        handleCurrentChange(val) {//页数改变
            this.page.page = val
            this.getList()
        },
        // 时间格式化函数
        dateFormat(time) {
            if (time) {
                var day = time.split("T");
                //获取日期部分的年月日
                var days = day[0].split("-");
                return days[0] + "-" + days[1] + "-" + days[2];
            } else {
                return "";
            }
        }
    }
}
</script>
<style lang="scss">
</style>
