<template>
  <div class="container">
    <div class="bread-container">
      <el-breadcrumb separator=">">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>校园名师</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-card class="teacher-card">
      <template #header>
        <div class="card-header">
          <div>
            <div class="title">校园名师</div>
            <div>教育大计老师为本</div>
          </div>

          <div class="header-search">
            <el-input v-model="queryParams.teacherName" style="width: 300px" placeholder="请输入关键词" clearable
              @clear="handleClear" />
            <el-button type="primary" :icon="Search" @click="handleQuery" />
          </div>
        </div>
      </template>

      <ul class="card-teacher-list">
        <li class="item" v-for="item in tableData">
          <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>

      <template #footer>
        <el-pagination class="footer" background layout="prev, pager, next, sizes,jumper" :total="total"
          @current-change="handleCurrentChange" @size-change="handleSizeChange" />
      </template>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Search } from '@element-plus/icons-vue'
import { getShowAPI } from '@/apis/home'

const router = useRouter()

const queryParams = ref({
  teacherName: '',
  pageNum: 1,
  pageSize: 10
})
const tableData = ref([])
const total = ref(0)
const getShowData = async () => {
  const res = await getShowAPI(queryParams.value)
  tableData.value = res.rows
  total.value = res.total
}

onMounted(() => {
  getShowData()
})

const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  getShowData()
}

const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  getShowData()
}

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

const handleQuery = () => {
  if (!queryParams.value.teacherName) {
    ElMessage.error('请输入查询关键字！')
    return
  }
  getShowData()
}

const handleClear = () => {
  queryParams.value.teacherName = ''
  getShowData()
}
</script>

<style lang="scss" scoped>
.container {
  .bread-container {
    padding: 25px 0;

    :deep(.el-breadcrumb__inner) {
      color: #fff;
      font-size: 16px;
    }

    :deep(.el-breadcrumb__separator) {
      color: #fff;
    }
  }

  .teacher-card {
    margin-top: 20px;

    .card-header {
      display: flex;
      justify-content: space-between;

      .title {
        font-size: 20px;
        font-weight: bold;
        margin-bottom: 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;
        }
      }
    }

    :deep(.el-table__row .cell) {
      font-size: 17px;
      padding: 7px;
    }

    .footer {
      display: flex;
      justify-content: end;
    }
  }

}
</style>