<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="姓名" min-width="120">
                                <template #default="scope">
                                    {{ scope.row.admin?.nickname || scope.row.admin?.name || scope.row.admin?.username || '-' }}
                                </template>
                            </el-table-column>
                            <el-table-column prop="user_browser" label="设备/浏览器" min-width="150">
                                <template #default="scope">
                                    {{ scope.row.user_browser || scope.row.user_agent || '未知' }}
                                </template>
                            </el-table-column>
                            <el-table-column prop="client_ip" label="IP地址" min-width="160"></el-table-column>
                            <el-table-column prop="create_time" label="访问时间" min-width="200"></el-table-column>
                            <el-table-column label="行为描述" min-width="260">
                                <template #default="scope">
                                    <div class="desc-main">{{ scope.row.request_path || '-' }}</div>
                                    <div class="desc-sub">{{ scope.row.request_method || '' }}</div>
                                </template>
                            </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 { operation_logmin } 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.getoperation_logmin()
    },
    methods: {
        //列表
        getoperation_logmin(){
            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
            operation_logmin(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
                }
            }).catch(()=>{
                this.tableData = []
                this.total = 0
            }).finally(()=>{
                this.loading = false
            })
        },
        handleSearch() {
            this.currentPage = 1
            this.getoperation_logmin()
        },
        handleSizeChange(val) {
            this.pageSize = val
            this.currentPage = 1
            this.getoperation_logmin()
        },
        handleCurrentChange(val) {
            this.currentPage = val
            this.getoperation_logmin()
        },
        formatIndex(index) {
            return (this.currentPage - 1) * this.pageSize + index + 1
        },
        rowClassName() {
            return 'log-row'
        },
        cellStyle({ column }) {
            const leftAlignedProps = ['user_browser', 'client_ip', 'create_time', 'request_path']
            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>