<template>
	<view class="main pt0">
		<view class="search">
			<u-search placeholder="搜索企业" v-model="param.name" bgColor="white" @search="refresh()" @clear="(param.name = ''), refresh()" :showAction="false"></u-search>
		</view>
		<view class="tab">
			<u-tabs :list="tab" @click="tabClick"></u-tabs>
			<view class="filters" @click="go('/pages/job/position/city')">
				<text class="icon">&#xe62e;</text>
				<text>{{ param.cityName || '南宁市' }}</text>
			</view>
		</view>
		<view class="list">
			<view class="item" v-for="(item, index) in list" :key="index" @click="go('/pages/job/enterprise/detail?id=' + item.id)">
				<view class="title omit">{{ item.name }}</view>
				<view class="desc">{{ item.regionName }}</view>
				<view class="distance" v-if="item.distance">距离你{{ item.distance }}km</view>
			</view>
			<view class="loading" v-if="loadMore"><u-loadmore :status="loadMore ? 'loading' : 'nomore'" /></view>
			<u-empty v-if="!loadMore && list.length == 0"></u-empty>
		</view>
	</view>
</template>
<script>
export default {
	data() {
		return {
			tab: [
				{ name: '最新', orderBy: 'id' },
				{ name: '附近', orderBy: 'distance' }
			],
			list: [],
			param: { pageNum: 1, pageSize: 10, orderBy: 'id' },
			loadMore: true,
			show: false
		};
	},
	onLoad(e) {
		if (this.getLocation()) {
			this.param.latitude = this.getLocation().latitude;
			this.param.longitude = this.getLocation().longitude;
		}
		uni.$on('select_city', (res) => {
			this.param.cityName = res.title;
			this.param.regionId = res.id;
			this.refresh();
		});
		this.getData();
	},
	methods: {
		getData() {
			this.http.request({
				url: '/app/enterprise/list',
				data: this.param,
				loading: 'false',
				success: (res) => {
					this.loadMore = res.data.pages > this.param.pageNum ? true : false;
					this.list.push(...res.data.rows);
				}
			});
		},
		tabClick(e) {
			this.param.orderBy = e.orderBy;
			this.refresh();
		},
		go(url) {
			uni.navigateTo({ url: url });
		},
		//刷新数据
		refresh() {
			this.loadMore = true;
			this.param.pageNum = 1;
			this.list = [];
			this.getData();
		}
	},
	//下拉刷新
	onPullDownRefresh() {
		setTimeout(() => {
			this.refresh();
			uni.stopPullDownRefresh();
		}, 1000);
	},
	//上拉加载
	onReachBottom() {
		if (this.loadMore) {
			this.param.pageNum++;
			this.getData();
		}
	}
};
</script>

<style lang="scss">
.item {
	background-color: white;
	border-radius: 5px;
	padding: 12px;
	margin-bottom: 10px;
	.title {
		font-weight: bold;
	}
	.desc {
		font-size: 14px;
		color: $font-c;
		padding-top: 7px;
	}
	.distance {
		float: right;
		margin-top: -18px;
		font-size: 14px;
		color: $font-c;
	}
}
</style>
