<template>
	<view class="main pt0">
		<view class="search">
			<view class="usearch">
				<u-search placeholder="搜索工作" :disabled="true" bgColor="white" :showAction="false" @click="go('/pages/job/search?type=' + param.type)"></u-search>
			</view>
			<view class="address omit" @click="go('/pages/job/position/city')">
				<text class="icon">&#xe64b;</text>
				<text>{{ param.cityName || '南宁市' }}</text>
			</view>
		</view>
		<view class="tab">
			<u-tabs :list="tab" :inactiveStyle="{ fontSize: '16px' }" :lineHeight="5" :activeStyle="{ color: '#3c9cff', fontSize: '16px' }" :current="current" @click="tabClick"></u-tabs>
			<view class="filters" @click="show = true" v-if="param.type == 0">
				<text class="icon">&#xe68c;</text>
				<text>筛选</text>
			</view>
		</view>
		<view class="list">
			<job :list="list"></job>
			<view class="loading" v-if="loadMore"><u-loadmore :status="loadMore ? 'loading' : 'nomore'" /></view>
			<u-empty v-if="!loadMore && list.length == 0"></u-empty>
		</view>
		<filters v-model="show" @confirm="confirm"></filters>
	</view>
</template>
<script>
export default {
	data() {
		return {
			tab: [
				{ name: '最新', orderBy: 'id', recommend: '' },
				{ name: '推荐', orderBy: 'id', recommend: 1 },
				{ name: '附近', orderBy: 'distance', recommend: '' }
			],
			current: 0,
			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;
		}
		this.param.type = e.type || 0;
		this.current = e.current || 0;
		this.param.orderBy = this.tab[this.current].orderBy;
		setTimeout(() => {
			uni.setNavigationBarTitle({ title: this.param.type == 0 ? '全职岗位' : '兼职岗位' });
		}, 300);
		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/position/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.param.recommend = e.recommend;
			this.refresh();
		},
		confirm(e) {
			Object.assign(this.param, e);
			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"></style>