<template>
  <div class="study-records">
    <div class="filter-bar">
      <el-input 
        v-model="searchKeyword" 
        placeholder="搜索用户..."
        style="width: 200px"
        clearable
      />
      <el-select v-model="selectedTask" placeholder="选择任务" style="width: 200px" clearable>
        <el-option 
          v-for="task in tasks" 
          :key="task.id" 
          :label="task.name" 
          :value="task.id" 
        />
      </el-select>
      <el-date-picker
        v-model="dateRange"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      />
      <el-button type="primary" @click="handleSearch">查询</el-button>
      <el-button @click="handleExport">导出</el-button>
    </div>

    <el-table :data="records" style="width: 100%">
      <el-table-column prop="userName" label="用户" width="120" />
      <el-table-column prop="taskName" label="任务名称" min-width="180" />
      <el-table-column prop="progress" label="完成进度" width="120">
        <template #default="{ row }">
          <el-progress :percentage="row.progress" />
        </template>
      </el-table-column>
      <el-table-column prop="duration" label="学习时长" width="120" />
      <el-table-column prop="startDate" label="开始时间" width="150" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="{ row }">
          <el-tag :type="getStatusType(row.status)">
            {{ getStatusText(row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" fixed="right">
        <template #default="{ row }">
          <el-button size="small" @click="viewDetail(row)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { ElInput, ElSelect, ElOption, ElDatePicker, ElButton, ElTable, ElTableColumn, ElTag, ElProgress, ElPagination, ElMessage } from 'element-plus';

const searchKeyword = ref('');
const selectedTask = ref('');
const dateRange = ref<[Date, Date] | null>(null);
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const records = ref<any[]>([]);
const tasks = ref<any[]>([]);

onMounted(() => {
  loadTasks();
  loadRecords();
});

function loadTasks() {
  // TODO: 加载任务列表
}

function loadRecords() {
  // TODO: 加载学习记录
}

function handleSearch() {
  currentPage.value = 1;
  loadRecords();
}

function handleExport() {
  // TODO: 导出学习记录
  ElMessage.info('导出功能开发中');
}

function getStatusType(status: string) {
  const typeMap: Record<string, any> = {
    'completed': 'success',
    'in-progress': 'warning',
    'not-started': 'info'
  };
  return typeMap[status] || 'info';
}

function getStatusText(status: string) {
  const textMap: Record<string, string> = {
    'completed': '已完成',
    'in-progress': '进行中',
    'not-started': '未开始'
  };
  return textMap[status] || status;
}

function viewDetail(row: any) {
  // TODO: 查看学习记录详情
}

function handleSizeChange(val: number) {
  pageSize.value = val;
  loadRecords();
}

function handleCurrentChange(val: number) {
  currentPage.value = val;
  loadRecords();
}
</script>

<style lang="scss" scoped>
.study-records {
  padding: 20px;
}

.filter-bar {
  display: flex;
  gap: 16px;
  margin-bottom: 20px;
  align-items: center;
}

.el-table {
  margin-bottom: 20px;
}
</style>

