<template>
    <div class="detail" v-loading="loading">
        <KVTable title="人员详情" :list="list" :rightButton="rightButton">
            <template #slotValue="slot">
                <template v-if="slot.row.type === 1">
                    <el-tag v-if="slot.row.value == 1" type="warning">冻结</el-tag>
                    <el-tag v-else-if="slot.row.value == 2" type="danger">停用</el-tag>
                    <el-tag v-else type="success">正常</el-tag>
                </template>
                <template v-else>
                    {{ slot.row.value }}
                </template>
            </template>
        </KVTable>
    </div>
</template>

<script>
import KVTable from '@/component/KVTable'
import { formatDate, notEmpty } from '@/utils/tool';

import { getUser } from '@/api/system_user';

export default {
    components: {
        KVTable,
    },
    data () {
        return {
            loading: false,
            rightButton: {},
            list: [],
            positionDic: {
                1: '主办人员',
                2: '协办人员',
                3: '主/协办人员'
            },
            roles: [],
        }
    },
    computed: {
        userId() {
            return this.$route.params.id;
        },
    },
    mounted () {
        if (notEmpty(this.userId)) {
            this.loadDetail();
        }
    },
    methods: {
        loadDetail() {
            this.loading = true;
            getUser(this.userId).then(res => {
                this.setupList(res);
                this.roles = res.roles || [];
                this.loading = false;
            });
        },
        setupList(user) {
            let create = '';
            if (user && user.create_time) {
                create = formatDate(user.create_time, true);
            }
            let update = '';
            if (user && user.update_time) {
                update = formatDate(user.update_time, true);
            }
            let login = '';
            if (user && user.last_login_time) {
                login = formatDate(user.last_login_time, true);
            }
            let position = '无';
            if (user && user.position) {
                position = this.positionDic[user.position];
            }
            let sex = '男';
            if (user && user.sex == 1) {
                sex = '女';
            }
            let list = [
                {
                    label: '姓名',
                    value: user && user.name || '',
                },
                {
                    label: '性别',
                    value: sex,
                },
                {
                    label: '登录账号',
                    value: user && user.account || '',
                },
                {
                    label: 'UID',
                    value: user && user.uid || '',
                },
                {
                    label: '所属机构',
                    value: user && user.enforce_org_name || '',
                },
                {
                    label: '检查职务',
                    value: position || '',
                },
                {
                    label: '联系方式',
                    value: user && user.phone_num || '',
                },
                {
                    label: '状态',
                    value: user.user_status,
                    type: 1
                },
                {
                    label: '状态变更原因',
                    value: user && user.status_reason || '',
                },
                {
                    label: '状态描述',
                    value: user && user.status_desc || '',
                },
            ];
            if (user && user.user_status == 1) {
                let start = '';
                if (user && user.status_start_time) {
                    start = formatDate(user.status_start_time, true);
                }
                let end = '';
                if (user && user.status_end_time) {
                    end = formatDate(user.status_end_time, true);
                }
                list.push({
                    label: '状态开始时间',
                    value: start,
                }, {
                    label: '状态结束时间',
                    value: end,
                });
            }
            const dates = [
                {
                    label: '创建人',
                    value: user && user.create_user_name || '',
                },
                {
                    label: '创建时间',
                    value: create,
                },
                {
                    label: '更新人',
                    value: user && user.update_user_name || '',
                },
                {
                    label: '更新时间',
                    value: update,
                },
            ];
            list = list.concat(dates);
            let role = [];
            if (user.roles && user.roles.length > 0) {
                user.roles.map(obj => {
                    if (notEmpty(obj.name)) {
                        role.push(obj.name);
                    }
                });
            }
            list.push({
                label: '角色',
                value: role.join('/'),
                row: true,
            });
            this.list = list;
        }
    },
}
</script>