<template>
  <view class="container" :style="appThemeStyle">
    <mescroll-body ref="mescrollRef" :sticky="true" @init="mescrollInit" :down="{ use: false }" :up="upOption" @up="upCallback">

      <!-- 文章列表 -->
      <view class="article-list">
        <view class="article-item" :class="[`show-type__${item.show_type}`]" v-for="(item, index) in articleList.data" :key="index"
          @click="onTargetDetail(item.solution_id)">
          <!-- 小图模式 -->
          <block v-if="item.show_type == 10">
            <view class="article-item__image article-item__left">
              <image class="image" mode="widthFix" :src="item.image_url"></image>
            </view>
            <view class="flex-box">
              <view class="article-item__title">
                <text class="twoline-hide">{{ item.title }}</text>
              </view>
             <!-- <view class="article-item__footer m-top10">
                <text class="article-views f-24 col-8">{{ item.show_views }}次浏览</text>
              </view> -->
            </view>
          </block>
          <!-- 大图模式 -->
          <block v-if="item.show_type == 20">
            <view class="article-item__title">
              <text class="twoline-hide">{{ item.title }}</text>
            </view>
            <view class="article-item__image m-top20">
              <image class="image" mode="widthFix" :src="item.image_url"></image>
            </view>
            <!-- <view class="article-item__footer m-top10">
              <text class="article-views f-24 col-8">{{ item.show_views }}次浏览</text>
            </view> -->
          </block>
        </view>
      </view>
    </mescroll-body>
  </view>
</template>

<script>
  import MescrollBody from '@/components/mescroll-uni/mescroll-body.vue'
  import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins'
  import * as Api from '@/api/solution/index.js'
  import * as UserApi from '@/api/user'
  import * as CategoryApi from '@/api/solution/category'
  import { getEmptyPaginateObj, getMoreListData } from '@/core/app'

  const pageSize = 15

  export default {
    components: {
      MescrollBody
    },
    mixins: [MescrollMixin],
    data() {
      return {
		CategoryId: 0,
        // 文章列表
        articleList: getEmptyPaginateObj(),
		userInfo: {},
		// 订单类型
		orderType: false,
        // 上拉加载配置
        upOption: {
          // 首次自动执行
          auto: true,
          // 每页数据的数量; 默认10
          page: { size: pageSize },
          // 数量要大于3条才显示无更多数据
          noMoreSize: 3,
        }
      }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      const app = this
	  console.log("options: ",options);
	  app.CategoryId = options.categoryId
	  app.orderType = options.orderType
      // 获取文章分类数据
      app.getArticleList()
	  app.getUserInfo()
    },

    methods: {

      /**
       * 上拉加载的回调 (页面初始化时也会执行一次)
       * 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10
       * @param {Object} page
       */
      upCallback(page) {
        const app = this
        // 设置列表数据
        app.getArticleList(page.num)
          .then(list => {
            const curPageLen = list.data.length
            const totalSize = list.data.total
            app.mescroll.endBySize(curPageLen, totalSize)
          })
          .catch(() => app.mescroll.endErr())
      },

      /**
       * 获取文章列表
       * @param {Number} pageNo 页码
       */
      getArticleList(pageNo = 1) {
        const app = this
        return new Promise((resolve, reject) => {
          Api.list({ categoryId: app.CategoryId, page: pageNo }, { load: false })
            .then(result => {
              // 合并新数据
              const newList = result.data.list
              app.articleList.data = getMoreListData(newList, app.articleList, pageNo)
              resolve(newList)
            })
            .catch(reject)
        })
      },
	  
	  // 获取当前用户信息
	  getUserInfo() {
	  	const app = this
		UserApi.info()
			.then(result => {
				app.userInfo = result.data.userInfo
			})
	  },

      // 获取当前标签项的值
      getTabValue() {
        const app = this
        return app.tabList.length ? app.tabList[app.curTab].value : 0
      },

      // 刷新列表数据
      onRefreshList() {
        this.articleList = getEmptyPaginateObj()
        setTimeout(() => this.mescroll.resetUpScroll(), 120)
      },

      // 跳转文章详情页
      onTargetDetail(articleId) {
		const { orderType } = this
		if( orderType ){
			return this.$navTo('package/solution/detail', { articleId })
		}
		return this.$navTo('package/solution/buy/index')
      }

    },

    /**
     * 分享当前页面
     */
    onShareAppMessage() {
      return {
        title: '文章首页',
        path: "/pages/article/index?" + this.$getShareUrlParams()
      }
    },

    /**
     * 分享到朋友圈
     * 本接口为 Beta 版本，暂只在 Android 平台支持，详见分享到朋友圈 (Beta)
     * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
     */
    onShareTimeline() {
      return {
        title: '文章首页',
        path: "/pages/article/index?" + this.$getShareUrlParams()
      }
    }

  }
</script>

<style lang="scss" scoped>
  .container {
    min-height: 100vh;
  }

  // 文章列表
  .article-list {
    padding-top: 20rpx;
    line-height: 1;
    background: #f7f7f7;
  }


  .article-item {
    margin-bottom: 20rpx;
    padding: 30rpx;
    background: #fff;

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

    .article-item__title {
      max-height: 74rpx;
      font-size: 28rpx;
      line-height: 38rpx;
      color: #333;
    }

    .article-item__image .image {
      display: block;
    }
  }

  // 小图模式
  .show-type__10 {
    display: flex;

    .article-item__left {
      padding-right: 20rpx;
    }

    .article-item__title {
      // min-height: 72rpx;
    }

    .article-item__image .image {
      width: 240rpx;
    }
  }

  // 大图模式
  .show-type__20 .article-item__image .image {
    width: 100%;
  }
</style>
