<template>
	<gui-page>
		<view slot="gBody">

			<view style="padding-bottom: 100rpx;">

				<view class="container" :style="appThemeStyle">
					<mescroll-body ref="mescrollRef" :sticky="true" @init="mescrollInit" :down="{ native: true }"
						@down="downCallback" :up="upOption" @up="upCallback">
						<!-- 排序标签 -->
						<view class="store-sort" style="z-index: 999;">
							<view class="sort-item" :class="{ active: sortType === 'all' }"
								@click="handleSortType('all')">
								<text>全部</text>
							</view>
							<view class="sort-item" :class="{ active: sortType === 'fbz' }"
								@click="handleSortType('fbz')">
								<text>发布中</text>
							</view>
							<view class="sort-item" :class="{ active: sortType === 'yxj' }"
								@click="handleSortType('yxj')">
								<text>已下架</text>
							</view>
						</view>
						<view style="padding: 20rpx;"
							class="gui-flex gui-rows gui-nowrap gui-align-items-center gui-space-between">
							<view @click="caozuo('qx')"
								style="font-size: 24rpx;padding: 5rpx 0;background-color: #F7E7F2;color: #AB1581;border-radius: 10rpx;text-align: center;width: 125rpx;">
								<text
									v-if="list.data.length > 0 && goodsIds.length > 0">{{list.data.length == goodsIds.length ? '取消' : ''}}</text>全选
							</view>
							<view @click="caozuo('sj')"
								style="font-size: 24rpx;padding: 5rpx 0;background-color: #F7E7F2;color: #AB1581;border-radius: 10rpx;text-align: center;width: 125rpx;">
								上架</view>
							<view @click="caozuo('xj')"
								style="font-size: 24rpx;padding: 5rpx 0;background-color: #F7E7F2;color: #AB1581;border-radius: 10rpx;text-align: center;width: 125rpx;">
								下架</view>
							<view @click="caozuo('sc')"
								style="font-size: 24rpx;padding: 5rpx 0;background-color: #F7E7F2;color: #AB1581;border-radius: 10rpx;text-align: center;width: 125rpx;">
								删除</view>
							<view @click="caozuo('fz')"
								style="font-size: 24rpx;padding: 5rpx 0;background-color: #F7E7F2;color: #AB1581;border-radius: 10rpx;text-align: center;width: 125rpx;">
								复制</view>

						</view>
						<!-- 商品列表 @click="onTargetDetail(item.goods_id)" -->
						<view class="goods-list clearfix" :class="['column-' + (showView ? '1' : '2')]">
							<view class="goods-item" v-for="(item, index) in list.data" :key="index"
								@click="onTargetDetail(item.goods_id)">
								<view style="position: absolute;z-index: 99;padding: 5rpx 0 0 5rpx;">
									<view @click.stop="xuanze(index)"
										style="width: 40rpx;height: 40rpx;border-radius: 50%;background: linear-gradient(to top, #4D0868, #9E1A91);color: #fff;text-align: center;font-size: 20rpx;line-height: 40rpx;"
										v-if="item.xuanze">
										<u-icon name="checkbox-mark"></u-icon>
										</view>
										
									<view @click.stop="xuanze(index)"
										style="width: 40rpx;height: 40rpx;border-radius: 50%;border: 1px solid #4D0868;"
										v-else></view>
								</view>
								<!-- 单列显示 -->
								<view v-if="showView" class="dis-flex">
									<!-- 商品图片 -->
									<view class="goods-item_left">
										<image class="image" :src="item.goods_image"></image>
									</view>
									<view class="goods-item_right">
										<!-- 商品名称 -->
										<view class="goods-name">
											<text class="twoline-hide">{{ item.goods_name }}</text>
										</view>
										<view class="goods-item_desc">
											<!-- 商品卖点 -->
											<view class="desc-selling_point dis-flex">
												<text class="oneline-hide">{{ item.selling_point }}</text>
											</view>
											<!-- 商品销量 -->
											<view class="desc-goods_sales dis-flex">
												<text>已售{{ item.goods_sales }}件</text>
											</view>
											<!-- 商品价格 -->
											<view class="desc_footer">
												<text class="price_x">¥{{ item.goods_price_min }}</text>
												<text class="price_y col-9"
													v-if="item.line_price_min > 0">¥{{ item.line_price_min }}</text>
											
											</view>
										</view>
									</view>
								</view>
								<!-- 多列显示 -->
								<view v-else class="">
									<!-- 商品图片 -->
									<view class="goods-image">
										<image class="image" mode="aspectFill" :src="item.goods_image"></image>
									</view>
									<view class="detail">
										<!-- 商品名称 -->
										<view class="goods-name">
											<text class="twoline-hide">{{ item.goods_name }}</text>
										</view>
										<!-- 商品价格 -->
										<view class="detail-price oneline-hide">
											<text class="goods-price f-30 col-m">￥{{ item.goods_price_min }}</text>
											<text v-if="item.line_price_min > 0"
												class="line-price col-9 f-24">￥{{ item.line_price_min }}</text>
										<text style="float: right;font-size: 20rpx;padding-top: 10rpx;color:#9E1A91 ;" v-if="item.status==10">已上架</text>
										<text style="float: right;font-size: 20rpx;padding-top: 10rpx;color:#ff0000 ;" v-if="item.status==20">已下架</text>
										
										</view>
									</view>
								</view>
							</view>
						</view>
						<!-- <view @click="adds" style="background: linear-gradient(to top, #4D0868, #9E1A91);position: fixed;bottom: 40rpx;width: 90%;color: #fff;margin: 0 5%;text-align: center;padding: 20rpx 0;border-radius: 50rpx;">
							添加商品
						</view> -->
					</mescroll-body>
				</view>

			</view>
			
			<view style="position: fixed;bottom: 50rpx;width: 100%;">
				<view @click="adds"
					style="width: 60%;text-align: center;padding: 20rpx 0;
					text-align: center;background: linear-gradient(to top, #0E001F, #7B0373 );
					margin: 100rpx auto 0 ;color: #fff;border-radius: 40rpx;font-size: 36rpx;">
					新增
				</view>
			</view>
		</view>
	</gui-page>
</template>

<script>
	import MescrollBody from '@/components/mescroll-uni/mescroll-body.vue'
	import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins'
	import * as GoodsApi from '@/api/goods'
	import {
		getEmptyPaginateObj,
		getMoreListData
	} from '@/core/app'
	import Search from '@/components/search'

	const pageSize = 15
	const showViewKey = 'GoodsList-ShowView';

	export default {
		components: {
			MescrollBody,
			Search
		},
		mixins: [MescrollMixin],
		data() {
			return {
				showView: false, // 列表显示方式 (true列表、false平铺)
				sortType: 'all', // 排序类型
				sortPrice: false, // 价格排序 (true高到低 false低到高)
				options: {}, // 当前页面参数
				list: getEmptyPaginateObj(), // 商品列表数据

				// 上拉加载配置
				upOption: {
					// 首次自动执行
					auto: true,
					// 每页数据的数量; 默认10
					page: {
						size: pageSize
					},
					// 数量要大于4条才显示无更多数据
					noMoreSize: 4,
				},
				goodsIds: []
			}
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			// 记录options
			this.options = options
			// 设置默认列表显示方式
			this.setShowView()
		},
		onShow() {
			this.handleSortType(this.sortType)
		},

		methods: {
			fanhui() {
				uni.navigateBack()
			},
			adds() {
				 uni.setStorageSync('gmxz','')
				  uni.setStorageSync('spxq','')
				uni.navigateTo({
					url: './addgoods'
				})
			},
			
			caozuo(type) {
				let _this = this


				// if (type == 'fz' && _this.goodsIds.length == 0) {
				// 	uni.navigateTo({
				// 		url: './addgoods'
				// 	})
				// 	return false;

				// }
				if (_this.goodsIds.length == 0 && type != 'qx') {
					_this.$toast('请选择商品')
					return false;
				}

				if (type == 'qx') {
					if (_this.list.data.length > 0 && _this.list.data.length == _this.goodsIds.length) {
						_this.list.data.forEach(function(element, index) {
							_this.list.data[index].xuanze = false
						});
						_this.goodsIds = [];
						return false;
					}
					_this.goodsIds = [];
					_this.list.data.forEach(function(element, index) {
						_this.list.data[index].xuanze = true
						_this.goodsIds.push(element.goods_id);
					});
				}

				if (type == 'sj' || type == 'xj') {
					GoodsApi.czstatus({
							status: type == 'sj' ? 10 : 20,
							goodsIds: _this.goodsIds
						})
						.then(result => {
							_this.goodsIds = [];
							_this.handleSortType(_this.sortType)
						})
				}

				if (type == 'sc') {
					uni.showModal({
						title: '友情提示',
						content: '确认要删除该商品吗？',
						success(o) {
							if (o.confirm) {
								GoodsApi.shanchu({
										goodsIds: _this.goodsIds
									})
									.then(result => {
										// 显示成功信息
										_this.$toast(result.message)
										_this.goodsIds = [];
										_this.handleSortType(_this.sortType)
									})
							}
						}
					});
				}

				if (type == 'fz') {
					if (_this.goodsIds.length == 1) {
						uni.showModal({
							title: '友情提示',
							content: '确认要复制该商品吗？',
							success(o) {
								if (o.confirm) {
									_this.$toast('还不能复制')
									uni.navigateTo({
										url: './copy?goodsId=' + _this.goodsIds[0]
									})
								}
							}
						});
					} else {
						_this.$toast('只能选择一个商品进行复制')
						return false;
					}

				}

			},
			xuanze(index) {
				let goods = this.list.data[index]
				if (!this.list.data[index].xuanze) {
					this.goodsIds.push(goods.goods_id);
				} else {
					var index = this.goodsIds.indexOf(goods.goods_id);
					if (index > -1) {
						this.goodsIds.splice(index, 1);
					}
				}
				this.list.data[index].xuanze = !this.list.data[index].xuanze
			},

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

			// 设置默认列表显示方式
			setShowView() {
				this.showView = uni.getStorageSync(showViewKey) || false
			},

			/**
			 * 获取商品列表
			 * @param {number} pageNo 页码
			 */
			getGoodsList(pageNo = 1) {
				const app = this
				const param = {
					status: this.sortType == 'all' ? 0 : this.sortType == 'fbz' ? 10 : 20,
					page: pageNo
				}
				return new Promise((resolve, reject) => {
					GoodsApi.shoplistb(param)
						.then(result => {
							// 合并新数据
							const newList = result.data.list
							app.list.data = getMoreListData(newList, app.list, pageNo)
							resolve(newList)
						})
						.catch(reject)
				})
			},

			// 切换排序方式
			handleSortType(newSortType) {
				const app = this
				app.sortType = newSortType
				// 刷新列表数据
				app.list = getEmptyPaginateObj()
				app.mescroll.resetUpScroll()

			},

			// 切换列表显示方式
			handleShowView() {
				const app = this
				app.showView = !app.showView
				uni.setStorageSync(showViewKey, app.showView)
			},

			// 跳转商品详情页
			onTargetDetail(goodsId) {
				this.$navTo('pages/user/shop/goodsdetail', {
					goodsId
				})
			},

			/**
			 * 商品搜索
			 */
			handleSearch() {
				const searchPageUrl = 'pages/search/index'
				// 判断来源页面
				let pages = getCurrentPages()
				if (pages.length > 1 &&
					pages[pages.length - 2].route === searchPageUrl) {
					uni.navigateBack()
					return
				}
				// 跳转到商品搜索页
				this.$navTo(searchPageUrl)
			}

		},


		/**
		 * 设置分享内容
		 */
		onShareAppMessage() {
			// 构建分享参数
			return {
				title: "全部分类",
				path: "/pages/category/index?" + this.$getShareUrlParams()
			}
		},

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

	}
</script>
<style>
	page {
		background-color: #f6f6f6;
	}
</style>

<style lang="scss" scoped>
	
	// 页面头部
	.header {
		display: flex;
		align-items: center;
		background-color: #fff;

		// 搜索框
		.search {
			flex: 1;
		}

		// 切换显示方式
		.show-view {
			width: 60rpx;
			height: 60rpx;
			font-size: 36rpx;
			color: #505050;
			display: flex;
			align-items: center;
		}
	}

	// 排序组件
	.store-sort {
		position: sticky;
		top: var(--window-top);
		display: flex;
		padding: 20rpx 0;
		font-size: 28rpx;
		background: #fff;
		color: #000;
		z-index: 99;

		.sort-item {
			flex-basis: 33.3333%;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 50rpx;

			&.active {
				color: $main-bg;
			}
		}

		.sort-item-price .price-arrow {
			margin-left: 20rpx;
			font-size: 24rpx;
			color: #000;

			.icon {
				&.active {
					color: $main-bg;
				}

				&.up {
					margin-bottom: -16rpx;
				}

				&.down {
					margin-top: -16rpx;
				}
			}

		}

	}

	// 商品列表
	.goods-list {
		padding: 4rpx;
		box-sizing: border-box;
	}

	// 单列显示
	.goods-list.column-1 {
		.goods-item {
			width: 100%;
			height: 280rpx;
			margin-bottom: 12rpx;
			padding: 20rpx;
			box-sizing: border-box;
			background: #fff;
			line-height: 1.6;

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

		.goods-item_left {
			display: flex;
			width: 300rpx;
			background: #fff;
			align-items: center;

			.image {
				display: block;
				width: 240rpx;
				height: 240rpx;
			}
		}

		.goods-item_right {
			position: relative;
			flex: 1;

			.goods-name {
				margin-top: 10rpx;
				min-height: 68rpx;
				line-height: 1.3;
				white-space: normal;
				color: #484848;
				font-size: 26rpx;
			}
		}

		.goods-item_desc {
			margin-top: 8rpx;
		}

		.desc-selling_point {
			width: 400rpx;
			font-size: 24rpx;
			color: #e49a3d;
		}

		.desc-goods_sales {
			color: #999;
			font-size: 24rpx;
		}

		.desc_footer {
			font-size: 24rpx;

			.price_x {
				margin-right: 16rpx;
				color: $main-bg;
				font-size: 30rpx;
			}

			.price_y {
				text-decoration: line-through;
			}
		}
	}

	// 平铺显示
	.goods-list.column-2 {
		.goods-item {
			width: 50%;
		}
	}

	.goods-item {
		float: left;
		box-sizing: border-box;
		padding: 6rpx;

		.goods-image {
			position: relative;
			width: 100%;
			height: 0;
			padding-bottom: 100%;
			overflow: hidden;
			background: #fff;

			&:after {
				content: '';
				display: block;
				margin-top: 100%;
			}

			.image {
				position: absolute;
				width: 100%;
				height: 100%;
				top: 0;
				left: 0;
				-o-object-fit: cover;
				object-fit: cover;
			}
		}

		.detail {
			padding: 8rpx;
			background: #fff;

			.goods-name {
				min-height: 68rpx;
				line-height: 1.3;
				white-space: normal;
				color: #484848;
				font-size: 26rpx;
				margin-bottom: 4rpx;
			}

			.detail-price {
				.goods-price {
					margin-right: 8rpx;
				}

				.line-price {
					text-decoration: line-through;
				}
			}
		}
	}
</style>