<template>
	<view>
		
		<view class="cu-list menu sm-border">
			<view class="cu-item arrow" @click="goHome">
				<view class="content">
					<text class="cuIcon-friend text-grey"></text>
					<text>切换身份</text>
				</view>
			</view>
		</view>
		
		
		
		<form>
			<view class="cu-form-group margin-top-sm">
				<view class="title">雇主类型</view>
				<input class="text-gray" placeholder="请输入..." data-field="wtype" name="wtype" :value="workerinfo.wtype==1?'个人雇主':'企业雇主'"
				 disabled="true"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">公司名称</view>
				<input placeholder="请输入..." data-field="title" name="title" :value="workerinfo.title" disabled="true"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">微店名称</view>
				<input placeholder="请输入..." data-field="ftitle" name="ftitle" :value="workerinfo.ftitle" disabled="true"></input>
			</view>
			<view class="padding-lr padding-tb-sm text-gray text-sm">上面信息不可修改</view>
			
			<view class="cu-form-group">
				<view class="title">姓名</view>
				<input placeholder="请输入..." data-field="realname" name="realname" :value="workerinfo.realname" @input="setInput"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">手机号</view>
				<input placeholder="请输入..." data-field="mobile" name="mobile" :value="workerinfo.mobile" @input="setInput"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">微信号</view>
				<input placeholder="请输入..." data-field="weixin" name="weixin" :value="workerinfo.weixin" @input="setInput"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">公开电话</view>
				<picker mode="selector" @change="isPublicPickerChange" :value="isPublicIndex" :range="isPublicPickerList" :range-key="`text`">
					<view class="picker padding-tb text-df" style="font-size: 30rpx;line-height:40rpx;text-align:left;">{{isPublicIndex>-1 ? isPublicPickerList[isPublicIndex].text : ("请选择...")}}</view>
				</picker>
			</view>
			
			
			<view class="cu-form-group margin-top-sm">
				<view class="title">微店图标</view>
				<view class="cu-avatar radius bg-gray">
					<avatar selWidth="640upx" selHeight="640upx" @upload="setTilpic" :avatarSrc="workerinfo.tilpic" noTab="true"
					 avatarStyle="width: 75upx; height: 75upx; border-radius: 5%;"></avatar>
				</view>
			</view>

			<view class="cu-form-group margin-top-sm">
				<view class="title">所属地区</view>
				<view style="text-align: left; flex: 1; font-size: 30rpx;">
					<pickerAddress @change="regionChange">{{workerinfo.province}} {{workerinfo.city}} {{workerinfo.district}}</pickerAddress>
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">详细地址</view>
				<input placeholder="请输入..." data-field="address" name="address" :value="workerinfo.address" @input="setInput"></input>
			</view>
			<view class="cu-form-group align-start">
				<view class="title">公司介绍</view>
				<textarea maxlength="-1" placeholder="请输入..." data-field="details" name="details" :value="workerinfo.details"
				 @input="setInput"></textarea>
			</view>
			
			<view class="padding flex flex-direction">
				<button class="cu-btn bg-green margin-tb-sm lg" @tap="editWorker">立即提交</button>
			</view>
			<view class="padding"></view>
		</form>

	</view>
</template>

<script>
	var _this;
	import avatar from "@/components/yq-avatar/yq-avatar.vue";
	import pickerAddress from "@/components/pickerAddress/pickerAddress.vue";
	export default {
		components: {
			avatar,
			pickerAddress
		},
		data() {
			return {
				userinfo: {},
				isRotate: false,
				workerinfo: false,
				isPublicPickerList: [
					{id:1, text: '是'},
					{id:2, text: '否'},
				],
				isPublicIndex: -1,
				isPublicValue: {
					id: false,
					text: false,
				},
			}
		},
		onLoad: function() {
			_this = this;
			_this.workerinfo = uni.getStorageSync('workerinfo') || false;
			if (_this.workerinfo === false) {
				uni.reLaunch({
					url: "/pages/my/my"
				});
				return false;
			}
			if(_this.workerinfo.is_public >= 1) {
				_this.isPublicIndex = _this.workerinfo.is_public - 1;
				_this.isPublicValue.text = _this.isPublicPickerList[_this.isPublicIndex].text;
				_this.isPublicValue.id = _this.isPublicPickerList[_this.isPublicIndex].id;
			}
		},
		methods: {
			// 填写字段
			setTilpic: function(rsp) {
				_this.setPic(rsp, "tilpic");
			},
			setPicone: function(rsp) {
				_this.setPic(rsp, "picone");
			},
			setPictwo: function(rsp) {
				_this.setPic(rsp, "pictwo");
			},
			setPicthr: function(rsp) {
				_this.setPic(rsp, "picthr");
			},
			setPic: function(rsp, picfield) {
				_this.$req.ajaxFile({
					path: "attachment/tplfieldimage",
					title: '正在上传',
					filePath: rsp.path,
					fileName: 'file',
				}).then((filedata) => {
					var data = JSON.parse(filedata.data);
					_this.workerinfo[picfield] = data.data.src;
				}).catch((err) => {
					uni.showModal({
						title: '信息提示',
						content: err,
						showCancel: false
					});
				});
			},
			setInput: function(e) {
				var field = e.currentTarget.dataset.field;
				_this.workerinfo[field] = e.detail.value;
			},
			regionChange: function(data) {
				_this.workerinfo.province = data.data[0];
				_this.workerinfo.city = data.data[1];
				_this.workerinfo.district = data.data[2];
			},
			//公开电话
			isPublicPickerChange: function(e) {
				console.log(e);
				_this.workerinfo.is_public = e.detail.value - 0 + 1;
				_this.isPublicIndex = e.detail.value;
				_this.isPublicValue.text = _this.isPublicPickerList[_this.isPublicIndex].text;
				_this.isPublicValue.id = _this.isPublicPickerList[_this.isPublicIndex].id;
			},
			
			goHome: function() {
				_this.userinfo = _this.checkLogin("/pages/my/my");
				if (_this.userinfo === false) {
					return false;
				}
				uni.setStorageSync('identityinfo', true);
				uni.setStorageSync('workerinfo', null);
				uni.reLaunch({
					url: "/pages/index/index"
				});
			},

			// 提交修改
			editWorker: function() {
				if (_this.isRotate) {
					return false;
				}
				_this.isRotate = true;
				_this.$req.ajax({
					path: "worker/editworker",
					title: "正在提交修改",
					data: {
						id: _this.workerinfo.id,
						userid: _this.workerinfo.userid,
						tilpic: _this.workerinfo.tilpic,
						realname: _this.workerinfo.realname,
						mobile: _this.workerinfo.mobile,
						position: _this.workerinfo.position,
						weixin: _this.workerinfo.weixin,
						province: _this.workerinfo.province,
						city: _this.workerinfo.city,
						district: _this.workerinfo.district,
						address: _this.workerinfo.address,
						details: _this.workerinfo.details,
						is_public:_this.workerinfo.is_public,
					}
				}).then((data) => {
					uni.setStorageSync('workerinfo', data.worker);
					uni.showModal({
						title: '信息提示',
						content: '公司信息修改成功。',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								uni.redirectTo({
									url: "/pages/worker/worker"
								});
							}
						}
					});
					_this.isRotate = false;
				}).catch((err) => {
					uni.showModal({
						title: '信息提示',
						content: err,
						showCancel: false
					});
					_this.isRotate = false;
				});
			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(5em + 15px);
	}

	.fieldpic {
		width: 150rpx;
		height: 150rpx;
	}

	.fieldpictil {
		width: 230rpx;
		text-align: center;
	}
</style>
