<template>
	<view class="web_box">
		<!-- <u-navbar height="44" leftText="职称申报" @leftClick="leftClick" :autoBack="true" bgColor="#fff">
		</u-navbar> -->
		<view class="search_top hflex acenter">
			<u--input placeholder="寻找课程" border="none" prefixIcon="search" v-model="value" @change="change" @confirm="search"></u--input>
			<view @click="cancel">取消</view>
		</view>
		<view class="content" v-if="!result">
			<view class="hflex acenter jbetween">
				<view class="leftText">搜索记录</view>
				<view class="hflex acenter" @click="detele">
					<u-icon name="trash" color="#999999" size="19"></u-icon>
					<view class="rightText">删除记录</view>
				</view>
			</view>
			<view class="hflex acenter fwrap list">
				<block v-for="(item,index) in history" :key="index">
					<view class="item">
						<u--image :src="item.img" width="322rpx" height="182rpx" radius="8"></u--image>
						<view class="text_style1">{{item.name}}</view>
					</view>
				</block>
			</view>
		</view>
		<view class="content" v-else-if="product.length>0">
			<block v-for="(item,index) in product" :key="index">
				<view class="item2 hflex" @tap="detail(index)">
					<u--image :src="item.img" width="322rpx" height="182rpx" radius="8"></u--image>
					<view class="item_right">
						<view class="title">
							{{item.name}}
						</view>
						<view class="text_style2">
							总学时：{{item.period}}
						</view>
						<view class="text_style2">
							讲师：{{item.lecturer}}
						</view>
						<view class="hflex acenter jbetween">
							<view class="price">￥{{item.price}}</view>
							<u-icon v-if="item.is_collection == 1" name="star-fill" color="#FA6400" size="28" @tap.stop="collect(index)"></u-icon>
							<u-icon v-else name="star" color="#FA6400" size="28" @tap.stop="collect(index)"></u-icon>
						</view>
					</view>
				</view>
			</block>
		</view>
		<view class="content vflex acenter jcenter" v-else-if="product.length==0">
			<image src="../../../static/images/common/search_none.png" class="img_none"></image>
			<view class="text_none">
				搜索内容为空
			</view>
		</view>
	</view>
</template>

<script>
	import $api from '@/static/js/api.js'
	export default {
		data() {
			return {
				value: '',
				history: [],
				result: false,
				product: [],
				page: 1,
				limit: 10,
				total: 0
			}
		},
		onLoad() {
			this.getHistory()
		},
		methods: {
			leftClick() {
			},
			// 收藏
			// collect(index) {
			// 	this.product[index].collect = !this.product[index].collect
			// },
			change(e) {
				console.log(e)
				this.value = e
			},
			// 开始搜索
			search() {
				var that = this
				that.result = true
				$api.req({
					url: '/api/Index/courselist',
					data: {
						page: that.page,
						limit: that.limit,
						keywords: that.value
					}
				}, function(res) {
					if(res.code == 1) {
						if(that.page == 1) {
							that.product = res.data.list
							that.total = res.data.count
						} else {
							that.total = res.data.count
							that.product = that.product.concat(res.data.list)
						}
						
					}
				})
			},
			// 获得搜索记录
			getHistory() {
				var that = this
				uni.getStorage({
					key: 'history',
					success: function(res) {
						console.log("缓存",res)
						that.history.push(res.data) 
						console.log("缓存记录",that.history)
					}
				})
			},
			// 取消
			cancel() {
				this.result = false
				this.value = ''
			},
			// 删除记录
			detele() {
				uni.removeStorage({
					key: 'history',
					success: function (res) {
					},
					
				});
				this.history = []
			},
			// 点击进入课程详情
			detail(index) {
				var that = this
				$api.jump('/pages/index/course/detail?id=' + that.product[index].id)
				var data = {
					id: that.product[index].id,
					img: that.product[index].img,
					name: that.product[index].name
				}
				uni.setStorage({
					data: data,
					key: 'history',
					success() {
						that.getHistory()
					},
				})
			},
			// 触底事件
			onReachBottom() {
				console.log("到底了");
				console.log(this.page );
				console.log(this.limit);
				console.log(Number(this.total));
				if (Number(this.page) * Number(this.limit) >= Number(this.total)) {
					$api.info("没有更多了")
				} else {
					this.page++
					this.change()
				}
			},
			// 收藏
			collect(index) {
				var that = this
				if (that.product[index].is_collection == 0) {
					that.product[index].is_collection = 1
				} else {
					that.product[index].is_collection = 0
				}
				$api.req({
					url: '/api/Index/collectionCancelCourse',
					data: {
						id: that.product[index].id,
						type: that.product[index].is_collection
					}
				}, function(res) {
					console.log("收藏=",res);
					if (res.code == 1) {
						$api.info(res.msg)
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.web_box::v-deep {
		padding: 0 30rpx;
		background-color: #fff;
		
		.u-navbar {
			width: 100%;
			box-sizing: border-box;
			padding: 36px 16px 0 0;
		}
		.search_top {
			width: 100%;
			box-sizing: border-box;
			padding: 67rpx 0;
		}
		.u-input {
			padding: 14rpx 18rpx !important;
			background: #F7F7F7;
			border-radius: 16rpx;
			margin-right: 20rpx;
		}
		.leftText {
			font-size: 16px;
			color: #999999;
			line-height: 22px;
		}
		.rightText {
			font-weight: 400;
			color: #999999;
			line-height: 20px;
			font-size: 14px;
		}
		.list {
			width: 100%;
			padding-top: 24rpx;
		}
		.item {
			width: 167px;
			height: 97px;
			background: #FFFFFF;
			box-shadow: 0px 2px 6px 0px rgba(213,213,213,0.5);
			border-radius: 12px;
			box-sizing: border-box;
			padding: 6rpx;
			margin: 0 14rpx 20rpx 0;
			position: relative;
		}
		.item:nth-child(2n) {
			margin-right: 0;
		}
		.text_style1 {
			position: absolute;
			bottom: 10rpx;
			left: 22rpx;
			font-weight: 600;
			color: #FFFFFF;
			font-size: 26rpx;
		}
		.item2 {
			width: 100%;
			height: 204rpx;
			background: #FFFFFF;
			box-shadow: 0px 2px 6px 0px rgba(213,213,213,0.5);
			border-radius: 12px;
			box-sizing: border-box;
			padding: 6rpx;
			margin-bottom: 20rpx;
		}
		.item_right {
			width: calc(100% - 322rpx);
			padding-left: 18rpx;
		}
		.title {
			padding-bottom: 6rpx;
			font-size: 13px;
			font-weight: 600;
			color: #333333;
			line-height: 18px;
		}
		.text_style2 {
			font-size: 10px;
			font-weight: 400;
			color: #999999;
			line-height: 14px;
			padding-bottom: 8rpx;
		}
		.price {
			font-size: 16px;
			font-weight: 600;
			color: #FA6400;
			line-height: 22px;
		}
		.text_none {
			font-size: 32rpx;
			color: #333333;
			margin-top: 44rpx;
		}
		.img_none {
			margin-top: 300rpx;
			width: 560rpx;
			height: 280rpx;
		}
	}
</style>