<template>
  <div class="wrong-questions">
    <!-- 顶部导航栏 -->
    <SubPageHeader>
      <template #right>
        <button class="retrain-btn" @click="handleRetrain">
          <img class="retrain-icon" :src="cuotichonglianIcon" alt="错题重练" />
          <span>错题重练</span>
        </button>
      </template>
    </SubPageHeader>

    <!-- 主体 -->
    <div class="body-wrap">
      <div class="content-card">
        <!-- 标题 + 筛选栏 -->
        <div class="card-header">
          <h2 class="page-title">错题记录</h2>
          <div class="filter-groups">
            <!-- 学科筛选 -->
            <LmFilterTabs v-model="activeSubject" :options="subjectFilters" />

            <!-- 重练状态筛选 -->
            <LmFilterTabs v-model="activePractice" :options="practiceFilters" />

            <!-- 时间筛选 -->
            <LmFilterTabs v-model="activeTime" :options="timeFilters" />
          </div>
        </div>

        <!-- 题目列表网格 -->
        <div class="card-body">
          <div class="question-grid">
            <div v-for="q in filteredQuestions" :key="q.id" class="question-card"
              :class="{ 'card-poor': q.accuracy === 0 }">
              <!-- 题目文本 -->
              <p class="question-text">{{ q.content }}</p>

              <div class="question-actions">
                <!-- 统计行 -->
                <div class="stat-row">
                  <span class="stat-item">
                    <span class="stat-label">做题数：</span>
                    <span class="stat-count">{{ q.count }}</span>
                    <span class="stat-unit">次</span>
                  </span>
                  <span class="stat-item">
                    <span class="stat-label">正确率：</span>
                    <span class="stat-accuracy" :class="q.accuracy > 0 ? 'high' : 'zero'">
                      {{ q.accuracy }}%
                    </span>
                  </span>
                </div>

                <!-- 标签行 -->
                <div class="tag-row">
                  <span class="tag subject-tag" :class="'subject-' + q.subject">{{ q.subject }}</span>
                  <span class="tag type-tag">{{ q.type }}</span>
                  <span class="tag level-tag">{{ q.level }}</span>
                  <span class="tag-time">{{ q.time }}</span>
                </div>

                <!-- 操作按钮 -->
                <div class="action-row">
                  <button
                    class="action-btn lx-btn lx-btn--sm lx-btn--outline-primary"
                    type="button"
                    @click="viewDetail(q)"
                  >
                    查看详情
                  </button>
                  <button
                    class="action-btn lx-btn lx-btn--sm lx-btn--outline-primary"
                    type="button"
                    @click="extendPractice(q)"
                  >
                    举一反三
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 错题重练 -->
    <el-dialog v-model="showRetrainDialog" class="retrain-dialog" width="880px" :show-close="false"
      :close-on-click-modal="true" modal-class="retrain-dialog-overlay" align-center>
      <div class="retrain-panel">
        <h3 class="dialog-title">错题重练</h3>

        <div class="dialog-section">
          <div class="section-title">选择条件</div>
          <div class="condition-row">
            <LmSelect
              v-for="item in retrainConditions"
              :key="item.key"
              v-model="retrainForm[item.key]"
              :options="item.options"
              width="180px"
            />
          </div>
        </div>

        <div class="dialog-section question-count-section">
          <div class="section-title">
            题目抽取数量 <span class="section-tip">(1-50题)</span>
          </div>
          <div class="counter-row">
            <button class="counter-btn" type="button" :disabled="retrainCount <= minRetrainCount"
              @click="decreaseRetrainCount">
              -
            </button>
            <div class="counter-value">{{ retrainCount }}</div>
            <button class="counter-btn" type="button" :disabled="retrainCount >= maxRetrainCount"
              @click="increaseRetrainCount">
              +
            </button>
          </div> 
        </div>

        <div class="dialog-actions">
          <button
            class="dialog-btn lx-btn lx-btn--xl lx-btn--short lx-btn--default"
            type="button"
            @click="showRetrainDialog = false"
          >
            取消
          </button>
          <button
            class="dialog-btn lx-btn lx-btn--xl lx-btn--short lx-btn--primary"
            type="button"
            @click="confirmRetrain"
          >
            确认
          </button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from "vue";
import { useRouter } from "vue-router";
import SubPageHeader from "@/components/layout/SubPageHeader.vue";
import LmFilterTabs from "@/components/lm/LmFilterTabs.vue";
import LmSelect from "@/components/lm/LmSelect.vue";
import cuotichonglianIcon from "@/assets/subjects/cuotichonglian.svg";

/* ---- 筛选选项 ---- */
const subjectFilters = [
  { key: "all", label: "全部" },
  { key: "英语", label: "英语" },
  { key: "数学", label: "数学" },
  { key: "语文", label: "语文" },
  { key: "科学", label: "科学" },
];
const practiceFilters = [
  { key: "all", label: "全部" },
  { key: "done", label: "已重练" },
  { key: "todo", label: "未重练" },
];
const timeFilters = [
  { key: "all", label: "全部" },
  { key: "today", label: "今天" },
  { key: "week", label: "近一周" },
  { key: "month", label: "近一月" },
];

const activeSubject = ref("all");
const activePractice = ref("all");
const activeTime = ref("all");
const showRetrainDialog = ref(false);
const minRetrainCount = 1;
const maxRetrainCount = 50;
const retrainCount = ref(10);
const router = useRouter();

type RetrainConditionKey = "subject" | "book" | "chapter" | "time";

const retrainForm = ref<Record<RetrainConditionKey, string>>({
  subject: "all-subject",
  book: "all-book",
  chapter: "all-chapter",
  time: "all-time",
});

const retrainConditions: Array<{
  key: RetrainConditionKey;
  options: Array<{ label: string; value: string }>;
}> = [
  {
    key: "subject",
    options: [
      { label: "全部学科", value: "all-subject" },
      { label: "英语", value: "english" },
      { label: "数学", value: "math" },
      { label: "语文", value: "chinese" },
      { label: "科学", value: "science" },
    ],
  },
  {
    key: "book",
    options: [
      { label: "全部教材", value: "all-book" },
      { label: "当前教材", value: "current-book" },
    ],
  },
  {
    key: "chapter",
    options: [
      { label: "全部章节", value: "all-chapter" },
      { label: "当前章节", value: "current-chapter" },
    ],
  },
  {
    key: "time",
    options: [
      { label: "全部时间", value: "all-time" },
      { label: "今天", value: "today" },
      { label: "近一周", value: "week" },
      { label: "近一月", value: "month" },
    ],
  },
];

/* ---- 模拟数据 ---- */
interface Question {
  id: number;
  content: string;
  count: number;
  accuracy: number;
  subject: string;
  type: string;
  level: string;
  time: string;
  practiced: boolean;
}

const questions: Question[] = [
  {
    id: 1,
    content: "There is no ______ trying to persuad...",
    count: 3,
    accuracy: 60,
    subject: "英语",
    type: "单选题",
    level: "中等",
    time: "今天 10:30",
    practiced: true,
  },
  {
    id: 2,
    content: "2024年国庆，全国从1日到7日放假七...",
    count: 3,
    accuracy: 60,
    subject: "语文",
    type: "单选题",
    level: "中等",
    time: "今天 10:30",
    practiced: true,
  },
  {
    id: 3,
    content: "已知A= 2y − xy，B = −xy + 3y。求 2...",
    count: 3,
    accuracy: 60,
    subject: "数学",
    type: "单选题",
    level: "中等",
    time: "今天 10:30",
    practiced: true,
  },
  {
    id: 4,
    content: "There is no ______ trying to persuad...",
    count: 1,
    accuracy: 0,
    subject: "英语",
    type: "单选题",
    level: "中等",
    time: "04/24 10:30",
    practiced: false,
  },
  {
    id: 5,
    content: "2024年国庆，全国从1日到7日放假七...",
    count: 1,
    accuracy: 0,
    subject: "语文",
    type: "单选题",
    level: "中等",
    time: "04/24 10:30",
    practiced: false,
  },
  {
    id: 6,
    content: "已知A= 2y − xy，B = −xy + 3y。求 2...",
    count: 1,
    accuracy: 0,
    subject: "数学",
    type: "单选题",
    level: "中等",
    time: "2025/04/24",
    practiced: false,
  },
  {
    id: 7,
    content: "There is no ______ trying to persuad...",
    count: 1,
    accuracy: 0,
    subject: "英语",
    type: "单选题",
    level: "中等",
    time: "04/24 10:30",
    practiced: false,
  },
  {
    id: 8,
    content: "There is no ______ trying to persuad...",
    count: 1,
    accuracy: 0,
    subject: "英语",
    type: "单选题",
    level: "中等",
    time: "04/24 10:30",
    practiced: false,
  },
  {
    id: 9,
    content: "There is no ______ trying to persuad...",
    count: 1,
    accuracy: 0,
    subject: "英语",
    type: "单选题",
    level: "中等",
    time: "2025/04/24",
    practiced: false,
  },
];

const filteredQuestions = computed(() => {
  return questions.filter((q) => {
    if (activeSubject.value !== "all" && q.subject !== activeSubject.value)
      return false;
    if (activePractice.value === "done" && !q.practiced) return false;
    if (activePractice.value === "todo" && q.practiced) return false;
    return true;
  });
});

function handleRetrain() {
  showRetrainDialog.value = true;
}

function decreaseRetrainCount() {
  retrainCount.value = Math.max(minRetrainCount, retrainCount.value - 1);
}

function increaseRetrainCount() {
  retrainCount.value = Math.min(maxRetrainCount, retrainCount.value + 1);
}

function confirmRetrain() {
  showRetrainDialog.value = false;
  // 关闭弹框后进入错题重练答题页，并把当前筛选条件传过去
  router.push({
    name: "wrong-retrain-answer",
    query: {
      mode: "answer",
      count: String(retrainCount.value),
      subject: retrainForm.value.subject,
      book: retrainForm.value.book,
      chapter: retrainForm.value.chapter,
      time: retrainForm.value.time,
    },
  });
}

function viewDetail(q: Question) {
  router.push({
    name: "wrong-retrain-answer",
    query: {
      mode: "detail",
      questionId: String(q.id),
      count: "10",
    },
  });
}

function extendPractice(q: Question) {
  void q;
  // TODO: 举一反三
}
</script>

<style scoped lang="scss">
.wrong-questions {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

/* ---- 顶部右侧按钮 ---- */
.retrain-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  background: transparent;
  border: none;
  cursor: pointer;
  font-size: 28px;
  color: #333;
  padding: 8px 16px;
  border-radius: 8px;
  transition: background 0.2s;

  &:hover {
    background: rgba(0, 0, 0, 0.05);
  }
}

.retrain-icon {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
}

/* ---- 主体 ---- */
.body-wrap {
  flex: 1;
  min-height: 0;
  padding: 0 64px 32px;
  display: flex;
  flex-direction: column;
}

.content-card {
  flex: 1;
  min-height: 0;
  width: 100%;
  max-width: 1792px;
  margin: 0 auto;
  background: rgba(255, 255, 255, 0.3);
  border: 2px solid rgba(255, 255, 255, 0.7);
  border-radius: 24px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ---- 卡片头部（标题 + 筛选） ---- */
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 28px 40px 24px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}

.page-title {
  font-size: 36px;
  font-weight: 600;
  color: #121212;
  flex-shrink: 0;
}



.filter-groups {
  display: flex;
  align-items: center;
  gap: 32px;
}

/* ---- 卡片内容 ---- */
.card-body {
  flex: 1;
  min-height: 0;
  padding: 32px;
  background: #fff;
  overflow-y: auto;
  scrollbar-width: none;

  &::-webkit-scrollbar {
    display: none;
  }
}

/* ---- 题目网格 ---- */
.question-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 28px;
}

/* ---- 题目卡片 ---- */
.question-card {
  background: #f2fbff;
  border-radius: 20px;
  //   padding: 28px 28px 20px;
  display: flex;
  flex-direction: column;
  border: 1.5px solid #e1f2fa;
  //   gap: 16px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.04);

  &.card-poor {
    background: #fff5f5;
  }
}


.question-actions {
  display: flex;
  flex-direction: column;
  background: #FFF;
  border-radius: 20px;
  padding: 22px 24px;
  gap: 16px;
}

.question-text {
  font-size: 28px;
  color: #121212;
  //   line-height: 1.5;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 24px;
}

/* 统计行 */
.stat-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  font-size: 24px;
  color: #333;
}

.stat-item {
  display: flex;
  align-items: baseline;
  gap: 2px;
}

.stat-label {
  font-size: 24px;
  color: #333;
}

.stat-count {
  font-size: 28px;
  font-weight: 600;
  color: #121212;
  line-height: 1;
}

.stat-unit {
  font-size: 24px;
  color: #666;
  margin-left: 2px;
}

.stat-accuracy {
  font-size: 28px;
  font-weight: 600;
  line-height: 1;

  &.high {
    color: var(--color-text-brand);
  }

  &.zero {
    color: #e84040;
  }
}

/* 标签行 */
.tag-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.tag {
  font-size: 20px;
  padding: 8px 12px;
  border-radius: 8px;
}

.subject-tag {
  font-weight: 500;

  &.subject-英语 {
    color: var(--color-text-brand);
    background: rgba(45, 111, 224, 0.1);
  }

  &.subject-语文 {
    color: #27ae60;
    background: rgba(39, 174, 96, 0.1);
  }

  &.subject-数学 {
    color: var(--color-text-brand);
    background: rgba(45, 111, 224, 0.1);
  }

  &.subject-科学 {
    color: #9b59b6;
    background: rgba(155, 89, 182, 0.1);
  }
}

.type-tag {
  color: var(--color-text-brand);
  border: 1.5px solid var(--color-text-brand);
  background: transparent;
}

.level-tag {
  color: #e07d2d;
  background: rgba(224, 125, 45, 0.12);
}

.tag-time {
  margin-left: auto;
  font-size: 24px;
  color: #666;
  white-space: nowrap;
}

/* 操作按钮 */
.action-row {
  display: flex;
  align-items: center;
  gap: 24px;
  margin-top: 11px;
}

.action-btn {
  flex: 1;
}

:global(.retrain-dialog) {
  --el-dialog-padding-primary: 0;
  border-radius: 26px;
  box-shadow: none;
}

:global(.retrain-dialog .el-dialog__header) {
  display: none;
}

:global(.retrain-dialog .el-dialog__body) {
  padding: 0;
}

:global(.retrain-dialog-overlay) {
  background-color: rgba(0, 0, 0, 0.58);
}

.retrain-panel {
  width: 100%;
  // min-height: 640px;
  padding: 48px 56px;
  border-radius: 26px;
  background: #fff;
}

.dialog-title {
  margin-bottom: 52px;
  color: #121212;
  font-size: 32px;
  font-weight: 700;
  line-height: 1;
  text-align: center;
}

.dialog-section {
  margin-bottom: 48px;
}

.section-title {
  margin-bottom: 24px;
  color: #121212;
  font-size: 24px;
  font-weight: 600;
  line-height: 1;
}

.section-tip {
  color: #9ca3af;
  font-weight: 400;
}

.condition-row {
  display: flex;
  align-items: center;
  gap: 18px;
}

.question-count-section {
  margin-bottom: 56px;
}

.counter-row {
  display: flex;
  align-items: center;
  gap: 22px;
}

.counter-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 46px;
  height: 46px;
  border: 1px solid #edf0f5;
  border-radius: 50%;
  background: #fff;
  color: #222;
  cursor: pointer;
  font-size: 26px;
  line-height: 1;

  &:disabled {
    color: #d9dee7;
    cursor: not-allowed;
  }
}

.counter-value {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 168px;
  height: 58px;
  border-radius: 4px;
  background: #f5f6f8;
  color: #333;
  font-size: 26px;
  font-weight: 500;
}

.dialog-actions {
  display: flex;
  justify-content: center;
  gap: 48px;
}

</style>
