<template>
    <div>
        <div class="coentent">
            <div class="contents-area">
                <div class="areas">
                    <div class="headers">
                        <div class="headers-left">
                            <div class="headers-name">用户行为日志</div>
                        </div>
                        <div class="headers-right">
                            <div class="filter-item">
                                <span class="filter-label">时间筛选</span>
                                <el-select v-model="filters.year" style="border:1px solid #ededed" placeholder="年份" class="filter-select" size="small">
                                    <el-option v-for="item in yearOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
                                </el-select>
                                <el-select v-model="filters.quarter" style="border:1px solid #ededed" placeholder="季度" class="filter-select" size="small">
                                    <el-option v-for="item in quarterOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
                                </el-select>
                                <el-select v-model="filters.week" style="border:1px solid #ededed" placeholder="周次" class="filter-select" size="small">
                                    <el-option v-for="item in weekOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
                                </el-select>
                            </div>
                            <el-input placeholder="输入姓名" style="border:1px solid #ededed" v-model="filters.keyword" class="search-input" size="small" clearable>
                                <el-button slot="append" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                            </el-input>
                        </div>
                    </div>

                    <div class="content-table">
                        <el-table
                            :data="tableData"
                            border
                            v-loading="loading"
                            :header-cell-style="{ background: '#EDEFFE', color: '#363636', textAlign: 'center' }"
                            :cell-style="cellStyle"
                            :row-class-name="rowClassName"
                            style="width: 100%">
                            <el-table-column type="index" label="序号" width="70" align="center" :index="formatIndex"></el-table-column>
                            <el-table-column label="IP地址" min-width="120">
                                <template #default="scope">
                                    {{ scope.row.login_ip  }}
                                </template>
                            </el-table-column>
                            <el-table-column label="时间" min-width="150">
                                <template #default="scope">
                                    {{ scope.row.login_time }}
                                </template>
                            </el-table-column>
                            <el-table-column prop="login_os" label="系统" ></el-table-column>
                        
                       
                        </el-table>
                        <div class="footer">
                            <el-pagination
                                @size-change="handleSizeChange"
                                @current-change="handleCurrentChange"
                                :current-page="currentPage"
                                :page-sizes="[10, 20, 30, 50]"
                                :page-size="pageSize"
                                layout="total, sizes, prev, pager, next, jumper"
                                :total="total">
                            </el-pagination>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { login_log } from '@/api/system/logs/userbehavior'
export default {
    data() {
        return {
            loading: false,
            tableData: [],
            total: 0,
            currentPage: 1,
            pageSize: 10,
            filters: {
                year: '2025',
                quarter: '第一季度',
                week: '第一周',
                keyword: ''
            },
            yearOptions: [
                { value: '2024', label: '2024' },
                { value: '2025', label: '2025' }
            ],
            quarterOptions: [
                { value: '第一季度', label: '第一季度' },
                { value: '第二季度', label: '第二季度' },
                { value: '第三季度', label: '第三季度' },
                { value: '第四季度', label: '第四季度' }
            ],
            weekOptions: [
                { value: '第一周', label: '第一周' },
                { value: '第二周', label: '第二周' },
                { value: '第三周', label: '第三周' },
                { value: '第四周', label: '第四周' }
            ]
        }
    },
    mounted() {
        this.getLoginLog()
    },
    methods: {
        getLoginLog() {
            const params = {
                page: this.currentPage,
                limit: this.pageSize
            }
            if (this.filters.keyword) {
                params.keyword = this.filters.keyword.trim()
            }
            if (this.filters.year) {
                params.year = this.filters.year
            }
            if (this.filters.quarter) {
                params.quarter = this.filters.quarter
            }
            if (this.filters.week) {
                params.week = this.filters.week
            }
            this.loading = true
            login_log(params).then(res => {
                if (res && res.code === 1 && res.data) {
                    const list = res.data.data || res.data.list || res.data.records || res.data
                    this.tableData = Array.isArray(list) ? list : []
                    this.total = res.data.total || res.data.count || this.tableData.length
                } else {
                    this.tableData = []
                    this.total = 0
                    if (res && res.msg) {
                        this.$message.warning(res.msg || '获取数据失败')
                    }
                }
            }).catch(error => {
                console.error('获取登录日志失败:', error)
                this.tableData = []
                this.total = 0
                // 错误信息已经在 request.js 的拦截器中处理并显示
                // 这里不再重复显示，避免重复提示
            }).finally(() => {
                this.loading = false
            })
        },
        handleSearch() {
            this.currentPage = 1
            this.getLoginLog()
        },
        handleSizeChange(val) {
            this.pageSize = val
            this.currentPage = 1
            this.getLoginLog()
        },
        handleCurrentChange(val) {
            this.currentPage = val
            this.getLoginLog()
        },
        formatIndex(index) {
            return (this.currentPage - 1) * this.pageSize + index + 1
        },
        rowClassName() {
            return 'log-row'
        },
        cellStyle({ column }) {
            const leftAlignedProps = ['nickname', 'user_browser', 'client_ip', 'create_time', 'description']
            return {
                textAlign: leftAlignedProps.includes(column.property) ? 'left' : 'center',
                color: '#363636'
            }
        }
    }
}
</script>

<style scoped>
.coentent {
    width: 100%;
    height: 100%;
    background: white;
}

.contents-area {
    width: 100%;
    margin-top: 20px;
}

.areas {
    width: 97%;
    margin: 0 auto;
}

.headers {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    flex-wrap: wrap;
    gap: 16px;
    padding-top: 18px;
}

.headers-left {
    display: flex;
    align-items: center;
}

.headers-name {
    font-size: 15px;
    color: #17192C;
}

.headers-right {
    display: flex;
    align-items: center;
    gap: 16px;
    flex-wrap: wrap;
}

.filter-item {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 13px;
    color: #6A6E8C;
}

.filter-label {
    min-width: 72px;
}

.filter-select {
    width: 140px;
}

.search-input {
    width: 220px;
}

.content-table {
    background: #fff;
    border-radius: 8px;
}

.footer {
    display: flex;
    justify-content: flex-end;
    margin-top: 20px;
}

::v-deep .log-row td {
    color: #363636;
}

::v-deep .el-table__body tr:hover > td {
    background-color: #F7F8FF !important;
}

.desc-main {
    font-weight: 500;
    color: #17192C;
}

.desc-sub {
    font-size: 12px;
    color: #6A6E8C;
}
</style>