<script setup>
import {onMounted, ref} from 'vue';
import {ElButton, ElForm, ElFormItem, ElInput, ElPagination, ElTable, ElTableColumn} from 'element-plus';
import axios from "axios";
import {useStore} from "vuex";

const store = useStore();
const token = store.getters['authentication/token']

const searchTitle = ref('');
const searchPath = ref('');
const searchAuthor = ref('');
const searchSummary = ref('');
const sortOrder = ref('');

const allNewsData = ref([]);
const newsData = ref([]);

const currentPage = ref(1);
const pageSize = ref(10);
const newsCount = ref(0);
const firstTimeLoad = ref(true);
const loadNews = async () => {
  if (firstTimeLoad.value || allNewsData.value.length < (currentPage.value * pageSize) && (currentPage.value * pageSize) <= newsCount) {
    console.error(allNewsData.value.length, currentPage.value, pageSize.value, newsCount.value)
    let params = {
      token: token,
      start: allNewsData.value.length,
      end: allNewsData.value.length + pageSize.value * 2
    }
    try {
      const response = await axios.get('/api/news', {params})
      const data = response.data
      newsCount.value = data.newsCount;
      allNewsData.value.push(...data.newsList);
    } catch (e) {
      console.log(e)
    }
    firstTimeLoad.value = false;
  }
  newsData.value = allNewsData.value.slice((currentPage.value - 1) * pageSize.value, currentPage.value * pageSize.value)
  console.error((currentPage.value - 1) * pageSize.value, currentPage.value * pageSize.value, newsData.value);
};

onMounted(() => {
  loadNews();

})


const handleSearch = () => {
  // 搜索逻辑
};

const handleReset = () => {
  searchTitle.value = '';
  searchPath.value = '';
  searchAuthor.value = '';
  searchSummary.value = '';
  sortOrder.value = '';
};

const handleEdit = (index) => {
  // 编辑逻辑
};

const handleDelete = (index) => {
  // 删除逻辑
};
</script>

<template>
  <div class="container">
    <div class="search-container">
      <el-form inline>
        <el-form-item label="新闻标题">
          <el-input v-model="searchTitle" placeholder="请输入标题"/>
        </el-form-item>
        <el-form-item label="新闻图片路径">
          <el-input v-model="searchPath" placeholder="请输入路径"/>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="sortOrder" placeholder="请输入排序"/>
        </el-form-item>
        <el-form-item label="作者">
          <el-input v-model="searchAuthor" placeholder="请输入作者"/>
        </el-form-item>
        <el-form-item label="新闻简介">
          <el-input v-model="searchSummary" placeholder="请输入简介"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="button-container">
      <el-button type="success">新增</el-button>
      <el-button type="warning">修改</el-button>
      <el-button type="danger">删除</el-button>
      <el-button type="info">导出</el-button>
    </div>

    <el-table :data="newsData" style="width: 100%;">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="title" label="新闻标题" align="center"></el-table-column>
      <el-table-column prop="author" label="作者" align="center"></el-table-column>
      <el-table-column prop="summary" label="新闻简介" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button @click="handleEdit(scope.$index)" type="text">修改</el-button>
          <el-button @click="handleDelete(scope.$index)" type="text">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
          @size-change="pageSize = $event;loadNews()"
          @current-change="currentPage = $event;loadNews()"
          :current-page="currentPage"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="newsCount"
          :page-sizes="[10, 20, 50, 100]"
      >
      </el-pagination>
    </div>
  </div>
</template>

<style scoped>
.container {
  padding: 20px;
}

.search-container {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-bottom: 20px;
}

.button-container {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.el-table {
  margin-bottom: 20px;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style>