<template>
	<view>
		<view>
			<view class="policy-box" v-for="item in datalist" :key="item.id"  @click="jumpPolicy(item.id)">
				<image :src="item.imgUrl" mode="aspectFill"></image>
				<text class="policy-title">{{item.title}}</text>
				<text class="policy-time">{{item.publishTime}}</text>
			</view>
		</view>
		<view class="notData" v-if="datalist.length == 0">
			暂时没有对应政策
		</view>
	</view>
</template>

<script>
	import { policy } from 'api/policy'
	import { search } from 'api/search'
	export default {
		props: {
				num: {
					type: Number
				},
				showModal: {
					// type: Object,
					default: ''
				},
				
		},

		data() {
			return {
				pagenum: 1,
				datalist: [],
				hasNextPage:true
			}
		},
		async created(options){
			const that = this
			if(this.showModal === ''){
				uni.showLoading({
					title: '正在获取数据...',
					icon:'loading',
				})
				try{
					const params = {
						"param": {
							"areaType": that.num,
							"cityId": that.$store.state.user.cityId,
							"pageNum": that.pagenum,
							"pageSize": 10
						},
					}
					const data = await policy(params)
					that.hasNextPage = data.hasNextPage
					that.datalist = that.datalist.concat(data.list)
					uni.hideLoading()
				}catch(err){
					//TODO handle the exception
					console.log(err)
					uni.hideLoading()
				}
			}else{
				uni.showLoading({
					title: '正在获取数据...',
					icon:'loading',
				})
				try{
					const params = {
						"param": {
								"areaId":  that.$store.state.user.cityId,
								"industrialCategories": that.showModal.industrialCategories,			//产业类别
								"name": that.showModal.name,				//项目名称
								"pageNum": that.$store.state.policy.pagenum,	//页数
								"pageSize": 10,
								"projectCategories": that.showModal.projectCategories					//项目类别
						},
					}
					const data = await search(params)
					that.datalist = that.datalist.concat(data.list)
					uni.hideLoading()
				}catch(err){
					//TODO handle the exception
					console.log(err)
					uni.hideLoading()
				}
			}
		},

		methods: {
			jumpPolicy(id){
				uni.navigateTo({
					url:`/pages/Policy/PolicyDetails?id=${id}&token=${this.$store.state.user.token}`
				})
				console.log(id)
			}
		},
		watch: {
			num(val){
					var that = this
					that.pagenum = 1
					that.datalist = []
					async function main () {
						uni.showLoading({
							title: '正在获取数据...',
							icon:'loading',
						})
						try{
							const params = {
								"param": {
									"areaType": val,
									"cityId": that.$store.state.user.cityId,
									"pageNum": that.pagenum,
									"pageSize": 10
								},
							}
							const data = await policy(params)
							that.datalist = that.datalist.concat(data.list)
							uni.hideLoading()
						}catch(err){
							//TODO handle the exception
							console.log(err)
							uni.hideLoading()
						}
					}
					main()
			},
			
			'$store.state.user.cityId'(val){
				var that = this
				that.pagenum = 1
				that.datalist = []
				async function main () {
					uni.showLoading({
						title: '正在获取数据...',
						icon:'loading',
					})
					try{
						const params = {
							"param": {
								"areaType": that.num,
								"cityId": that.$store.state.user.cityId,
								"pageNum": that.pagenum,
								"pageSize": 10
							},
						}
						const data = await policy(params)
						that.datalist = that.datalist.concat(data.list)
						uni.hideLoading()
					}catch(err){
						//TODO handle the exception
						console.log(err)
						uni.hideLoading()
					}
				}
				main()
			},
			
			
			'$store.state.policy.pagenum'(val){
					uni.showLoading({
						title: '正在获取数据...',
						icon:'loading',
					})
					var that = this
					if(that.hasNextPage === true){
						async function main () {
							try{
								const params = {
									"param": {
										"areaId": that.showModal.areaId,
										"industrialCategories": that.showModal.industrialCategories,			//产业类别
										"name": that.showModal.name,				//项目名称
										"pageNum": that.$store.state.policy.pagenum,
										"pageSize": 10,
										"projectCategories": that.showModal.projectCategories					//项目类别
									},
								}
								const data = await policy(params)
								uni.hideLoading()
								that.datalist = that.datalist.concat(data.list)
							}catch(err){
								//TODO handle the exception
								console.log(err)
								uni.hideLoading()
							}
						}
						main()
					}else{
						uni.showToast({
							title:'到底了',
							icon:'none'
						})
					}
			},
			
			showModal(obj){
				const that = this
				that.datalist = []
				async function main(){
					uni.showLoading({
						title: '正在获取数据...',
						icon:'loading',
					})
					try{
						const params = {
							"param": {
									"areaId": obj.areaId,
									"industrialCategories": obj.industrialCategories,			//产业类别
									"name": obj.name,				//项目名称
									"pageNum": that.$store.state.policy.pagenum,
									"pageSize": 10,
									"projectCategories": obj.projectCategories					//项目类别
							},
						}
						const data = await search(params)
						that.datalist = that.datalist.concat(data.list)
						uni.hideLoading()
					}catch(err){
						//TODO handle the exception
						console.log(err)
						uni.hideLoading()
					}
				}
				main()
			},
		}
	}
</script>

<style lang="scss" scoped>
	.notData{
		height: 300upx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: $gray;
	}
</style>
