<template>
  <MyHeader></MyHeader>
  <div class="container">
    <el-card class="news-card">
      <template #header>
        <div class="card-header">
          <div class="header-search">
            <el-space wrap>
              <span>名称</span>
              <el-input v-model="queryParams.title" style="width: 300px" placeholder="请输入关键词" clearable
                @clear="handleClear" />

              <span>格式</span>
              <el-select v-model="queryParams.fileSuffix" placeholder="请选择格式" style="width: 300px"
                @keyup.enter="handleQuery">
                <el-option v-for="item in formatOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
              <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
            </el-space>
          </div>
        </div>
      </template>

      <el-table @row-click="handleRowClick" :data="tableData" class="table" stripe>
        <el-table-column prop="title" label="课件名称" />
        <el-table-column prop="createTime" label="课件格式" />
        <el-table-column prop="createTime" label="课件大小" />
        <el-table-column prop="createTime" label="目录" />
        <el-table-column prop="createTime" label="创建时间" 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>
  <MyFooter></MyFooter>
</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 formatOptions = [
  {
    value: '.doc',
    label: 'doc',
  },
  {
    value: '.docx',
    label: 'docx',
  },
  {
    value: '.xls',
    label: 'xls',
  },
  {
    value: '.xlsx',
    label: 'xlsx',
  },
  {
    value: '.ppt',
    label: 'ppt',
  },
  {
    value: '.pptx',
    label: 'pptx',
  },
  {
    value: '.pdf',
    label: 'pdf',
  },
  {
    value: '.txt',
    label: 'txt',
  }
]

const queryParams = ref({
  title: '',
  fileSuffix: '',
  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 {
  .news-card {
    margin-top: 20px;

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

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

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

}
</style>