<template>
	<view class="main">
		<!--搜索-->
		<view class="search">
			<view class="usearch">
				<u-search placeholder="搜索工作" :disabled="true" bgColor="white" :showAction="false" @click="go('/pages/job/search')"></u-search>
			</view>
			<view class="address omit" @click="go('/pages/job/position/city')">
				<text class="icon">&#xe62e;</text>
				<text>{{ param.cityName || '南宁市' }}</text>
			</view>
		</view>
		<!--轮播图-->
		<view class="banner">
			<u-swiper circular :radius="5" :indicator="true" keyName="pic" :list="bannerList" :height="160" class="uni-swiper" @click="click"></u-swiper>
		</view>
		<!--菜单-->
		<view class="menu">
			<view class="cd" @click="go('/pages/job/map')">
				<view class="out">
					<view class="int">
						<view class="icon" style="background-color: #03a9f4">&#xe647;</view>
						<view class="title">地图找工</view>
					</view>
				</view>
			</view>
			<view class="cd" @click="go('/pages/job/list?type=0')">
				<view class="out">
					<view class="int">
						<view class="icon" style="background-color: #4581fb">&#xe9d9;</view>
						<view class="title">全职</view>
					</view>
				</view>
			</view>
			<view class="cd" @click="go('/pages/job/list?type=1')">
				<view class="out">
					<view class="int">
						<view class="icon" style="background-color: #ff5722">&#xe62a;</view>
						<view class="title">兼职</view>
					</view>
				</view>
			</view>
			<view class="cd" @click="go('/pages/index/index')">
				<view class="out">
					<view class="int">
						<view class="icon" style="background-color: #03a9f4">&#xe725;</view>
						<view class="title">结算广场</view>
					</view>
				</view>
			</view>
		</view>
		<view class="tab">
			<u-tabs :list="tab" @click="tabClick"></u-tabs>
		</view>
		<!--职位列表-->
		<view class="list">
			<job :list="list" v-if="list.length > 0"></job>
			<u-empty v-else text="该城市暂无工作"></u-empty>
		</view>
	</view>
</template>
<script>
export default {
	data() {
		return {
			tab: [
				{ name: '最新', orderBy: 'id' },
				{ name: '附近', orderBy: 'distance' }
			],
			list: [],
			param: { pageNum: 1, pageSize: 20, type: 0 },
			bannerList: [],
			noticeList: []
		};
	},
	onLoad() {
		this.getData();
		this.initData();
		uni.$on('select_city', (res) => {
			this.param.cityName = res.title;
			this.param.regionId = res.id;
			this.getData();
		});
	},
	methods: {
		initData() {
			//首页数据
			this.http.request({
				url: '/app/home/index',
				success: (res) => {
					this.contract = res.data.data.contract;
					this.bannerList = res.data.data.bannerList;
					res.data.data.noticeList.forEach((item) => {
						this.noticeList.push(item.title);
					});
				}
			});
			//初始化所有地区
			this.http.request({
				url: '/app/common/column/city/all',
				success: (res) => {
					uni.setStorageSync('city_all', res.data.data);
				}
			});
			// #ifdef MP-WEIXIN
			//授权获取位置
			uni.authorize({
				scope: 'scope.userLocation',
				success: (s) => {
					uni.getLocation({
						type: 'wgs84',
						success: (res) => {
							uni.setStorageSync('location', res);
							this.param.latitude = res.latitude;
							this.param.longitude = res.longitude;
							this.getData();
						}
					});
				},
				fail(res) {
					uni.showModal({ title: '提示', content: '定位失败,请检查是否允许定位', showCancel: false });
				}
			});
			// #endif
		},
		getData() {
			this.http.request({
				url: '/app/position/list',
				data: this.param,
				success: (res) => {
					this.list = res.data.rows;
				}
			});
		},
		tabClick(e) {
			this.param.orderBy = e.orderBy;
			this.getData();
		},
		go(url) {
			uni.navigateTo({ url: url });
		}
	}
};
</script>

<style lang="scss">
.banner {
	margin-top: 15px;
	box-shadow: $box-shadow;
}
.menu {
	box-shadow: none;
}
</style>
