<script setup>
import { onMounted, ref } from 'vue';
import { ElButton, ElForm, ElFormItem, ElInput, ElMessage, ElPagination, ElTable, ElTableColumn } from 'element-plus';
import axios from "axios";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import Course from "@views/course-management/Course.vue";
import BackgroundWrapper from './BackgroundWrapper.vue';

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

const searchTitle = ref('');
const searchAuthor = ref('');
const searchDescription = ref('');
const sortOrder = ref('');

const allCoursesData = ref([]);
const coursesData = ref([]);

const currentPage = ref(1);
const pageSize = ref(10);
const coursesCount = ref(0);
const firstTimeLoad = ref(true);

const selections = ref([]);

const loadCourses = async (forceReload = false) => {
  if (forceReload) {
    firstTimeLoad.value = true;
    allCoursesData.value = [];
  }

  if (firstTimeLoad.value || allCoursesData.value.length < (currentPage.value * pageSize.value) && (currentPage.value * pageSize.value) <= coursesCount.value) {
    let params = {
      token: token,
      start: allCoursesData.value.length,
      end: allCoursesData.value.length + pageSize.value * 2
    };
    try {
      const response = await axios.get('/api/courses', { params });
      const data = response.data;
      coursesCount.value = data.courseCount;
      allCoursesData.value.push(...data.courseList);
    } catch (e) {
      console.log(e);
    }
    firstTimeLoad.value = false;
  }
  coursesData.value = allCoursesData.value.slice((currentPage.value - 1) * pageSize.value, currentPage.value * pageSize.value);
};

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

const handleSearch = async () => {
  try {
    const response = await axios.get('/api/courses/search', {
      params: {
        token: token,
        title: searchTitle.value || '',
        author: searchAuthor.value || '',
        description: searchDescription.value || '',
        sortOrder: sortOrder.value || '',
        start: 0,
        end: pageSize.value
      }
    });
    const data = response.data;
    coursesCount.value = data.courseCount;
    allCoursesData.value = data.courseList;
    coursesData.value = allCoursesData.value.slice(0, pageSize.value);
    currentPage.value = 1;
    firstTimeLoad.value = false;
  } catch (e) {
    console.error(e);
  }
};

const handleReset = async () => {
  searchTitle.value = '';
  searchAuthor.value = '';
  searchDescription.value = '';
  sortOrder.value = '';
  await loadCourses(true);  // 强制重新加载课程数据
};

const handleSort = async () => {
  try {
    const response = await axios.get('/api/courses/sort', {
      params: {
        token: token,
        sortField: 'orderNo',
        sortDirection: 'asc',
        start: 0,
        end: pageSize.value
      }
    });
    const data = response.data;

    // 检查返回的数据是否按预期排序
    console.log(data.courseList);

    coursesCount.value = data.courseCount;
    allCoursesData.value = data.courseList;
    coursesData.value = allCoursesData.value.slice(0, pageSize.value);
    currentPage.value = 1;
    firstTimeLoad.value = false;
  } catch (e) {
    console.error(e);
  }
};

const handleEditButton = () => {
  if (selections.value.length === 1) {
    // 只有一个课程被选中，导航到编辑页面
    const selectedCourse = selections.value[0];
    router.push({ name: 'Course', query: { mode: 'edit', id: selectedCourse.id } });
  } else if (selections.value.length > 1) {
    // 选中了多个课程，弹出提示
    ElMessage.warning('无法同时修改多个目标');
  } else {
    // 没有选中任何课程，弹出提示
    ElMessage.warning('请先选择要修改的课程');
  }
};

const handleDeleteButton = async () => {
  if (selections.value.length === 0) {
    ElMessage.warning('请先选择要删除的课程');
    return;
  }
  try {
    const deletePromises = selections.value.map(selection =>
        axios.delete(`/api/courses/${selection.id}`, { params: { token: token } })
    );
    await Promise.all(deletePromises);
    ElMessage.success('删除成功');
    selections.value = []; // 清空选中项
    await loadCourses(true); // 强制重新加载课程数据
  } catch (e) {
    ElMessage.error('删除失败');
    console.error(e);
  }
};

const handleSelectionChange = (newSelections) => {
  selections.value = newSelections;
};

const handleEditInTable = (index) => {
  router.push({ name: 'Course', query: { mode: 'edit', id: coursesData.value[index].id } });
};

const handleDeleteInTable = async (index) => {
  try {
    await axios.delete(`/api/courses/${coursesData.value[index].id}`, { params: { token: token } });
    coursesData.value.splice(index, 1);
    allCoursesData.value.splice(index, 1);
    coursesCount.value--;
    await loadCourses(true); // 强制重新加载课程数据
  } catch (e) {
    ElMessage.error('删除失败');
    console.error(e);
  }
};
</script>

<template>
  <BackgroundWrapper>
    <div class="center-wrapper">
      <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="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="searchDescription" 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" @click="router.push('/course?mode=create')">新增</el-button>
          <el-button type="warning" @click="handleEditButton">修改</el-button>
          <el-button type="danger" @click="handleDeleteButton">删除</el-button>
          <el-button type="info" @click="handleSort">排序</el-button>
        </div>

        <el-table :data="coursesData" style="width: 100%;" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="orderNo" label="排序" align="center"></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="description" label="课程简介" align="center"></el-table-column>
          <el-table-column label="操作" align="center">
            <template #default="scope">
              <el-button @click="handleEditInTable(scope.$index)" type="text">修改</el-button>
              <el-button @click="handleDeleteInTable(scope.$index)" type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

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

<style scoped>
html, body {
  height: 100%;
  margin: 0;
}

.center-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
}

.container {
  padding: 20px;
  background: rgba(255, 255, 255, 0.8); /* 为了让内容清晰，可加上一层半透明白色背景 */
  border-radius: 15px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  max-width: 800px; /* 控制页面内容宽度 */
  width: 100%;
}

.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>