<template>
  <!-- #ifdef H5 -->
  <zj-page-layout>
    <template slot="header">
      <view class="search-container">
        <u-search
          shape="round"
          :showAction="false"
          placeholder="搜索您需要的服务"
          v-model="keyword"
          @search="searchData"
          @clear="searchData">
        </u-search>
      </view>
      <!-- <view class="address-container">
        <u-notice-bar direction="column" icon="map" mode="link" :text="[location]" @click="getAddress"></u-notice-bar>
      </view> -->
    </template>

    <view class="main-container">
      <view class="left">
        <block v-for="(item, index) in classifyList" :key='index'>
          <view class="item ellipsis" :class="curClassify == item.categoryId ? 'current':''" @tap="changeLeft(item.categoryId)">{{item.name}}</view>
        </block>
      </view>
      <view class="right">
        <view class="goods-item" v-for="(item, index) in goodsList" :key='index' @tap="toGoodsDetail(item.goodsId)">
          <image :src="item.imgUrl" mode="aspectFill"></image>
          <view class="main">
            <view class="name">{{item.goodsName}}</view>
            <view class="bottom">
              <view class="price"><text>¥</text>{{item.goodsPrice | priceFilter}}</view>
            </view>
          </view>
        </view>

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

<script>
  // #ifdef H5
	export default {
		data() {
			return {
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar,
				keyword: '',
				classifyList: [],
				curClassify: 0,
				goodsList: [],
				loadStatus: 0,
				location: '点击获取定位',
			}
		},
    
		onLoad({categoryId}) {
			this.getAddress();
			this.getClassifyList(categoryId);
		},

		methods: {
			async getAddress() {
				const lo = await this.$getAddress();
				this.location = `${lo.province} ${lo.city} ${lo.area}`;
			},

			// 获取分类列表
			getClassifyList(categoryId) {
				this.$api.get('/goods/category/list', {
          type: 1
        }).then(res => {
					res.data.unshift({
						categoryId: '',
						name: '全部'
					})
					this.classifyList = res.data;
					if(categoryId) {
						this.curClassify = categoryId;
					} else {
						this.curClassify = res.data.length > 0 ? res.data[0].categoryId : 0;
					}
					this.getGoodsList();
				})
			},

			// 获取商品列表
			getGoodsList() {
				this.loadStatus = 1;
				this.$api.get('/goods/list/sort/page', {
          pageNum: 1,
          pageSize: -1,
          categoryId: this.curClassify,
          keyword: this.keyword
				}).then(res => {
					this.loadStatus = 0;
					let list = res.data.records;
					if(list.length < 1){
						this.loadStatus = 2;
					}
					this.goodsList = list;
				}).catch(() => {
					this.loadStatus = 2;
				})
			},

			searchData() {
				this.getGoodsList();
			},

			// 切换分类
			changeLeft(pid) {
				this.curClassify = pid;
				this.getGoodsList();
			},

			// 进入商品详情
			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">
	.search-container {
		background: #ffffff;
    padding: 0 20rpx 20rpx;
		::v-deep .u-search {
			height: 60rpx;
      input {
        background: #F7F8FF !important;
      }
			.u-search__content {
        background: #F7F8FF !important;
				height: 60rpx;
			}
		}
	}
	.address-container {
		background: #ffffff;
		padding-top: 10rpx;
	}

	.main-container {
		display: flex;
		height: 100%;
		.left {
			width: 220rpx;
			height: 100%;
			overflow-y: scroll;
			box-sizing: border-box;
			flex-shrink: 0;
			.item {
				font-size: 28rpx;
				color: #333333;
				height: 100rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				position: relative;
				&.current {
					background: #FFFFFF;
					font-weight: 500;
					&::after {
						content: '';
						width: 8rpx;
						height: 38rpx;
						background: $theme-color;
						position: absolute;
						top: 50%;
						left: 0;
						margin-top: -19rpx;
						border-radius: 0 8rpx 8rpx 0;
					}
				}
			}
		}
		.right {
			flex: 1;
			background: #FFFFFF;
			border-radius: 10rpx;
			padding: 0 20rpx 20rpx 20rpx;
			height: 100%;
			overflow-y: scroll;
			box-sizing: border-box;
			.goods-item {
				padding: 20rpx 0;
				border-bottom: 1px solid #f5f5f5;
				&:last-child {
					border: none;
				}
			}
		}
	}

	.bottom-container {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx 30rpx;
		.left {
			position: relative;
			.iconfont {
				font-size: 44rpx;
				color: $sec-font;
			}
		}
		.right {
			display: flex;
			align-items: center;
			.total {
				display: flex;
				align-items: center;
				flex-shrink: 0;
				margin-right: 20rpx;
				.text {
					flex-shrink: 0;
				}
				.price {
					color: $minor-color;
					font-weight: 500;
					font-size: 32rpx;
					text {
						font-size: 24rpx;
					}
				}
			}
			::v-deep .u-button {
				width: 200rpx;
				border-radius: 80rpx;
			}
		}
	}

</style>
