<template>
    <div class="app-container" v-loading="state.loading">
        <el-card class="search-container">
            <el-form :inline="true">
                <el-form-item label="职位名称">
                    <el-input v-model="state.queryParams.userName" placeholder="职位名称" clearable />
                </el-form-item>
                <el-form-item>
                    <el-date-picker v-model="state.timeSpan" type="datetimerange" start-placeholder="开始"
                        end-placeholder="结束" range-separator="至" clearable />
                </el-form-item>
                <el-form-item>
                    <el-button @click="handleQuery" icon="Search" v-auth="'logLoginIndex:page'">查询</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="paged-table-container">
            <el-table ref="tableRef" row-key="id" :data="state.tableData" border>
                <el-table-column type="index" width="50">
                    <template #default="scope">
                        {{ scope.$index + 1 + (state.pageParams.Page - 1) *
                            state.pageParams.PageSize }}
                    </template>
                </el-table-column>
                <el-table-column prop="id" label="Id"></el-table-column>
                <el-table-column prop="account" label="账号"></el-table-column>
                <el-table-column prop="ipAddress" label="IP"></el-table-column>
                <el-table-column prop="browser" label="Browser"></el-table-column>
                <el-table-column prop="os" label="Os"></el-table-column>
                <el-table-column prop="message" label="消息" width="210" show-overflow-tooltip></el-table-column>
                <el-table-column prop="logStatusText" label="状态" width="120"/>
                <el-table-column prop="createTime" label="记录时间" />
            </el-table>
            <el-pagination style="margin-top: 15px;float:right" v-model:currentPage="state.pageParams.Page"
                v-model:page-size="state.pageParams.PageSize" :total="state.pageParams.Total" background
                layout="total, sizes,prev, pager, next" @size-change="handleQuery" @current-change="handleQuery"
                :page-sizes="[10, 20, 50, 100]" />
        </el-card>
    </div>
</template>
<script setup>
defineOptions({ name: 'logLoginIndex' })
import { reactive, onMounted } from 'vue'
import {getLogLogin} from '@/api/system/logApi'
import {formatDate} from '@/utils/formatTime'

const state = reactive({
    loading: false,
    queryParams: {
        userName: '',
        startTime: null,
        endTime: null,
    },
    pageParams: {
        Page: 1,
        PageSize: 10,
        Total: 0
    },
    tableData: [],
    timeSpan: []
})

onMounted(() => {
    let nowDay=formatDate(new Date(),"YYYY-mm-dd")
    state.timeSpan.push(nowDay+" 00:00:00")
    state.timeSpan.push(nowDay+" 23:59:59")
    handleQuery()
})

function handleQuery() {
    state.loading = true
    if(state.timeSpan&&state.timeSpan.length>1)
    {
        state.queryParams.startTime=state.timeSpan[0]
        state.queryParams.endTime=state.timeSpan[1]
    }else{
        state.queryParams.startTime=null
        state.queryParams.endTime=null
    }

    getLogLogin(Object.assign({}, state.queryParams, state.pageParams))
        .then((resp) => {
            state.tableData = resp.data.data
            state.pageParams.Total = resp.data.total
        })
        .finally(() => (state.loading = false))
}

</script>