<script setup lang="ts">
import { Search, Filter } from '@element-plus/icons-vue'
import type { SemesterType } from '@/types'
import type { CourseListParamsType } from '../types'
import { searchCourseList, getSemesterList } from '../api'
import { useMapStore } from '@/stores/map'

const emit = defineEmits(['change'])

const mapStore = useMapStore()
// 选课类型
const electiveTypes = mapStore.getMapValuesByKey('system_elective_type')

// 列表参数
const params = reactive<CourseListParamsType>({ id: '', semester_ids: [], elective_types: [] })

// 搜索课程
const searchValue = $ref('')
function querySearch(query: string, cb: (arg: any) => void) {
  searchCourseList({ search_name: query }).then(res => {
    let results = res.data.data || []
    results = results.map((item: any) => {
      item.value = item.name
      return item
    })
    cb(results)
  })
}
// 设置关键词高亮
function searchHighlight(name: string) {
  return searchValue ? name.replace(searchValue, `<span class="search-highlight">${searchValue}</span>`) : name
}
// 搜索选择
function handleSelect(data: any) {
  params.id = data.id
  emit('change', params)
}
function handleChange(value: string) {
  if (!value) {
    params.id = ''
    emit('change', params)
  }
}
// 获取学期列表
let semesterList = $ref<SemesterType[]>([])
// 整理后的学期列表
const currentSemesterList = $computed(() => {
  return semesterList.map(item => ({ value: item.id, label: item.name }))
})
function fetchSemesterList() {
  getSemesterList().then(res => {
    semesterList = res.data
  })
}
onMounted(() => {
  fetchSemesterList()
})
// 筛选列表
const filterList = computed(() => {
  return [
    { model: 'semester_ids', label: '学期', options: currentSemesterList },
    { model: 'elective_types', label: '课程类型', options: electiveTypes }
  ]
})
</script>

<template>
  <div class="course-search">
    <el-autocomplete
      clearable
      placeholder="搜索"
      v-model="searchValue"
      :fetch-suggestions="querySearch"
      :prefix-icon="Search"
      :trigger-on-focus="false"
      @select="handleSelect"
      @change="handleChange"
      class="course-search__input"
    >
      <template #default="{ item }">
        <div class="search-course-item">
          <h4 class="search-course-item__name" v-html="searchHighlight(item.name)"></h4>
          <div class="search-course-chapters" v-if="item.chapters?.length">
            <div class="search-course-chapter-item" v-for="chapter in item.chapters" :key="chapter.id">
              <p class="search-course-chapter-item__name" v-html="searchHighlight(chapter.name)"></p>
              <div class="search-course-sections" v-if="chapter.sections?.length">
                <div
                  class="search-course-section-item"
                  v-for="section in chapter.sections"
                  :key="section.id"
                  v-html="searchHighlight(section.name)"
                ></div>
              </div>
            </div>
          </div>
        </div>
      </template>
    </el-autocomplete>
    <el-popover placement="bottom-end" :width="292" trigger="click">
      <template #reference>
        <el-button :icon="Filter" class="course-filter__button"></el-button>
      </template>
      <div class="course-filter">
        <dl class="course-filter-item" v-for="item in filterList" :key="item.model">
          <dt class="course-filter-item__label">{{ item.label }}</dt>
          <dd class="course-filter-item__main">
            <el-checkbox-group v-model="params[item.model]" @change="$emit('change', params)">
              <el-checkbox-button v-for="option in item.options" :label="option.value" :key="option.value">
                {{ option.label }}
              </el-checkbox-button>
            </el-checkbox-group>
          </dd>
        </dl>
      </div>
    </el-popover>
  </div>
</template>

<style lang="scss">
// 搜索
.course-search {
  display: flex;
  align-items: center;
}
.course-search__input {
  flex: 1;
}
.search-highlight {
  color: var(--main-color);
}
.search-course-item {
  padding: 10px 0;
  border-bottom: 1px dashed #d6d6d6;
}
.search-course-item__name {
  font-size: 16px;
  font-weight: 500;
  line-height: 30px;
  color: #333333;
}
.search-course-chapter-item__name {
  position: relative;
  font-size: 16px;
  font-weight: 400;
  line-height: 24px;
  color: #333333;
  padding-left: 15px;
  &::before {
    content: '';
    position: absolute;
    left: 5px;
    top: 50%;
    width: 5px;
    height: 5px;
    background: var(--main-color);
    border-radius: 50%;
    transform: translateY(-50%);
  }
}
.search-course-sections {
  margin-left: 30px;
}
.search-course-section-item {
  font-size: 14px;
  font-weight: 400;
  line-height: 24px;
  color: #666666;
}

// 筛选
.course-filter__button {
  margin-left: 10px;
  padding: 8px;
}
.course-filter {
  padding: 8px;
}
.course-filter-item + .course-filter-item {
  margin-top: 20px;
}
.course-filter-item__label {
  font-size: 16px;
  line-height: 1;
  color: #333;
}
.course-filter-item__main {
  margin-top: 12px;
  .el-checkbox-group {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
  }
  .el-checkbox-button {
    margin-bottom: 10px;
    --el-checkbox-button-checked-text-color: var(--el-color-primary);
    --el-checkbox-button-checked-bg-color: #f7e9ec;
    --el-checkbox-button-checked-border-color: var(--el-color-primary);
    --el-checkbox-button-border-color: #adadad;
  }
  .el-checkbox-button__inner {
    padding: 10px 20px;
    border-left: var(--el-border);
    border-radius: 18px !important;
    &:hover {
      border-color: var(--el-checkbox-button-checked-border-color);
    }
  }
}
</style>
