<template>
	<view class="main">
		<view class="form_group">
			<view class="lable re">兼职名称</view>
			<input placeholder="请输入" v-model="item.title" :disabled="item.audit == 1" />
		</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">&#xe62b;</view>
			</picker>
		</view>
		<view class="form_group">
			<view class="lable re">兼职描述(要求)</view>
			<leditor ref="editor" v-model="item.contents" placeholder="请输入兼职描述" :read="item.audit == 1"></leditor>
		</view>
		<view class="form_group">
			<view class="lable re">兼职金额(元)</view>
			<view class="bgm">
				<input type="digit" placeholder="请输入" v-model="item.salary" class="input" :disabled="item.audit == 1" />
				<view class="msg">
					<text>{{ money }}</text>
					<text @click="go('/pages/user/money/index')">充值</text>
				</view>
			</view>
			<view class="bz">
				<text class="icon">&#xe634;</text>
				<text>兼职金额从账户余额扣除，请保留充足的余额</text>
			</view>
		</view>
		<view class="form_group">
			<view class="lable re">兼职时间</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 class="form_group">
			<view class="lable re">结算方式</view>
			<input value="完工结算" :disabled="true" />
		</view>
		<view class="form_group">
			<view class="lable">兼职地点</view>
			<picker :disabled="true" @click="chooseLocation()">
				<input :placeholder="item.type == 0 ? '请选择' : '留空不限工作地点'" 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="ok()" v-if="item.id && item.audit == 0">编辑</button>
		<button class="btn" @click="save()" v-if="!item.id">保存</button>
		<u-popup :show="show" round="15" mode="center" :closeable="true" :closeOnClickOverlay="false" :customStyle="{ width: '85%' }" @close="show = false">
			<view class="popup">
				<view class="mtt">兼职金额</view>
				<view class="money">¥{{ item.salary }}</view>
				<view>
					<view class="item">
						<text class="tt">服务费</text>
						<text class="la">¥{{ item.serviceMoney }}</text>
					</view>
					<view class="item">
						<text class="tt">费率</text>
						<text class="la">{{ platform.partTimeService }}%</text>
					</view>
					<view class="item">
						<text class="tt">实际扣除</text>
						<text class="la">¥{{ item.realMoney }}</text>
					</view>
				</view>
				<button class="btn" @click="ok()">确定</button>
			</view>
		</u-popup>
	</view>
</template>
<script>
export default {
	data() {
		return {
			money: 0,
			item: { type: 1 },
			end: this.util.getDate('day'),
			platform: {},
			show: false
		};
	},
	onLoad(e) {
		if (e.id) {
			this.http.request({
				url: '/app/position/manage/detail/' + e.id,
				success: (res) => {
					this.item = res.data.data;
					this.$refs.editor.setContents();
					uni.setNavigationBarTitle({ title: '编辑兼职' });
				}
			});
		}
		uni.$on('select_position', (res) => {
			this.item.positionName = res.title;
			this.item.positionId = res.id;
			this.$forceUpdate();
		});
		this.getPlatform();
	},
	onShow() {
		this.money = uni.getStorageSync('money');
	},
	methods: {
		//平台服务费信息
		getPlatform() {
			this.http.request({
				url: '/app/pay/getPlatform',
				success: (res) => {
					this.platform = res.data.data;
				}
			});
		},
		picker(e, tag) {
			this.item[tag] = e.detail.value;
			this.$forceUpdate();
		},
		go(url) {
			uni.navigateTo({ url: url });
		},
		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.location = res.name;
					this.item.address = res.address;
					this.item.longitude = res.longitude;
					this.item.latitude = res.latitude;
					this.item.regionName = region;
					this.item.cityName = city;
					this.$forceUpdate();
				}
			});
		},
		save() {
			let rule = [
				{ name: 'title', checkType: 'notnull', errorMsg: '请输入兼职名称' },
				{ name: 'positionName', checkType: 'notnull', errorMsg: '请选择兼职类型' },
				{ name: 'contents', checkType: 'notnull', errorMsg: '请输入兼职描述(要求)' },
				{ name: 'salary', checkType: 'notnull', errorMsg: '请输入兼职金额' },
				{ name: 'startDate', checkType: 'notnull', errorMsg: '请选择兼职开始时间' },
				{ name: 'endDate', checkType: 'notnull', errorMsg: '请选择兼职结束时间' }
			];
			if (!this.verify.check(this.item, rule)) {
				uni.showModal({ content: this.verify.error, showCancel: false });
				return false;
			}
			this.show = true;
			this.item.serviceMoney = (this.item.salary * (this.platform.partTimeService / 100)).toFixed(2);
			this.item.realMoney = (parseFloat(this.item.salary) + parseFloat(this.item.serviceMoney)).toFixed(2);
		},
		ok() {
			this.http.request({
				url: this.item.id ? '/app/position/manage/edit' : '/app/position/manage/add',
				data: this.item,
				method: 'POST',
				success: (res) => {
					//实名认证跳转
					if (res.data.code == 7878) {
						uni.showModal({
							title: '提示',
							content: res.data.msg,
							showCancel: false,
							success: (res) => {
								uni.navigateTo({ url: '/pages/user/auth' });
							}
						});
						return;
					}
					//余额不足，请先充值
					if (res.data.code == 8080) {
						uni.showModal({
							title: '提示',
							content: res.data.msg,
							showCancel: false,
							success: (res) => {
								uni.navigateTo({ url: '/pages/user/money/index' });
							}
						});
						return;
					}
					uni.showToast({ title: '操作成功' });
					setTimeout(() => {
						uni.$emit('position');
						uni.navigateBack();
					}, 1500);
				}
			});
		}
	}
};
</script>

<style lang="scss">
.bgm {
	.input {
		width: 50%;
	}
	.msg {
		text {
			padding-left: 17px;
		}
	}
}
</style>
