<template>
  <ai-list class="Learning">
    <ai-title
      slot="title"
      title="新手园地"
      isShowBottomBorder>
    </ai-title>
    <template slot="content">
      <ai-search-bar>
        <template #left>
          <el-radio-group v-model="search.categoryId" @change="onChange">
            <el-radio-button label="">全部</el-radio-button>
            <el-radio-button label="isFavorite">我的收藏</el-radio-button>
            <el-radio-button :label="item.id" :key="item.id" v-for="item in cateList">{{ item.name }}</el-radio-button>
          </el-radio-group>
        </template>
        <template #right>
        </template>
      </ai-search-bar>
      <ai-table
        :tableData="tableData"
        :col-configs="colConfigs"
        :total="total"
        :current.sync="search.current"
        :size.sync="search.size"
        style="margin-top: 8px;"
        @getList="getList">
        <el-table-column slot="options" label="操作" align="center" fixed="right" width="140px">
          <template v-slot="{ row }">
            <div class="table-options">
              <el-button type="text" @click="collection(row.id, row.isFavorite)">{{ row.isFavorite === '0' ? '收藏' : '取消收藏' }}</el-button>
              <el-button type="text" @click="toDetail(row.url)">详情</el-button>
            </div>
          </template>
        </el-table-column>
      </ai-table>
    </template>
  </ai-list>
</template>

<script>
  export default {
    name: 'Learning',

    data () {
      return {
        colConfigs: [
          { prop: 'title', label: '标题', align: 'left' },
          { prop: 'createTime', label: '发布时间', align: 'center' },
        ],
        tableData: [],
        total: 0,
        search: {
          current: 1,
          size: 10,
          categoryId: ''
        },
        cateList: [],
        isFavorite: 0
      }
    },

    created () {
      this.$store.dispatch('getUserInfo').then(e => {
        console.log(e)
      })
      this.getCateList()
      this.getList()
    },

    methods: {
      toDetail (url) {
        window.open(url)
      },

      onChange (e) {
        if (e === 'isFavorite') {
          this.$http.post('/api/learning/favoritePage', null, {
            params: {
              ...this.search
            }
          }).then(res => {
            if (res.code === 0) {
              this.tableData = res.data.records
              this.total = res.data.total
            }
          })
        } else {
          this.search.current = 1
          this.getList()
        }
      },

      collection (id, isFavorite) {
        this.$confirm(isFavorite === '0' ? '确定收藏该文章?' : '确定取消收藏？', '温馨提示', {
          confirmButtonText: '确定',
          callback: action => {
            if (action === 'confirm') {
              this.$http.post(isFavorite === '0' ? `/api/learning/addFavorite?id=${id}` : `/api/learning/delFavorite?id=${id}`).then(res => {
                if (res.code === 0) {
                  this.$message.success(isFavorite === '1' ? '取消成功' : '收藏成功')
                  this.getList()
                }
              })
            }
          }
        })
      },

      getCateList () {
        this.$http.post('/api/learningCategory/page?size=50').then(res => {
          if (res.code === 0) {
            this.cateList = res.data.records
          }
        })
      },

      getList () {
        this.$http.post('/api/learning/pluginPage', null, {
          params: {
            ...this.search
          }
        }).then(res => {
          if (res.code === 0) {
            this.tableData = res.data.records
            this.total = res.data.total
          }
        })
      }
    }
  }
</script>

<style scoped lang="scss">
</style>
