<template>
  <div class="login-log">
    <el-card>
      <template #header>
        <div class="card-header">
          <div class="search-section">
            <el-input
              v-model="searchKeyword"
              placeholder="搜索用户名或IP地址"
              clearable
              style="width: 300px"
              @clear="handleSearch"
              @keyup.enter="handleSearch"
            >
              <template #prefix>
                <el-icon><Search /></el-icon>
              </template>
            </el-input>
            <el-button
              type="primary"
              @click="handleSearch"
            >
              搜索
            </el-button>
          </div>
        </div>
      </template>
      <el-table
        v-loading="loading"
        :data="dataSource"
        style="width: 100%"
        @sort-change="handleSortChange"
      >
        <el-table-column
          prop="id"
          label="ID"
          sortable="custom"
        />
        <el-table-column
          prop="username"
          label="用户名"
          sortable="custom"
        />
        <el-table-column
          prop="ip"
          label="IP地址"
          sortable="custom"
        />
        <el-table-column
          prop="location"
          label="登录地点"
          sortable="custom"
        />
        <el-table-column
          prop="browser"
          label="浏览器"
          sortable="custom"
        />
        <el-table-column
          prop="os"
          label="操作系统"
          sortable="custom"
        />
        <el-table-column label="状态">
          <template #default="{ row }">
            <el-tag 
              :type="row.status === '0' ? 'success' : 'danger'"
              effect="dark"
              style="font-weight: 500; font-size: 14px;"
            >
              {{ row.status === '0' ? '成功' : '失败' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="loginTime"
          label="登录时间"
          sortable="custom"
        >
          <template #default="{ row }">
            {{ formatDateTime(row.loginTime) }}
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="pagination.current"
        v-model:page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="pagination.total"
        layout="total, sizes, prev, pager, next, jumper"
        style="margin-top: 16px; justify-content: flex-end"
        @current-change="handleTableChange"
        @size-change="handleSizeChange"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { Search } from '@element-plus/icons-vue'
import request from '@/utils/request'
import { formatDateTime } from '@/utils/dateFormat'

const loading = ref(false)
const dataSource = ref([])
const searchKeyword = ref('')
const pagination = reactive({
  current: 1,
  pageSize: 10,
  total: 0
})

const sortInfo = reactive({
  sort: 'id',
  order: 'asc'
})

const fetchData = async () => {
  loading.value = true
  try {
    const res: any = await request.get('/logs/login/page', {
      params: {
        page: pagination.current - 1,
        size: pagination.pageSize,
        sort: sortInfo.sort,
        order: sortInfo.order,
        keyword: searchKeyword.value || undefined
      }
    })
    dataSource.value = res.content
    pagination.total = res.totalElements
  } finally {
    loading.value = false
  }
}

const handleTableChange = () => {
  fetchData()
}

const handleSizeChange = () => {
  pagination.current = 1
  fetchData()
}

const handleSearch = () => {
  pagination.current = 1
  fetchData()
}

const handleSortChange = ({ prop, order }: any) => {
  sortInfo.sort = prop
  sortInfo.order = order === 'ascending' ? 'asc' : 'desc'
  fetchData()
}

onMounted(() => fetchData())
</script>

<style scoped lang="css">
.login-log {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;

    .search-section {
      display: flex;
      gap: 8px;
      align-items: center;
    }
  }
}
</style>
