<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <na-search
                    :controls="[
                        {
                            type: 'select',
                            field: ['dy', 'httpStatusCode'],
                            options: [
                                { label: '成功', value: '200,299' },
                                { label: '失败', value: '300,999' },
                            ],
                            placeholder: '登录结果',
                            style: 'width:15rem',
                        },
                        {
                            type: 'input',
                            field: ['dy', 'extraData'],
                            placeholder: '登录名',
                            style: 'width:15rem',
                        },
                        {
                            type: 'input',
                            field: ['dy', 'createdClientIp'],
                            placeholder: '客户端IP',
                            style: 'width:15rem',
                        },
                    ]"
                    :vue="this"
                    @search="onSearch"
                    ref="search" />
            </div>
            <div class="right-panel"></div>
        </el-header>
        <el-main class="nopadding">
            <sc-table
                :apiObj="$API.sys_log.pagedQuery"
                :context-menus="['id', 'httpStatusCode', 'extraData', 'createdClientIp', 'os', 'createdUserAgent', 'createdTime']"
                :context-opers="['view']"
                :default-sort="{ prop: 'createdTime', order: 'descending' }"
                :params="query"
                :vue="this"
                @row-click="rowClick"
                ref="table"
                remoteFilter
                remoteSort
                row-key="id"
                stripe>
                <el-table-column :label="$t('日志编号')" prop="id" sortable="custom" width="150"></el-table-column>
                <el-table-column :label="$t('结果')" align="center" prop="httpStatusCode" sortable="custom" width="80">
                    <template #default="scope">
                        <sc-status-indicator :type="scope.row.httpStatusCode === 200 ? 'success' : 'danger'" />
                        {{ scope.row.httpStatusCode === 200 ? '成功' : '失败' }}
                    </template>
                </el-table-column>
                <el-table-column :label="$t('登录名')" prop="extraData" sortable="custom" width="200"></el-table-column>
                <el-table-column :label="$t('客户端IP')" prop="createdClientIp" sortable="custom" width="200">
                    <template #default="scope">
                        <na-ip :ip="scope.row.createdClientIp"></na-ip>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('操作系统')" prop="os" width="150"></el-table-column>
                <el-table-column :label="$t('用户代理')" prop="createdUserAgent" show-overflow-tooltip sortable="custom"></el-table-column>
                <el-table-column :label="$t('创建时间')" align="right" prop="createdTime" sortable="custom" width="170"></el-table-column>
            </sc-table>
        </el-main>
    </el-container>

    <na-info v-if="dialog.info" ref="info"></na-info>
</template>

<script>
import naInfo from '@/components/naInfo/index.vue'
import tool from '@/utils/tool'
import ScTable from '@/components/scTable/index.vue'

export default {
    components: {
        ScTable,
        naInfo,
    },
    watch: {},
    inject: ['reload'],
    data() {
        return {
            query: {
                dynamicFilter: {
                    filters: [],
                },
                filter: {},
            },
            dialog: {
                info: false,
            },
        }
    },
    mounted() {
        this.$refs.search.form.dy.apiId = 'api/sys/user/login.by.pwd'
    },
    created() {
        this.query.dynamicFilter.filters.push({
            field: 'apiId',
            operator: 'eq',
            value: 'api/sys/user/login.by.pwd',
        })
    },
    methods: {
        //搜索
        onSearch(form) {
            if (Array.isArray(form.dy.createdTime)) {
                this.query.dynamicFilter.filters.push({
                    field: 'createdTime',
                    operator: 'dateRange',
                    value: form.dy.createdTime,
                })
            }
            if (form.dy.httpStatusCode) {
                this.query.dynamicFilter.filters.push({
                    field: 'httpStatusCode',
                    operator: 'range',
                    value: form.dy.httpStatusCode,
                })
            }
            if (form.dy.apiId) {
                this.query.dynamicFilter.filters.push({
                    field: 'apiId',
                    operator: 'contains',
                    value: form.dy.apiId,
                })
            }
            if (form.dy.extraData) {
                this.query.dynamicFilter.filters.push({
                    field: 'extraData',
                    operator: 'contains',
                    value: form.dy.extraData,
                })
            }
            if (form.dy.createdClientIp) {
                this.query.dynamicFilter.filters.push({
                    field: 'createdClientIp',
                    operator: 'contains',
                    value: form.dy.createdClientIp,
                })
            }
            this.$refs.table.upData()
        },

        async rowClick(row) {
            this.dialog.info = true
            await this.$nextTick()
            const res = await this.$API.sys_log.query.post({
                filter: { id: row.id },
            })
            this.$refs.info.open(tool.sortProperties(res.data[0]), `日志详情：${row.id}`)
        },
    },
}
</script>

<style scoped></style>