<template>
  <div id="MyCollect">
    <van-swipe-cell v-for="(item, index) in CollectList" :key="index" :name="'collectItem' + index" :before-close="beforeClose">
      <div class="collect_item" @click="handleCourse(item)">
        <div class="course_img" :style="{ backgroundImage: 'url(' + item.img + ')' }"></div>
        <div class="right">
          <p class="course_name">{{ item.title }}</p>
          <p class="course_price">
            <span>¥ {{ item.price }}起</span><van-icon name="delete-o" @click.stop="deleteCollect(index)" />
          </p>
        </div>
      </div>
      <template #right>
        <div class="delete_button">
          <span>取消<br />收藏</span>
        </div>
      </template>
    </van-swipe-cell>
  </div>
</template>
<script>
export default {
  data() {
    return {
      CollectList: [],
    }
  },
  mounted() {
    this.getMyCollect()
  },
  methods: {
    getMyCollect() {
      const userInfo = JSON.parse(localStorage.getItem('userInfo'))
      this.yxAxios
        .post(`${this.proxyUrl}/api/product/getMyCollect`, {
          centerNo: userInfo.centerNo,
        })
        .then((res) => {
          if (res.data.code == 200) {
            this.CollectList = res.data.rows
          } else {
            this.$toast.fail(res.data.message)
          }
        })
    },
    // position 为关闭时点击的位置
    beforeClose({ position, instance, name }) {
      switch (position) {
        case 'right':
          this.$dialog
            .confirm({
              title: '确定取消收藏吗？',
            })
            .then(() => {
              let index = name.replace('collectItem', '')
              this.cancelCollect(index)
              instance.close()
            })
            .catch(() => {
              instance.close()
            })
      }
    },
    handleCourse(item) {
      this.$router.push({ name: 'ServiceAbroadDetail', query: { courseId: item.productId, publicName: localStorage.getItem('publicName') } })
    },
    // 点击按钮取消收藏
    deleteCollect(index) {
      this.$dialog
        .confirm({
          title: '确定取消收藏吗？',
        })
        .then(() => {
          this.cancelCollect(index)
        })
    },
    // 取消收藏
    cancelCollect(index) {
      const userInfo = JSON.parse(localStorage.getItem('userInfo'))
      this.yxAxios
        .post(`${this.proxyUrl}//api/product/cancelCollect`, {
          centerNo: userInfo.centerNo,
          productId: this.CollectList[index].productId,
        })
        .then((res) => {
          if (res.data.code == 200) {
            this.$toast('取消收藏成功')
            this.getMyCollect()
          } else {
            this.$toast.fail(res.data.message)
          }
        })
    },
  },
}
</script>
<style lang="scss">
#MyCollect {
  .collect_item {
    padding: 32px 36px;
    &:not(:nth-of-type()) {
      border-bottom: 1px solid #f0f0f0;
    }
    .course_img {
      width: 150px;
      height: 150px;
      overflow: hidden;
      background-size: cover;
      border-radius: 12px;
      display: inline-block;
      vertical-align: top;
    }
    .right {
      display: inline-flex;
      flex-direction: column;
      justify-content: space-between;
      width: 500px;
      height: 150px;
      margin-left: 20px;
      .course_name {
        font-size: 28px;
      }
      .course_price {
        display: flex;
        justify-content: space-between;
        font-size: 24px;
        color: #999;
      }
    }
  }
  .delete_button {
    width: 140px;
    height: 100%;
    background: #bbb;
    position: relative;
    span {
      font-size: 28px;
      color: #fff;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  }
}
</style>
