<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="news-card">
      <template #header>
        <div class="card-header">
          <div>
            <div class="title">学校动态</div>
            <div>校园资讯常看常新</div>
          </div>
          <div class="header-search">
            <el-input v-model="queryParams.title" style="width: 300px" placeholder="请输入关键词" clearable
              @clear="handleClear" />
            <el-button type="primary" :icon="Search" @click="handleQuery" />
          </div>
        </div>
      </template>

      <el-table @row-click="handleRowClick" :data="tableData" class="table" stripe :show-header="false">
        <el-table-column prop="title" label="title">
          <template #default="{ row }">
            <img src="@/assets/images/trend.png" alt="">
            &nbsp;
            <span>{{ row.title }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="date" width="200" />
      </el-table>

      <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 { getTrendAPI } from '@/apis/home'

const router = useRouter()

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

onMounted(() => {
  getTrendData()
})

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

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

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

const handleClear = () => {
  queryParams.value.title = ''
  getTrendData()
}

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

const handleRowClick = (row) => {
  if (row.type == 1) {
    goTarget(row.url)
  } else {
    router.push(`news/${row.trendId}`)
  }
}
</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;
    }
  }

  .news-card {
    margin-top: 20px;
    // box-shadow: 10px 10px 10px 90px #FAEADF;

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

      .title {
        font-size: 20px;
        font-weight: bold;
        margin-bottom: 5px;
      }
    }

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

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

}
</style>