<template>
  <div class="collection">
    <div class="gap10 title">
      <div class="line_vertical"></div>
      <div class="">{{ $t('personalCenter.myCollection') }}</div>
    </div>
    <div class="collection-list">
      <div @click.prevent="toDetail(item)" class="flex-center-between li" v-for="(item, index) in list" :key="index">
        <img class="image" :src="item.coverImageUrl" alt="">
        <div class="collection-list-main">
          <div class="collection-list-main-left">
            <div class="titles">{{ item.courseTitle }}</div>
            <div class="gap10 tag">
              <el-button type="primary" size="large" plain>{{ item.skillTag }}</el-button>
            </div>
            <div class="">{{ item.courseIntro }}</div>
          </div>
          <div @click.stop.prevent="cancelCollect(item, index)" class="btn flex-center active">
            <img src="/src/assets/imgs/my/star@2x.png" alt="">
            <div class="">{{ $t('common.cancelCollect') }}</div>
          </div>
        </div>
      </div>
    </div>
    <template v-if="list.length">
      <Pagination :total="form.total" :page-size="form.pageSize" :current-page="form.pageNum"
        @page-change="handlePageChange" />
    </template>
    <el-empty v-else :description="$t('common.empty')" />
  </div>
</template>
<script setup lang="ts">
import Pagination from '@/components/Pagination.vue'

import { ref, onMounted } from 'vue'

import { collectList } from '@/api/my.js'

import { collect } from '@/api/course'

import DGTMessage from '@/utils/message'

import { useI18n } from 'vue-i18n'

const { t } = useI18n()
import { useRouter } from 'vue-router'
const router = useRouter()

const form = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0,
  orderByColumn: 'create_time',
  isAsc: 'desc'
})

const list = ref([])

// 取消收藏
const cancelCollect = async (item, index) => {
  let res = await collect({ objectId: item.objectId });
  DGTMessage.success(`${t('common.unCollect')}${t('common.success')}`)
  getList()
}

const getList = async () => {
  let res = await collectList(form.value);
  form.value.total = res.total;
  list.value = res.rows
}
const handlePageChange = (page, pageSize) => {
  list.value = []
  form.value.pageNum = page;
  if(pageSize)form.value.pageSize = pageSize;
  getList()
}
// 跳转详情
const toDetail = (item: any) => {
  router.push({
    path: `/learning-system/detail/${item.objectId}/course/${item.objectId}`,
    query: {
      courseId: item.objectId,
      metaTitle: item.courseTitle || '课程详情'
    }
  })
}

onMounted(() => {
  getList()
})
</script>
<style scoped lang="scss">

.collection {
  padding-bottom: 20px;

  .title {
    height: 60px;
    font-size: 20px;
    font-weight: bold;
    color: #333333;
  }

  .collection-list {
    .li {
      padding: 16px;
      margin-bottom: 16px;
      background: #F5F7FA;
      border-radius: 16px;

      &:last-child {
        margin-bottom: 0;
      }

      .image {
        width: 213px;
        height: 120px;
        object-fit: cover;
        border-radius: 8px;
        margin-right: 16px;
      }

      .collection-list-main {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .collection-list-main-left {
          flex: 1;
          margin-right: 16px;

          .titles {
            font-size: 18px;
            font-weight: bold;
            color: #333333;
          }

          .tag {
            margin: 8px 0;
          }

          .content {
            color: #666666;
            font-size: 14px;
            line-height: 22px;
          }
        }

        .btn {
          width: 120px;
          height: 32px;
          color: #FFFFFF;
          font-size: 14px;
          cursor: pointer;
          background: linear-gradient(270deg, #0055FE 0%, #C832FA 100%);
          border-radius: 4px;

          &:hover {
            opacity: 0.7;
          }

          img {
            width: 14px;
            height: 14px;
            margin-right: 4px;
          }
        }

        .active {
          background: #FFFFFF;
          color: #2D71FF;
        }
      }
    }
  }
}
</style>