<template>
  <div class="container">
    <div class="home-banner">
      <el-carousel height="500px" arrow="always">
        <el-carousel-item v-for="item in bannerList" :key="item.bannerId">
          <img :src="item.coverUrl" :alt="item.title" @click="handleChange(item)">
        </el-carousel-item>
      </el-carousel>
    </div>

    <div class="column">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-card style="height: 290px;">
            <template #header>
              <div class="title">学校动态</div>

              <div class="card-left-header">
                <span>校园资讯常看常新</span>
                <span class="more">
                  <RouterLink to="news">查看更多></RouterLink>
                </span>
              </div>
            </template>

            <ul class="card-left-list">
              <li v-for="item in trendList" @click="handleTrendClick(item)">
                <span>{{ item.title }}</span>
                <span>{{ parseTime(item.createTime, '{y}-{m}-{d}') }}</span>
              </li>
            </ul>
          </el-card>
        </el-col>

        <el-col :span="12">
          <el-card style="height: 290px;">
            <template #header>
              <div class="title">同步教材</div>

              <div class="card-right-header">
                <span>数字化教材云平台</span>
                <span class="more">
                  <RouterLink to="textBook">查看更多></RouterLink>
                </span>
              </div>
            </template>

            <div class="card-right-content">
              <el-radio-group v-model="grade" class="grade-radio" @change="firstChange">
                <el-radio-button v-for="item in firstType" :label="item.label" :value="item.id" :key="item.id" />
              </el-radio-group>
              <el-tabs v-model="activeName" type="border-card" @tab-click="secondChange">
                <el-tab-pane v-for="item in secondType" :label="item.label" :name="item.id" :key="item.id"
                  class="class-book">
                  <el-tag v-for="item in threeType" :key="item.id" type="info" @click="gotoInfo(item.id)">{{ item.label
                    }}</el-tag>
                </el-tab-pane>
              </el-tabs>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <div class="subject">
      <el-card>
        <template #header>
          <div class="title">专题资源</div>
          <div class="card-subject-header">
            <span>助力加强理论学习</span>
            <span class="more">
              <RouterLink to="subject">查看更多></RouterLink>
            </span>
          </div>
        </template>
        <div class="subject-content">
          <el-card v-for="item in resourceList" @click="handleSubjectClick(item)">
            <img :src="item.coverUrl" alt="">
            <template #footer>
              <div class="subject-title">{{ item.catalogName }}</div>
              <div class="subject-date">{{ parseTime(item.createTime, '{y}-{m}-{d}') }}</div>
            </template>
          </el-card>
        </div>
      </el-card>
    </div>

    <div class="teacher">
      <el-card>
        <template #header>
          <div class="title">校园名师</div>
          <div class="card-teacher-header">
            <span>教育大计老师为本</span>
            <RouterLink to="teacher">查看更多></RouterLink>
          </div>
        </template>

        <ul class="card-teacher-list">
          <li class="item" v-for="item in showList">
            <el-card @click="handleTeacherClick(item)">
              <img :src="item.avatarUrl" alt="">
              <p class="teacher-name">{{ item.teacherName }}</p>
              <p class="teacher-class">{{ item.gradeName }}</p>
              <p class="teacher-desc" v-html="item.content"></p>
            </el-card>
          </li>
        </ul>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getBannerAPI, getTrendAPI, getTextbookAPI, getShowAPI, getResourceAPI } from '@/apis/home'
import { parseTime } from '@/utils/utils'

const router = useRouter()

const bannerList = ref([])
const getBanner = async () => {
  const res = await getBannerAPI({
    pageNum: 1,
    pageSize: 10,
  })
  bannerList.value = res.rows
}

const handleChange = (val) => {
  console.log(val.url);
  goTarget(val.url)
}


const trendList = ref([])
const getTrendData = async () => {
  const res = await getTrendAPI({
    pageNum: 1,
    pageSize: 5
  })
  trendList.value = res.rows
}

const grade = ref()
const activeName = ref()
const textbookList = ref([])
const firstType = ref([])
const secondType = ref([])
const threeType = ref([])
const getTextbookData = async () => {
  const res = await getTextbookAPI()
  textbookList.value = res.data

  const firstList = res.data[0].children
  firstType.value = firstList
  grade.value = firstType.value[0].id

  const secondList = firstList[0].children
  secondType.value = secondList
  activeName.value = secondType.value[0].id

  const threeList = secondList[0].children
  threeType.value = threeList
}

const firstChange = (val) => {
  const firstList = textbookList.value[0].children
  secondType.value = firstList.filter(item => item.id == val)[0].children
  activeName.value = secondType.value[0].id
}

const secondChange = (tab) => {
  const id = tab.props.name
  threeType.value = secondType.value.filter(item => item.id == id)[0].children
}

const resourceList = ref([])
const getResourceData = async () => {
  const res = await getResourceAPI({
    type: 2,
    pageNum: 1,
    pageSize: 8
  })
  resourceList.value = res.rows
}

const showList = ref([])
const getShowData = async () => {
  const res = await getShowAPI({
    pageNum: 1,
    pageSize: 4
  })
  showList.value = res.rows
}


onMounted(() => {

  getBanner()
  getTrendData()
  getTextbookData()
  getResourceData()
  getShowData()

})

const gotoInfo = (id) => {
  router.push(`textBook`)
}

const goTarget = (url) => {
  window.open(url, '__blank');
}

const handleTrendClick = (item) => {
  if (item.type == 1) {
    goTarget(item.url)
  } else {
    router.push(`news/${item.trendId}`)
  }
}

const handleSubjectClick = (item) => {
  router.push({ path: `subSubject`, query: { catalogId: item.catalogId, catalogName: item.catalogName, createTime: item.createTime, resourceNum: item.resourceNum } })
}

const handleTeacherClick = (item) => {
  router.push(`teacher/${item.showId}`)
}
</script>

<style lang="scss" scoped>
.container {
  .title {
    font-size: 20px;
    font-weight: bold;
  }

  .more {
    font-size: 15px;
    color: #8A8178;
  }

  .home-banner {
    width: 1240px;
    height: 500px;

    img {
      width: 100%;
      height: 500px;
      border-radius: 10px;
    }
  }

  .column {
    height: 290px;
    margin-top: 20px;

    .card-left-header {
      display: flex;
      justify-content: space-between;
      margin-top: 5px;
      color: #8A8178;
    }

    .card-left-list {
      li {
        display: flex;
        justify-content: space-between;
        padding: 7px;
        font-size: 15px;
      }
    }

    .card-right-header {
      display: flex;
      justify-content: space-between;
      margin-top: 5px;
      color: #8A8178;
    }

    .grade-radio {
      margin-top: -25px;
      vertical-align: middle;
    }

    .card-right-content {
      padding: 0;

      .class-book {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 8px;
      }
    }
  }

  .subject {
    margin-top: 20px;

    .card-subject-header {
      display: flex;
      justify-content: space-between;
      margin-top: 5px;
    }

    .subject-content {
      padding: 0;
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      grid-template-rows: repeat(2, 1fr);
      gap: 10px;

      img {
        border-radius: 5px;
        height: 240px;
        width: 100%;
      }

      .subject-title {
        font-size: 15px;
      }

      .subject-date {
        margin-top: 5px;
        color: #DADADA;
      }
    }
  }

  .teacher {
    margin-top: 20px;

    .card-teacher-header {
      display: flex;
      justify-content: space-between;
      margin-top: 5px;
    }

    .card-teacher-list {
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      grid-template-rows: repeat(1, 1fr);
      gap: 10px;

      .item {
        text-align: center;

        img {
          border-radius: 10px;
          width: 100%;
          height: 300px;
        }

        .teacher-name {
          line-height: 45px;
          font-size: 18px;
          font-weight: bold;
        }

        .teacher-class {
          line-height: 40px;
          font-size: 17px;
        }

        .teacher-desc {
          font-size: 16px;
          color: #7F7F7F;
          text-align: left;
        }
      }
    }
  }
}
</style>