<template>
  <div class="app-container">
    <div class="page-section-title">课程搜索结果</div>

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="内容名称" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入内容名称"
          clearable
          size="small"
          style="width: 260px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['course:publicCourseSearch:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-refresh"
          size="mini"
          :loading="refreshLoading"
          @click="handleRefresh"
          v-hasPermi="['course:publicCourseSearch:list']"
        >刷新数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" border :data="dataList">
      <el-table-column label="序号" align="center" width="70">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="搜索词" align="center" prop="keyword" min-width="160" show-overflow-tooltip />
      <el-table-column label="搜索次数" align="center" prop="searchPv" width="120" />
      <el-table-column label="搜索人数" align="center" prop="searchUv" width="120" />
      <el-table-column label="关联课程数量" align="center" prop="relatedCourseCount" width="130" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listPublicCourseSearchKeywordStat,
  exportPublicCourseSearchKeywordStat,
  refreshPublicCourseSearchKeywordStat
} from '@/api/course/publicCourseCourseLink'

export default {
  name: 'PublicCourseCourseLink',
  data() {
    return {
      loading: true,
      exportLoading: false,
      refreshLoading: false,
      showSearch: true,
      total: 0,
      dataList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listPublicCourseSearchKeywordStat(this.queryParams).then(response => {
        this.dataList = response.rows || []
        this.total = response.total || 0
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleExport() {
      this.exportLoading = true
      exportPublicCourseSearchKeywordStat(this.queryParams).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => {
        this.exportLoading = false
      })
    },
    handleRefresh() {
      this.refreshLoading = true
      refreshPublicCourseSearchKeywordStat().then(response => {
        this.msgSuccess(response.msg || '刷新成功')
        this.getList()
        this.refreshLoading = false
      }).catch(() => {
        this.refreshLoading = false
      })
    }
  }
}
</script>

<style scoped>
.page-section-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  padding-left: 10px;
  border-left: 4px solid #409eff;
  margin-bottom: 18px;
  line-height: 1.4;
}
</style>
