<template>
    <el-dialog :title="dialogTitle" :visible.sync="show" class="custom-dialog" destroy-on-close :before-close="onClose">
        <el-form v-if="role" :inline="true" :model="formData" ref="form" label-width="80px">
            <el-form-item label="姓名:" prop="name">
                <el-input v-model="formData.name"></el-input>
            </el-form-item>
            <el-form-item label="所属机构:" prop="enforceOrgs">
                <el-cascader
                    :options="orgOptions"
                    class="user-cascader"
                    v-model="formData.enforceOrgs"
                    :props="orgProps"
                    :show-all-levels="false"
                    clearable>
                </el-cascader>
            </el-form-item>
            <el-button class="ml" icon="el-icon-refresh-right" @click="resetForm">重置</el-button>
            <el-button type="primary" icon="el-icon-search" @click="loadUsers">查询</el-button>
        </el-form>
        <el-table border :data="userList" v-loading="isLoading">
            <el-table-column prop="name" show-overflow-tooltip label="姓名" />
            <el-table-column prop="sex" show-overflow-tooltip label="性别" />
            <el-table-column v-if="!role" prop="role" show-overflow-tooltip label="角色" />
            <el-table-column v-if="!org" prop="enforce_org_name" show-overflow-tooltip label="机构" />
            <el-table-column prop="position" show-overflow-tooltip label="检查职务" />
            <el-table-column prop="phone_num" show-overflow-tooltip label="联系方式" />
            <el-table-column prop="user_status" show-overflow-tooltip label="账号状态" align="center" width="154">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.user_status == 1" type="warning">冻结</el-tag>
                    <el-tag v-else-if="scope.row.user_status == 2" type="danger">停用</el-tag>
                    <el-tag v-else type="success">正常</el-tag>
                </template>
            </el-table-column>
        </el-table>
        <Page :page="query.page" :size="query.size" :pageSizes="[5, 10, 15, 20]" :total="total" @pageChange="pageChange"></Page>
    </el-dialog>
</template>

<script>
import { getUsers } from '@/api/system_user';
import { notEmpty } from '@/utils/tool';
import { orgMixin } from '@/mixin/org';

export default {
    props: ['show', 'org', 'role'],
    mixins: [orgMixin],
    data () {
        return {
            formData: {
                name: '',
                enforceOrgs: '',
            },
            query: {
                page: 1,
                size: 5,
                sort: 'create_time desc'
            },
            total: 0,
            isLoading: false,
            userList: [],
            positionDic: {
                1: '主办人员',
                2: '协办人员',
                3: '主/协办人员'
            },
        }
    },
    computed: {
        dialogTitle() {
            let s = '成员';
            if (this.org && this.org.enforce_org_name) {
                s = this.org.enforce_org_name + s;
            }
            if (this.role && this.role.name) {
                s = this.role.name + s;
            }
            return s;
        },
    },
    watch: {
        org: {
            handler(n) {
                if (n) {
                    this.loadUsers();
                }
            },
            immediate: true,
            deep: true,
        },
        role: {
            handler(n) {
                if (n) {
                    this.loadUsers();
                }
            },
            immediate: true,
            deep: true,
        },
    },
    mounted () {
        this.loadOrgTree();
    },
    methods: {
        onClose() {
            this.$emit('update:show', false);
            this.$emit('close');
        },
        pageChange(page, size) {
            this.query.page = page;
            this.query.size = size;
            this.loadUsers();
        },
        resetForm() {
            this.$refs.form.resetFields();
            this.query.page = 1;
            this.loadUsers();
        },
        loadUsers() {
            this.isLoading = true;
            let query = [];
            if (this.org) {
                query.push(`enforce_org_id==${this.org.enforce_org_id}`);
            }
            if (this.role) {
                this.query.role_id = this.role.id;
                if (notEmpty(this.formData.name)) {
                    query.push(`name==*${this.formData.name.trim()}*`);
                }
                if (this.formData.enforceOrgs && this.formData.enforceOrgs.length > 0) {
                    const orgId = this.formData.enforceOrgs[this.formData.enforceOrgs.length - 1];
                    if (notEmpty(orgId)) {
                        query.push(`enforce_org_id==*${orgId}*`);
                    }
                }
            }
            this.query.query = query.join(';');
            getUsers(this.query).then(res => {
                this.total = res.total || 0;
                const list = res.data || [];
                list.map(item => {
                    if (item.sex) {
                        item.sex = item.sex == 1 ? '女' : '男';
                    }
                    if (item.position) {
                        item.position = this.positionDic[item.position] || '';
                    }
                    let role = [];
                    if (item.roles && item.roles.length > 0) {
                        item.roles.map(obj => {
                            if (notEmpty(obj.name)) {
                                role.push(obj.name);
                            }
                        });
                    }
                    item.role = role.join('/');
                });
                this.userList = list;
                this.isLoading = false;
            });
        }
    }
}
</script>