<template>
	<view>
		<view v-if="isLogin && !is_city">
			<Fail />
		</view>
		<view v-else>
			<view class="search_box">
				<uni-icons type="back" size="30" :style="{'position':'relative','top': '-2px'}" @click.native="goBack()"></uni-icons>
				<i @click="search"></i>
				<input type="text" placeholder="我是要找的商品名" v-model="keyword" @confirm="search">
				<view class="category_list">
					<text :class="{act: tabIndex === -1}" @click="changeTabs(-1, firsr_cate_id)">全部</text>
					<text v-for="(item, index) in cate_list" :key="index" :class="{act: tabIndex === index}" @click="changeTabs(index, item.cate_id)">{{item.cate_name}}</text>
				</view>
			</view>
			<view class="goods_box" v-if="goods_list instanceof Array">
				<view class="goods_item" v-for="(item, index) in goods_list" :key="index">
					<image :src="item.default_image || $noGoodsImg" @click="$jumpPhpPage(item.goods_id)"></image>
					<view class="goods_item_info">
						<p>{{item.goods_name}}</p>
						<p>{{item.goods_subname}}</p>
						<view class="goods_tag">
							<text>限时免息</text>
							<!-- <text>9个月账期</text> -->
						</view>
						<p class="price">
							<text>{{isLogin ? '￥' : ''}}</text>{{isLogin ? item.price : '登录显示价格'}}
						</p>
					</view>
					<i class="cart_icon" @click="addCart(item.spec_id, 1)"></i>
				</view>
				<view class="pageBox">
					<uni-pagination ref="pagination" :total="page['total']" current="1" show-icon="true" @change="changePage"></uni-pagination>
				</view>
			</view>
			<view class="nodata" v-else>暂无此类商品</view>
			<BottomBar />
		</view>
	</view>
</template>

<script>
	import uniIcons from "@/components/uni-icons/uni-icons.vue";
	import uniPagination from '@/components/uni-pagination/uni-pagination.vue';
	import BottomBar from "@/components/BottomBar/BottomBar.vue";
	import Fail from '../fail.vue';
	import { php, go } from '../../common/host.js';
	export default {
		data() {
			return {
				goods_list: [],
				page: {
					total: 0,
					current: 1
				},
				cate_list: [],
				firsr_cate_id: '',
				cate_id_1: '',
				tabIndex: -1,
				keyword: '',
				isLogin: 0,
				is_city: 0
			}
		},
		onLoad(option) {
			uni.showLoading({
			    title: '加载中'
			});
			this.getData({
				current: 1,
				page_size: 10
			});
			// #ifdef H5
			//默认请求微信分享
			if (this.$wechat && this.$wechat.isWechat()) {
				this.$wechat.share({titie: this.$getNavigationBarTitle()});
			}
			this.postData();
			// #endif
		},
		onPullDownRefresh() {
			if(this.keyword) {
				this.search(this.keyword, uni.stopPullDownRefresh);
				return
			} else if (this.cate_id_1) {
				this.changeTabs(this.tabIndex, this.cate_id_1, uni.stopPullDownRefresh);
			} else {
				this.changeTabs(this.tabIndex, this.firsr_cate_id, uni.stopPullDownRefresh);
			}
		},
		components: { 
			uniIcons,
			uniPagination,
			BottomBar,
			Fail
		},
		methods: {
			goBack() {
				this.$backup();
			},
			// 向小程序发送数据
			postData(){
				jWeixin.miniProgram.getEnv(function (res) {
					//获取当前环境
					if(res.miniprogram){ 
						// this.$wechat.postData('谛宝多多商城-首页');
						const postData = {
							link: window.location.href,
							title: '谛宝多多商城'
						}
						jWeixin.miniProgram.postMessage({ data: JSON.stringify(postData) });
					}
				});
			},
			getData(params) {
				uni.request({
					url: '/uni/api/credit_goods/get_credit_all_goods',
					method: 'POST',
					data: params,
					dataType: 'json',
					success: (res) => {
						const { credit_cates_list, credit_goods_list } = res.data.data,
							  { page } = res.data;
						this.goods_list = credit_goods_list;
						this.cate_list = credit_cates_list;
						this.page = page;
						this.isLogin  = res.data.login_flg;
						this.is_city = res.data.city_flg;
						uni.hideLoading();
					}
				});
			},
			changeTabs(index, cate_id, cb) {
				if(index != -1) {
					var params = {
						cate_id_1: cate_id+''
					};
					this.cate_id_1 = cate_id+'';
				} else {
					var params = {}
					this.cate_id_1 = '';
				}
				this.tabIndex = index;
				uni.request({
					url: '/uni/api/credit_goods/get_credit_all_goods',
					method: 'POST',
					data: {
						...params,
						goods_name: this.keyword,
						current: 1,
						page_size: 10
					},
					dataType: 'json',
					success: (res) => {
						const { credit_goods_list } = res.data.data,
							  { page } = res.data;
						this.goods_list = credit_goods_list;
						this.page = page;
						if(credit_goods_list instanceof Array) {
							setTimeout(() => {
								this.$refs.pagination.parentsChangeCurrent(1);
							}, 0);
						}
						!!cb && cb();
					}
				});
			},
			changePage(data) {
				const { current } = data;
				uni.request({
					url: '/uni/api/credit_goods/get_credit_all_goods',
					method: 'POST',
					data: {
						cate_id_1: this.cate_id_1,
						goods_name: this.keywords,
						current: current,
						page_size: 10
					},
					dataType: 'json',
					success: (res) => {
						const { credit_goods_list } = res.data.data;
						this.goods_list = credit_goods_list;
					}
				});
			},
			search(keyword, cb) {
				uni.request({
					url: '/uni/api/credit_goods/get_credit_all_goods',
					method: 'POST',
					data: {
						goods_name: this.keyword,
						current: 1,
						page_size: 10
					},
					dataType: 'json',
					success: (res) => {
						const { credit_goods_list } = res.data.data,
							  { page } = res.data;
						this.goods_list = credit_goods_list;
						this.page = page;
						!!cb && cb();
						this.tabIndex = -1;
						if(credit_goods_list instanceof Array) {
							setTimeout(() => {
								this.$refs.pagination.parentsChangeCurrent(1);
							}, 0);
						}
					}
				});
			},
			addCart(spec_id, quantity) {
				this.$addCart(spec_id, quantity, this.isLogin);
			},
			jumpPhpPage(url) {
				let link = url ? `${php}${url}` : php;
				window.location.href =  link;
			}
		}
	}
</script>

<style lang="less" scoped>
	.search_box {
		background: url(../../static/category/2641581405726_.pic.png) no-repeat;
		background-position: 100% 100%;
		position: fixed;
		width: 100%;
		left: 0;
		top: 0;
		z-index: 10;
		i {
			display: inline-block;
			width: 60rpx;
			height: 60rpx;
			background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADgAAAA4CAMAAACfWMssAAAAulBMVEUhISEiIiIjIyMsLCwtLS0vLy86OjpAQEBISEhMTExPT09SUlJaWlppaWl0dHR4eHh5eXmOjo6Tk5OYmJienp7AwMDJycnNxGbNxWjOxWjOxWrPxmvPxm7Qx3DQyHPRyXTRynbV1dXW1tbX0IrX0YvY0YvY0ozb1Zbb1pjc3Nzl5eXm4rvm4rzo6Ojp5cTp5sXq6urr6Mzr6M3s7Ozt6tLt69Xu69bv7+/w7tzx8fH19O/19e/39/X4+Phd4ycQAAABNklEQVRIx+3XyVKDQBCA4R8JEBJZBWTcIrjvUdS4zfu/lodErYhVA11lTukbFB80MNM9w/ZXHN1/6B7Bgh0/6X4xh5NbrSXw7E1L4M6V1hK496hF8OBFy6DQafQarhAWWZpmRV9YxQ4AOHHVAzahxXdYYdMV1h6AGyVJ5AJ4dTdYD4BgkWEVAIO6C2w8sPOf49wGr+kAQ7CXnlDbEJphZUG+fCoHqzLCGILf1wQQG6EDrbtX4JhgAW77fVwoDDCDqA0jyAwwhaQNE0j/C4pTFX8c8e+QDwDxkBMP8j+nlXUpm8gwnklKB8CoERSriyHgTwXlceoDwxtBQW5GgC9pAbMxbIh6x+umtbXujyuA4gXSRLokU/vCRaBSu9ciWJZlef4uhOXhnSRVpZRSp89CqNTJQ6/twycM+aPLaun/FQAAAABJRU5ErkJggg==) no-repeat;
			background-size: 100% 100%;
			margin-left: 20rpx;
			position: relative;
			top: 8rpx;
		}
		input {
			display: inline-block;
			vertical-align: middle;
			width: 590rpx;
			height: 60rpx;
			line-height: 60rpx;
			font-size: 28rpx;
			margin: 14rpx 0 40rpx 0;
			background: #F8F8F8;
			border-radius: 0 30rpx 30rpx 0;
		}
		.category_list {
			display: block;
			white-space: nowrap;
			overflow: scroll;
			width: 94.6%;
			margin-left: 2.7%;
			padding-bottom: 8rpx;
			text {
				font-size: 28rpx;
				line-height: 40rpx;
				margin-right: 68rpx;
			}
			text:last-child {
				margin-right: 0;
			}
			text.act {
				font-size: 32rpx;
				line-height: 44rpx;
				position: relative;
				font-weight: 500;
			}
			text.act:after {
				width: 52rpx;
				height: 4rpx;
				background: #212121;
				position: absolute;
				left: 50%;
				bottom: -8rpx;
				margin-left: -26rpx;
				content: '';
			}
		}
		.category_list::-webkit-scrollbar {
			display: none;
		}
	}
	.nodata {
		text-align: center;
		padding: 40rpx 0;
		margin-top: 174rpx;
	}
	.goods_box {
		padding-bottom: 118rpx;
		margin-top: 174rpx;
		.pageBox {
			text-align: center;
		}
		.goods_item {
			position: relative;
			width: 100%;
			box-sizing: border-box;
			padding: 32rpx 88rpx 32rpx 20rpx;
			background: #fff;
			display: flex;
			justify-content: space-between;
			.cart_icon {
				position: absolute;
				right: 88rpx;
				bottom: 32rpx;
				width: 56rpx;
				height: 56rpx;
				background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADgAAAA4CAAAAACN7WTCAAABfElEQVRIx2P4TyZgGNU4qnFUIxEafx87+eYvORqvVFRUbiTLxstTSktW/yHHj7/2FRa9ICtw/kzM2wa2/MTmzZs2gTAEbL3yi0Co7sye/A9IfcpGBVlZU/7i1/g4o/gryObzEJtgdm4uyliDX+PXgpQn2Ly0LaUNv8Z/PYl7sGlcnTiRQMpZETsXm8aJccsJaDwTXfULU9/3ouiDBDS+j4z5jKnxU3T4IwIaf+SFXMXUeCsk+jOh3NEfuBpT4+bAwl+ENK7x68bUOMOvl2B+vOmd8h1d7F+F9xKCGj/5e75DF/sc6X6MoMbfOS7oiv6/dnF5SbjomODYhV4O7HIM+ExY4z5b+1lr1gPBurVr16xZs3rVipnetpV/CWv8XmiFAbzuElPKfd4wpb8PCPr7+yeAwMRJK14Mt5L822ZQmJ6FhOOHTSDOub/EaFwVCwYfwJxFYHbcR2I03stJTk5OgqaC6xkgTjdRNv7//R0I/sEKaWTOaI08qnHoaQQAAyi97TBEkioAAAAASUVORK5CYII=) no-repeat;
				background-size: 100% 100%;
			}
			image {
				width: 240rpx;
				height: 240rpx;
				margin-right: 24rpx;
			}
			.goods_item_info {
				width: 402rpx;
				position: relative;
				p:nth-of-type(1) {
					width: 100%;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					font-size: 30rpx;
					line-height: 42rpx;
					margin-bottom: 10rpx;
				}
				p:nth-of-type(2) {
					width: 100%;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
					overflow: hidden;
					text-overflow: ellipsis;
					font-size: 22rpx;
					color: #7C7C7C;
				}
				.goods_tag {
					display: block;
					text {
						display: inline-block;
						vertical-align: middle;
						border: 1px solid #C7B64E;
						color: #C7B64E;
						height: 36rpx;
						line-height: 36rpx;
						padding: 0 12rpx;
						border-radius: 18rpx;
						font-size: 22rpx;
						margin-right: 8rpx;
					}
				}
				.price {
					font-size: 40rpx;
					line-height: 40rpx;
					margin-top: 26rpx;
					text {
						font-size: 28rpx;
					}
				}
			}
		}
	}
</style>
