<template>
	<view class="main">
		<view class="form_group">
			<view class="lable re">职位名称</view>
			<input placeholder="请输入" v-model="item.title" />
		</view>
		<view class="form_group">
			<view class="lable re">职位类型</view>
			<picker :disabled="true" @click="go('/pages/job/position/classification')">
				<input placeholder="请选择" v-model="item.positionName" :disabled="true" />
				<view class="icon more">&#xe8f2;</view>
			</picker>
		</view>
		<view class="form_group">
			<view class="lable re">职位描述</view>
			<leditor ref="editor" v-model="item.contents" placeholder="请输入职位描述"></leditor>
		</view>
		<view class="form_group">
			<view class="lable re">经验要求</view>
			<picker :range="dict.experience" @change="picker($event, 'experience')">
				<input placeholder="请选择" v-model="item.experience" :disabled="true" />
				<view class="icon more">&#xe8f2;</view>
			</picker>
		</view>
		<view v-if="item.type == 0">
			<view class="form_group">
				<view class="lable re">薪资范围</view>
				<multiSelector v-model="item.salary" :range="dict.salary" name="薪资" placeholder="请选择薪资范围"></multiSelector>
			</view>
		</view>
		<view v-else>
			<view class="form_group">
				<view class="lable re">{{ lable }}金额(元)</view>
				<input type="number" placeholder="请输入" v-model="item.salary" />
			</view>
			<view class="form_group">
				<view class="lable re">结算类型</view>
				<picker :range="dict.unit" @change="picker($event, 'unit')">
					<input placeholder="请选择" v-model="item.unit" :disabled="true" />
					<view class="icon more">&#xe8f2;</view>
				</picker>
			</view>
			<view class="form_group">
				<view class="lable re">{{ lable }}时间</view>
			</view>
			<view class="form_group" style="display: flex">
				<view class="start">
					<picker mode="date" :start="end" @change="picker($event, 'startDate')">
						<input placeholder="开始时间" v-model="item.startDate" :disabled="true" />
					</picker>
				</view>
				<view class="hor">至</view>
				<view class="start">
					<picker mode="date" :start="end" @change="picker($event, 'endDate')">
						<input placeholder="结束时间" v-model="item.endDate" :disabled="true" />
					</picker>
				</view>
			</view>
		</view>
		<view class="form_group">
			<view class="lable re">工作地点</view>
			<picker :disabled="true" @click="chooseLocation()" v-if="item.type == 0">
				<input placeholder="请选择" v-model="item.location" :disabled="true" />
				<view class="icon more">&#xe8f2;</view>
			</picker>
			<picker :range="dict.location" @change="picker($event, 'location')" v-else>
				<input placeholder="请选择" v-model="item.location" :disabled="true" />
				<view class="icon more">&#xe8f2;</view>
			</picker>
			<input placeholder="请输入楼层/单元室/门牌号" v-model="item.mph" v-if="item.address" />
			<view class="bz">请仔细确认位置点，否置会影响求职者导航</view>
		</view>
		<button class="btn" @click="save()">保存</button>
	</view>
</template>
<script>
export default {
	data() {
		return {
			lable: '职位',
			user: this.getUser(),
			item: {},
			end: this.util.getDate('day'),
			dict: {
				positionName: this.util.getData('positionName'),
				experience: this.util.getData('experience'),
				education: ['不限'].concat(this.util.getData('education')),
				salary: [['面议'], ['']],
				location: this.util.getData('address'),
				unit: this.util.getData('unit')
			},
			rule: [],
			userState: ''
		};
	},
	onLoad(e) {
		if (e.type) {
			this.item.type = e.type;
		}
		if (e.id) {
			this.http.request({
				url: '/app/position/user/push/detail/' + e.id,
				success: (res) => {
					this.item = res.data.data;
					this.$refs.editor.setContents();
				}
			});
		}
		uni.$on('select_position', (res) => {
			this.item.positionName = res.title;
			this.item.positionId = res.id;
			this.$forceUpdate();
		});
	},
	methods: {
		backTo() {
			this.http.request({
				url: '/app/position/close/' + this.item.id,
				success: (res) => {
					uni.showToast({
						title: '操作成功!'
					});
					setTimeout(() => {
						uni.$emit('position');
						uni.navigateBack();
					}, 1500);
				}
			});
		},
		go(url) {
			uni.navigateTo({
				url: url
			});
		},
		picker(e, tag) {
			if (tag == 'startDate' || tag == 'endDate') {
				this.item[tag] = e.detail.value;
			} else {
				this.item[tag] = this.dict[tag][e.detail.value];
			}
			if (this.item.positionName == '选择职位') {
				this.item.positionName = '';
				uni.navigateTo({
					url: '/pages/position/select'
				});
			}
			if (this.item.location == '选择工作地点') {
				this.item.location = '';
				this.chooseLocation();
			}
			this.$forceUpdate();
		},
		chooseLocation() {
			uni.chooseLocation({
				success: (res) => {
					let reg = /.+?(省|市|自治区|自治州|县|区)/g;
					let addressList = res.address.match(reg).toString().split(',');
					//注意区分直辖市;
					let city = addressList.length >= 3 ? addressList[1] : addressList[0];
					let region = addressList.length >= 3 ? addressList[2] : addressList[1];
					this.item.
					this.item.location = res.name;
					this.item.address = res.address;
					this.item.longitude = res.longitude;
					this.item.latitude = res.latitude;
					this.item.regionName = region;
					this.$forceUpdate();
				}
			});
		},
		save() {
			/* 			if (!this.verify.check(this.item, this.rule)) {
				uni.showModal({
					content: this.verify.error,
					showCancel: false
				});
				return false;
			} */
			this.item.type=0;
			this.http.request({
				url: '/app/position/edit',
				data: this.item,
				method: 'POST',
				success: (res) => {
					uni.showToast({ title: '发布成功' });
					setTimeout(() => {
						uni.$emit('position');
						uni.navigateBack();
					}, 1500);
				}
			});
		}
	}
};
</script>

<style lang="scss"></style>
