<script setup>
import { ref, reactive, onMounted, toRefs } from "vue";
import { Search, Plus } from "@element-plus/icons-vue";

import { getAdminLog } from "@/api/adminuser";

const state = reactive({
  search: {
    keyword: "",
  },
  page: 1,
  pageSize: 10,
});


const { page, pageSize, search} = toRefs(state);

const total = ref(0);
const loading = ref(false);

const tableData = ref([]);

onMounted(() => {
  getTableData();
});

const handleSearch = () => {
  getTableData();
};

const getTableData = async () => {
  loading.value = true;
  let res = await getAdminLog(state);
  if (res.code === 200) {
    tableData.value = res.data.list;
    total.value = res.data.total;
  }
  loading.value = false;
};

// Pagination
const handleSizeChange = (val) => {
  state.pageSize = val;
  getTableData();
};

const handleCurrentChange = (val) => {
  state.page = val;
  getTableData();
};

const handlePrevClick = (val) => {
  state.page = val;
  getTableData();
};

const handleNextClick = (val) => {
  state.page = val;
  getTableData();
};

</script>

<template>
  <div class="main">
    <!-- Breadcrumb: Start -->
    <div class="breadcrumb-section">
      <div>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item> 權限管理 </el-breadcrumb-item>
          <el-breadcrumb-item>管理員紀錄</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div></div>
    </div>
    <!-- Breadcrumb: End -->

    <el-card style="margin-bottom: 5px;">
      <div class="search-section">
        <el-row>
          <el-col :xs="12" :sm="12" :md="4" :lg="4" :xl="4">
            <div>
              <el-input v-model="search.keyword" clearable/>
            </div>
          </el-col>
          <el-col :xs="12" :sm="12" :md="4" :lg="4" :xl="4">
            <div style="padding: 0 5px;">
              <el-button type="primary" @click="handleSearch">搜尋</el-button>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-card>

    <el-card :body-style="{ padding: '0px' }">
      <div v-loading="loading">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="id" label="#" width="50" />
          <el-table-column prop="admin_name" label="操作者" width="180" />
          <el-table-column prop="time" label="操作日期" width="180" />
          <el-table-column prop="ip" label="IP地址" width="180" />
          <el-table-column prop="content" label="操作紀錄" />
        </el-table>
        <div class="pagination-block">
          <el-pagination v-model:current-page="page" v-model:page-size="pageSize" :page-sizes="[10, 20, 50, 100]"
            layout="->,total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" @prev-click="handlePrevClick" @next-click="handleNextClick" />
        </div>
      </div>
    </el-card>
  </div>
</template>
