<template>
  <!-- #ifdef H5 -->
  <zj-page-layout
    :hasFooter="false"
    :isScroll="true"
    :refresherTriggered="refresherTriggered"
    @refresherrefresh="refresherrefresh"
    @scrolltolower="scrolltolower">
    <view class="all-container" :style="'background:' + ((pageConfig[2] && dataList.length > 0) ? pageConfig[2] : 'unset')">
      <view class="banner-container" v-if="pageConfig[1]">
        <image :src="pageConfig[1]" mode="widthFix"></image>
      </view>

      <view class="top-container">
        <view class="tab">
          <view class="item" :class="screenType === 0 ? 'current':''" @tap="changeScreen(0)">综合</view>
          <view class="item" :class="screenType === 1 ? 'current':''" @tap="changeScreen(1)">销量</view>
          <view class="item" :class="screenType === 2 || screenType === 3 ? 'current':''"  @tap="changeScreen(2)">价格
            <image src="@/static/icon/price_1.png" v-if="screenType === 2"></image>
            <image src="@/static/icon/price_2.png" v-if="screenType === 3"></image>
            <image src="@/static/icon/price_0.png" v-if="screenType != 2 && screenType != 3"></image>
          </view>
          <view class="item" :class="screenType === 4 ? 'current':''" @tap="changeScreen(4)">上架时间</view>
        </view>
        <view class="icon">
          <image src="@/static/icon/show_1.png" v-if="showType == 1" @tap="showType = 2"></image>
          <image src="@/static/icon/show_2.png" v-if="showType == 2" @tap="showType = 1"></image>
        </view>
      </view>

      <view class="goods-waterfall-list" v-show="showType == 1">
        <view class="left">
          <block v-for="(item, index) in dataList" :key='index'>
            <view class="item" v-if="index%2==0" @tap="toGoodsDetail(item.goodsId)">
              <view class="image">
                <image :src="item.imgUrl" mode="aspectFill" class="img"></image>
                <image :src="item.logo" mode="aspectFill" class="water" v-if="item.isShowWater"></image>
              </view>
              <view class="content">
                <view class="title ellipsis-2">{{item.goodsName}}</view>
                <view class="tags" v-if="item.tags1 && item.tags1.length > 0">
                	<view class="it" v-for="(it, idx) in item.tags1" :key="idx">{{it}}</view>
                </view>
                <view class="tags2" v-if="(item.tags2 && item.tags2.length > 0) || item.promotionFullPiece || item.isTrade == 'YES'">
                	<view class="it" v-if="item.promotionFullPiece">满件打折</view>
                  <view class="it" v-if="item.isTrade == 'YES'">以旧换新</view>
                	<view class="it" v-for="(it, idx) in item.tags2" :key="idx">{{it}}</view>
                </view>
                <view class="price">
                  <view class="price-1">¥{{item.goodsPrice | priceFilter}}</view>
                  <view class="price-2">¥{{item.orgGoodsPrice | priceFilter}}</view>
                </view>
                <view class="text">销量：{{item.soldNum}}</view>
              </view>
            </view>
          </block>
        </view>
        <view class="right">
          <block v-for="(item, index) in dataList" :key='index'>
            <view class="item" v-if="index%2==1" @tap="toGoodsDetail(item.goodsId)">
              <view class="image">
                <image :src="item.imgUrl" mode="aspectFill" class="img"></image>
                <image :src="item.logo" mode="aspectFill" class="water" v-if="item.isShowWater"></image>
              </view>
              <view class="content">
                <view class="title ellipsis-2">{{item.goodsName}}</view>
                <view class="tags" v-if="item.tags1 && item.tags1.length > 0">
                	<view class="it" v-for="(it, idx) in item.tags1" :key="idx">{{it}}</view>
                </view>
                <view class="tags2" v-if="(item.tags2 && item.tags2.length > 0) || item.promotionFullPiece || item.isTrade == 'YES'">
                	<view class="it" v-if="item.promotionFullPiece">满件打折</view>
                  <view class="it" v-if="item.isTrade == 'YES'">以旧换新</view>
                	<view class="it" v-for="(it, idx) in item.tags2" :key="idx">{{it}}</view>
                </view>
                <view class="price">
                  <view class="price-1">¥{{item.goodsPrice | priceFilter}}</view>
                  <view class="price-2">¥{{item.orgGoodsPrice | priceFilter}}</view>
                </view>
                <view class="text">销量：{{item.soldNum}}</view>
              </view>
            </view>
          </block>
        </view>
      </view>

      <view class="goods-row-list" v-show="showType == 2">
        <block v-for="(item, index) in dataList" :key='index'>
          <view class="item" @tap="toGoodsDetail(item.goodsId)">
            <view class="image">
              <image :src="item.imgUrl" mode="aspectFill" class="img"></image>
              <image :src="item.logo" mode="aspectFill" class="water" v-if="item.isShowWater"></image>
            </view>
            <view class="right">
              <view>
                <view class="title ellipsis-2">{{item.goodsName}}</view>
                <view class="tags" v-if="item.tags1 && item.tags1.length > 0">
                  <view class="it" v-for="(it, idx) in item.tags1" :key="idx">{{it}}</view>
                </view>
              </view>
              <view>
                <view class="bottom">
                  <view class="price">
                    <view class="price-1">¥{{item.goodsPrice | priceFilter}}</view>
                    <view class="price-2">¥{{item.orgGoodsPrice | priceFilter}}</view>
                  </view>
                  <view class="text">销量：{{item.soldNum}}</view>
                </view>
              </view>
            </view>
          </view>
        </block>
      </view>

      <Loading :loadStatus="loadStatus" :dataList="dataList" />
    </view>
  </zj-page-layout>
  <!-- #endif -->
  <!-- #ifndef H5 -->
  <web-view :src="webViewHref(`/packageGoods/pages/activity`, pam,crossPagePam)" @message="crossPage.$listener"></web-view>
  <!-- #endif -->
</template>

<script>
  // #ifdef H5
  import { compareTime } from '@/common/utils/util.js'

	export default {
		data() {
			return {
				type: null,
				screenType: '',
				dataList: [],
				pageNum: 1,
        loadStatus: 0,
        refresherTriggered: false,
				showType: 1,
        templateInfo: {},
        pageConfig: ['','',''],
			}
		},

		onShow() {
			this.crossPage.$on('hanbleShare', () => {
				this.clickShare();
			})
		},

		onHide() {
			this.crossPage.$off('hanbleShare');
		},

		async onLoad({type}) {
			this.type = type;

      let tif = await this.$getTemplateInfo();
      this.templateInfo = tif;
      const MAP = {
      	2: ['首页弹窗', tif.popupHeadImage, tif.popupBackgroundColor],
      	3: ['活动专区1', tif.active1HeadImage, tif.active1BackgroundColor],
      	4: ['活动专区2左侧', tif.active2LeftHeadImage, tif.active2LeftBackgroundColor],
      	5: ['活动专区2右侧', tif.active2RightHeadImage, tif.active2RightBackgroundColor],
      	6: ['专场专区1', tif.only1HeadImage, tif.only1BackgroundColor],
      	7: ['专场专区2', tif.only2HeadImage, tif.only2BackgroundColor],
      	8: ['专场专区3', tif.only3HeadImage, tif.only3BackgroundColor],
      	9: ['专场专区4', tif.only4HeadImage, tif.only4BackgroundColor],
      	10: ['专题精选1', tif.topics1HeadImage, tif.topics1BackgroundColor],
      	11: ['专题精选2', tif.topics2HeadImage, tif.topics2BackgroundColor],
      	12: ['专题精选3', tif.topics3HeadImage, tif.topics3BackgroundColor],
      	13: ['底部广告图', tif.bottomBannerHeadImage, tif.bottomBannerBackgroundColor],
      }
      console.log(this.type ? MAP[this.type] : ['','','']);
      this.pageConfig = this.type ? MAP[this.type] : ['','',''];

			this.getList();
		},

		methods: {
			// 获取商品列表
			getList() {
				this.$api.get('/renovation/goods/list', {
          pageNo: this.pageNum,
          pageSize: 10,
          type: this.type,
          objId: this.templateInfo.companyWechatTemplateId,
          sort: this.screenType
				}).then(res => {
					res.data.records.forEach(item => {
						if(item.logo && item.logoStartTime) {
							item.isShowWater = compareTime(item.logoStartTime, item.logoEndTime);
						}else {
							item.isShowWater = false;
						}
					})

          this.loadStatus = 0;
          let list = res.data.records;
          if (list.length < 10) {
            this.loadStatus = 2;
          }
          this.dataList = this.dataList.concat(list);
				}).catch(() => {
          this.loadStatus = 2;
        }).finally(res => {
          this.refresherTriggered = false;
        })
			},

      // 滚动到底部
      scrolltolower(e) {
        if (this.loadStatus === 0) {
          this.pageNum++;
          this.getList();
        }
      },

      // 触发下拉刷新
      refresherrefresh(e) {
        this.refresherTriggered = true;
        this.refreshList();
      },

      refreshList() {
        this.dataList = [];
        this.pageNum = 1;
        this.getList();
      },

			// 切换筛选类型
			changeScreen(type) {
				if(type != 2) {
					if(this.screenType !== type) {
						this.screenType = type;
					}else {
						this.screenType = '';
					}
				}else {
					if(this.screenType != 2 && this.screenType != 3) {
						this.screenType = 2;
					}else if(this.screenType == 2) {
						this.screenType = 3;
					}else {
						this.screenType = '';
					}
				}
				this.refreshList();
			},

			toGoodsDetail(id) {
				this.$navToPage({
					url: '/packageGoods/pages/detail?id=' + id
				})
			}
		}
	}
  // #endif
  // #ifndef H5
  export default {
    data() {
      return {
        pam: {},
      }
    },

    onLoad(pam) {
      this.pam = pam;
    },
  }
  // #endif
</script>

<style lang="scss" scoped>
	.all-container {
		box-sizing: border-box;
		&.noBg {
			background: unset !important;
		}
	}
	.banner-container {
		image {
			width: 100%;
			display: block;
		}
	}
	.top-container {
		position: sticky;
		top: 0;
		left: 0;
		z-index: 99;
		width: 100%;
		background: #FFFFFF;
		display: flex;
		padding: 0 20rpx;
		align-items: center;
		box-sizing: border-box;
		.tab {
			flex: 1;
			display: flex;
			padding: 0 80rpx 0 30rpx;
			box-sizing: border-box;
			justify-content: space-between;
			.item {
				height: 88rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 30rpx;
				color: #666666;
				&.current {
					color: $theme-color;
				}
				image {
					width: 18rpx;
					height: 30rpx;
					display: block;
					margin-left: 10rpx;
				}
			}
		}
		.icon {
			padding-right: 10rpx;
			image {
				width: 36rpx;
				height: 36rpx;
				display: block;
			}
		}
	}
</style>
