<template>
	<view class="content">
		<view class="search-box">
			<!-- mSearch组件 如果使用原样式，删除组件元素-->
			<!-- <mSearch class="mSearch-input-box" :mode="2" button="inside" :placeholder="defaultKeyword"
				@search="doSearch(false)" @input="inputChange" @confirm="doSearch(false)" v-model="keyword"></mSearch> -->
			<!-- 原样式 如果使用原样式，恢复下方注销代码 -->

			<!-- <view class="input-box">
				<input type="text" :adjust-position="true" :placeholder="defaultKeyword" @input="inputChange" v-model="keyword" @confirm="doSearch(false)"
				 placeholder-class="placeholder-class" confirm-type="search">
			</view>
			<view class="search-btn" @tap="doSearch(false)">搜索</view> -->
			<u-search style="width: 100%;" placeholder="输入搜索内容" :focus="true" v-model="keyword" :show-action="true"
				:animation="true" shape="square" bg-color="#F7F7F7" color="#1A1A1A" action-text="取消" @custom="goBack()"
				@search="doSearch(false)"></u-search>
			<!-- 原样式 end -->
		</view>
		<view class="search-keyword">
			<scroll-view class="keyword-list-box" v-show="isShowKeywordList" scroll-y>
				<view class="margin-lr-sm padding-bottom">
					<view class="flex justify-between margin-bottom padding-sm radius"
						v-for="(item, index) in keywordList" :key="index" @click="goOrder(item)">
						<!-- <image :src="item.headImg?item.headImg: '../../../static/logo.png'"
							style="width: 220rpx;height: 200rpx;border-radius: 10rpx;"></image> -->
							<view style="width: 300upx;height: 260upx;border-radius: 10rpx;">
								<image :src="item.headImg?item.headImg: '../../static/logo.png'"
									style="width: 300upx;height: 260upx;" mode="aspectFit"></image>
							</view>
						<view class=" margin-left-sm text-white flex flex-direction justify-between" style="width: 50%;">
							<view class="flex">
								<view v-if="item.authentication == 2">
									<image src="../../../static/images/qi.png" style="width: 35rpx;height: 35rpx;"></image>
							
								</view>
								<view class="" v-if="item.authentication == 1">
									<image src="../../../static/images/geren.png" style="width: 35rpx;height: 35rpx;"></image>
								</view>
								<view class="margin-right-xs text-df margin-left-xs"
									style="margin-top: -2px;display: inline-block;width: 320upx; overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">
									{{item.myLevel}}
								</view>
							</view>
							<view style="color: #999999;"  v-if="item.authentication == 1">
								<text v-if="item.sex == 1">女</text>
								<text v-if="item.sex == 0">男</text>
								/<text>{{age}}岁</text>
								/<text>{{item.orderLevel}}</text>
								/<text >{{item.region}}</text>
									
							</view>
							<view style="color: #999999;"  v-if="item.authentication == 2">
								<text>{{item.name}}</text>
								
							</view>
							<view class="flex flex-wrap">
								<text class=" box" v-for="(item,index) in item.gameName"
									:key="index" style="margin-right: 5rpx;">{{item}}</text>
							</view>
							<!-- <view class="flex" style="align-items: center;font-size: 24rpx;" v-if="item.salesNum">
								<view style="color: #999999;background: #F2F2F2; padding: 5rpx 10rpx;">已售{{item.salesNum}}
								</view>
							</view> -->
							<view style="width: 100%;display: flex;justify-content: space-between;align-items: center;">
								<view style="color:#FF1200;font-size: 31rpx;">
									￥{{item.oldMoney}}/月
								</view>
								<!-- <view style="background: #005DFF;color: #ffffff;padding: 15rpx 25rpx;border-radius: 8rpx;">
									预约服务
								</view> -->
							</view>
						</view>
					</view>
				</view>
				<!-- <view v-if="keywordList.length == 0">
					暂无数据
				</view> -->
				<empty v-if="keywordList.length == 0"></empty>
			</scroll-view>
			<scroll-view class="keyword-box" v-show="!isShowKeywordList" scroll-y>
				<view class="keyword-block">
					<view class="keyword-list-header">
						<view>热门搜索</view>
						<view>
							<image @tap="hotToggle" :src="'/static/images/index/attention'+forbid+'.png'"></image>
						</view>
					</view>
					<view class="keyword" v-if="forbid==''">
						<view v-for="(keyword,index) in hotKeywordList" @tap="doSearch(keyword)" :key="index">
							{{keyword}}
						</view>
					</view>
					<view class="hide-hot-tis" v-else>
						<view>当前搜热已隐藏</view>
					</view>
				</view>
				<view class="keyword-block" v-if="oldKeywordList.length>0">
					<view class="keyword-list-header">
						<view>历史记录</view>
						<view>
							<image @tap="oldDelete" src="/static/images/index/delete.png"></image>
						</view>
					</view>
					<view class="keyword">
						<view v-for="(keyword,index) in oldKeywordList" @tap="doSearch(keyword)" :key="index">
							{{keyword}}
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import empty from '@/components/empty.vue'
	export default {
		components: {
			empty
		},
		data() {
			return {
				defaultKeyword: "",
				keyword: "",
				oldKeywordList: [], //历史记录
				hotKeywordList: [], //热搜
				keywordList: [], //搜索列表
				forbid: '',
				isShowKeywordList: false,
				limit: 10,
				page: 1,
				userId: '',
				isVip: false,
				age: 0,
				birthDate: ''
			}
		},
		onLoad() {
			this.userId = uni.getStorageSync('userId')
			if (this.userId) {
				this.getSearchList()
			}
			this.isVip = uni.getStorageSync('isVIP')

		},
		methods: {
			//换算年龄
			getAge(e) {
				console.log('出生日期：' + e)
				this.birthDate = e
				//创建系统日期
				var today = new Date();
				//把出生日期转换成日期
				this.birthDate = new Date(this.birthDate);
				//分别获取到年份后相减
				this.age = today.getFullYear() - this.birthDate.getFullYear();
				// console.log(this.age+'当前年龄')
			},
			// 获取搜索历史
			getSearchList() {
				this.$Request.get("/app/Search/selectAppSearchNum").then(res => {
					console.log(res)
					if (res.code == 0) {
						this.oldKeywordList = res.data.userSearchName
						// for (let i = 0; i < this.oldKeywordList.length; i++) {
						// 	this.oldKeywordList[i].gameName = this.oldKeywordList[i].gameName.split(",");
						// }
						this.hotKeywordList = res.data.allSerchName


					}
				});
			},
			//清除历史搜索
			oldDelete() {
				uni.showModal({
					content: '确定清除历史搜索记录？',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定');
							this.$Request.get("/app/Search/deleteAppSearch").then(res => {
								if (res.code == 0) {
									this.getSearchList()
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//执行搜索
			doSearch(keyword) {
				this.keyword = keyword === false ? this.keyword : keyword;
				this.isShowKeywordList = true;
				if (!this.keyword) {
					uni.showToast({
						title: '请输入内容',
						icon: 'none',
						duration: 1000
					})
					return
				}

				let data = {
					like: this.keyword,
					limit: this.limit,
					page: this.page,
				}
				if (this.userId) {
					this.$Request.get("/app/orderTaking/queryTaking", data).then(res => {
						if (res.code == 0) {
							if (this.page == 1) this.keywordList = [];
							this.keywordList = [...this.keywordList, ...res.data.list]
							for (let i = 0; i < this.keywordList.length; i++) {
								this.keywordList[i].gameName = this.keywordList[i].gameName.split(",");
								this.getAge(this.keywordList[i].birthdate)
								// this.keywordList[i].region = this.keywordList[i].region.split(",");
								if(this.keywordList[i].region){
									let region=this.keywordList[i].region.split(",");
									this.keywordList[i].region = region[1]
								}else{
									this.keywordList[i].region='不限地区'
								}
							}
						}
					});
				} else {
					this.$Request.get("/app/orderTaking/queryTakings", data).then(res => {
						if (res.code == 0) {
							if (this.page == 1) this.keywordList = [];
							this.keywordList = [...this.keywordList, ...res.data.list]
							for (let i = 0; i < this.keywordList.length; i++) {
								this.keywordList[i].gameName = this.keywordList[i].gameName.split(",");
								this.getAge(this.keywordList[i].birthdate)
								// this.keywordList[i].region = this.keywordList[i].region.split(",");
								if(this.keywordList[i].region){
									let region=this.keywordList[i].region.split(",");
									this.keywordList[i].region = region[1]
								}else{
									this.keywordList[i].region='不限地区'
								}
							}
						}
					});
				}
			},
			// 点击取消返回首页
			goBack() {
				uni.navigateBack()
			},
			//热门搜索开关
			hotToggle() {
				this.forbid = this.forbid ? '' : '_forbid';
			},
			// 跳转订单
			goOrder(e) {
				if (this.userId) {
					uni.navigateTo({
						url: '/pages/index/game/order?id=' + e.id
					});
				} else {
					uni.navigateTo({
						url: '/pages/public/login'
					});
				}
			},

		},
		onReachBottom: function() {
			this.page = this.page + 1;
			this.doSearch(false);
		},
		onPullDownRefresh: function() {
			this.page = 1;
			this.doSearch(false);
		},
	}
</script>
<style>
	page {
		background-color: #F7F7F7;
	}

	.bg {
		background-color: #FFFFFF;
	}

	.search-box {
		width: 100%;
		/* background-color: rgb(242, 242, 242); */
		padding: 15upx 2.5%;
		display: flex;
		justify-content: space-between;
		position: sticky;
		top: 0;
		background-color: #FFFFFF;
	}

	.search-box .mSearch-input-box {
		width: 100%;
	}

	.search-box .input-box {
		width: 85%;
		flex-shrink: 1;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.search-box .search-btn {
		width: 15%;
		margin: 0 0 0 2%;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-shrink: 0;
		font-size: 28upx;
		color: #fff;
		background: linear-gradient(to right, #ff9801, #ff570a);
		border-radius: 60upx;
	}

	.search-box .input-box>input {
		width: 100%;
		height: 60upx;
		font-size: 32upx;
		border: 0;
		border-radius: 60upx;
		-webkit-appearance: none;
		-moz-appearance: none;
		appearance: none;
		padding: 0 3%;
		margin: 0;
		background-color: #ffffff;
	}

	.placeholder-class {
		color: #9e9e9e;
	}

	.search-keyword {
		width: 100%;
		background-color: #FFFFFF;
	}

	.keyword-list-box {
		height: calc(100vh - 110upx);
		padding-top: 10upx;
		/* border-radius: 20upx 20upx 0 0; */
		/* background-color: #fff; */
	}

	.keyword-entry-tap {
		background-color: #eee;
	}

	.keyword-entry {
		width: 94%;
		height: 80upx;
		margin: 0 3%;
		font-size: 30upx;
		color: #333;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: solid 1upx #e7e7e7;
	}

	.keyword-entry image {
		width: 60upx;
		height: 60upx;
	}

	.keyword-entry .keyword-text,
	.keyword-entry .keyword-img {
		height: 80upx;
		display: flex;
		align-items: center;
	}

	.keyword-entry .keyword-text {
		width: 90%;
	}

	.keyword-entry .keyword-img {
		width: 10%;
		justify-content: center;
	}

	.keyword-box {
		height: calc(100vh - 110upx);
		/* border-radius: 20upx 20upx 0 0; */
		background-color: #F7F7F7;
	}

	.keyword-box .keyword-block {
		padding: 10upx 0;
	}

	.keyword-box .keyword-block .keyword-list-header {
		width: 94%;
		padding: 10upx 3%;
		font-size: 27upx;
		font-weight: 700;
		/* color: #FFFFFF; */
		display: flex;
		justify-content: space-between;
	}

	.keyword-box .keyword-block .keyword-list-header image {
		width: 40upx;
		height: 40upx;
	}

	.keyword-box .keyword-block .keyword {
		width: 94%;
		padding: 3px 3%;
		display: flex;
		flex-flow: wrap;
		justify-content: flex-start;
	}

	.keyword-box .keyword-block .hide-hot-tis {
		display: flex;
		justify-content: center;
		font-size: 28upx;
		color: #FFFFFF;
	}

	.keyword-box .keyword-block .keyword>view {
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 10upx;
		padding: 0 20upx;
		margin: 10upx 20upx 10upx 0;
		height: 60upx;
		font-size: 28upx;
		background-color: #FFFFFF;
		color: #343546;
	}
	.box {
		border: 1rpx solid #005dff;
		color: #005DFF;
		padding: 5rpx 15rpx;
		font-size: 22rpx;
		letter-spacing: 2rpx;
		border-radius: 8rpx;
		margin-bottom: 2rpx;
	}
</style>
