<template>
	<view>
		
		<view class="cu-list menu margin-bottom">
			<view class="cu-item">
				<view class="content">
					<text class="text-gray">代理门店</text>
				</view>
				<view class="action">
					<text>{{broker.agent.title}}</text>
				</view>
			</view>
		</view>
		
		<view v-if="entry==null">
			<view class="cu-bar bg-white solids-bottom">
				<view class="action">
					<text class="cuIcon-titles text-blue"></text> 报备信息
				</view>
				<view class="action">
					<!-- <text class="text-df text-blue">扫一扫 自动识别身份证</text> -->
					<avatar selWidth="640rpx" selHeight="640rpx" @upload="setIdcard" :avatarSrc="$getImageUrl('static/images/applet/setidcard.png')" noTab="true" avatarStyle="width: 400rpx; height: 50rpx; border-radius: 5%;"></avatar>
				</view>
			</view>
			<form>
				<view class="cu-form-group">
					<view class="title">姓名</view>
					<input placeholder="请输入姓名..." @input="bindInput" data-val="realname" :value="forminfo.realname"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">手机号</view>
					<input placeholder="请输入手机号..." @input="bindInput" data-val="mobile" :value="forminfo.mobile"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">身份证号</view>
					<input placeholder="请输入身份证号..." @input="bindInput" data-val="idcard" :value="forminfo.idcard"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">性别</view>
					<picker @change="genderChange" :value="forminfo.gender" :range="forminfo.genderarr">
						<view class="picker">
							{{forminfo.genderarr[forminfo.gender]}}
						</view>
					</picker> 
				</view>
				<view class="cu-form-group">
					<view class="title">民族</view>
					<input placeholder="请输入民族..." @input="bindInput" data-val="nation" :value="forminfo.nation"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">户籍地址</view>
					<input placeholder="请输入户籍地址..." @input="bindInput" data-val="address" :value="forminfo.address"></input>
				</view>
				<view class="cu-form-group margin-top">
					<view class="title">意向工厂</view>
					<picker @change="factoryidChange" :value="forminfo.factoryid" :range="forminfo.factoryidarr">
						<view class="picker">
							{{forminfo.factoryidarr[forminfo.factoryid]}}
						</view>
					</picker> 
				</view>
				<view class="cu-form-group">
					<view class="title">出发日期</view>
					<picker mode="date" :value="forminfo.startdate" start="2019-01-01" end="2100-12-31" @change="startdateChange">
						<view class="picker">
							{{forminfo.startdate}}
						</view>
					</picker>
				</view>
				<view class="cu-form-group">
					<view class="title">到达日期</view>
					<picker mode="date" :value="forminfo.enddate" start="2019-01-01" end="2100-12-31" @change="enddateChange">
						<view class="picker">
							{{forminfo.enddate}}
						</view>
					</picker>
				</view>
				<view class="cu-form-group">
					<view class="title">返费政策</view>
					<input placeholder="请输入返费政策..." @input="bindInput" data-val="refpolicy" :value="forminfo.refpolicy"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">报备状态</view>
					<picker @change="statusChange" :value="forminfo.status" :range="forminfo.statusarr">
						<view class="picker">
							{{forminfo.statusarr[forminfo.status]}}
						</view>
					</picker> 
				</view>
				<view class="cu-form-group align-start">
					<view class="title">报备备注</view>
					<textarea maxlength="140" placeholder="请输入报备备注..." @input="bindInput" data-val="agentremark" :value="forminfo.agentremark"></textarea>
				</view>
				
				<view class="padding flex flex-direction">
					<button class="cu-btn bg-orange margin-tb-sm lg" @tap="addEntry">确认提交</button>
				</view>
			</form>
		</view>
		<view v-else>
			<view class="cu-bar bg-white solid-bottom margin-top">
				<view class="action">
					<text class="cuIcon-titles text-blue"></text> 报备信息
				</view>
			</view>
			<view class="cu-list menu sm-border">
				<view class="cu-item">
					<view class="content">
						<text class="text-gray">姓名</text>
					</view>
					<view class="action">
						<text>{{entry.realname}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="text-gray">手机号</text>
					</view>
					<view class="action">
						<text>{{entry.mobile}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="text-gray">性别</text>
					</view>
					<view class="action">
						<text>{{entry.gender}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="text-gray">民族</text>
					</view>
					<view class="action">
						<text>{{entry.nation}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="text-gray">地址</text>
					</view>
					<view class="action">
						<text>{{entry.address}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="text-gray">身份证号</text>
					</view>
					<view class="action">
						<text>{{entry.idcard}}</text>
					</view>
				</view>
			</view>
			<view class="cu-list menu sm-border">
				<view class="cu-item">
					<view class="content">
						<text class="text-gray">意向工厂</text>
					</view>
					<view class="action">
						<text>{{entry.reportFactory.title}}</text>
					</view>
				</view>	
				<view class="cu-item">
					<view class="content">
						<text class="text-gray">返费模式</text>
					</view>
					<view class="action">
						<text>{{entry.reftype}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="text-gray">返费政策</text>
					</view>
					<view class="action">
						<text>{{entry.refpolicy}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="text-gray">出发日期</text>
					</view>
					<view class="action">
						<text>{{entry.startdate}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="text-gray">到达日期</text>
					</view>
					<view class="action">
						<text>{{entry.enddate}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="text-gray">报备时间</text>
					</view>
					<view class="action">
						<text>{{entry.createtime}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="text-gray">报备备注</text>
					</view>
					<view class="action">
						<text>{{entry.agentremark}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="text-gray">报备状态</text>
					</view>
					<view class="action">
						<text>{{statusarr[entry.status]}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="text-gray">状态备注</text>
					</view>
					<view class="action">
						<text>{{entry.remark}}</text>
					</view>
				</view>
			</view>
			<view class="padding flex flex-direction" v-if="entry.status==1">
				<button class="cu-btn bg-orange margin-tb-sm lg" @tap="statusEntry">报备信息确认</button>
				<button class="cu-btn bg-white margin-tb-sm lg" @tap="deleteEntry">删除报备信息</button>
			</view>
			<view class="padding"></view>
		</view>
		
	</view>
</template>

<script>
	import avatar from "@/components/yq-avatar/yq-avatar.vue";
	var _this;
	export default {
		components: {
			avatar
		},
		data() {
			return {
				isRotate: false,
				brokerinfo: {},
				entry: null,
				broker: {},
				
				forminfo: {
					realname: "",
					mobile: "",
					idcard: "",
					gender: 0,
					nation: "",
					address: "",
					factoryid: 0,
					startdate: "",
					enddate: "",
					reftype: 0,
					refpolicy: "",
					agentremark: "",
					status: 0,
					
					factoryidarr: [],
					genderarr: ['男','女'],
					reftypearr: ['小时工','一次性','管理费'],
					statusarr: ['待确认', '已确认']
				},
				factorylist: [],
				statusarr: ['','待确认', '已确认', '已入职', '未通过']
			}
		},
		onLoad: function(option) {
			_this = this;
			_this.brokerinfo = uni.getStorageSync('brokerinfo') || false;
			if (_this.brokerinfo===false){
				uni.reLaunch({
					url: "/pages/my/my"
				});
				return false;
			}
			var entryid = option.entryid || 0;
			_this.$req.ajax({
				path: "broker/getentry",
				data: {
					entryid: entryid,
					brokerid: _this.brokerinfo.id
				}
			}).then((data) => {
				_this.entry = data.entry;
				_this.forminfo.factoryidarr = data.factoryidarr;
				_this.forminfo.startdate = data.today;
				_this.forminfo.enddate = data.today;
				_this.factorylist = data.factorylist;
				_this.broker = data.broker;
			}).catch((err) => {
				uni.showModal({
					title: '信息提示',
					content: err,
					showCancel: false
				});
			});
		},
		methods: {
			
			// 身份证识别
			setIdcard: function(rsp) {
				if(_this.isRotate){
					return false;
				}
				_this.isRotate = true;
				_this.$req.ajaxFile({
					path: "attachment/tplfieldimage",
					title:'正在上传',
					filePath: rsp.path,
					fileName:'file',
				}).then((filedata) => {
					var fdata = JSON.parse(filedata.data);
					_this.$req.ajax({
						path: "broker/setidcard",
						title:'正在识别',
						data: {
							picpath: fdata.data.path
						}
					}).then((data) => {
						// console.log("data: " + JSON.stringify(data));
						_this.forminfo.realname = data.idcard.name;
						_this.forminfo.idcard = data.idcard.num;
						_this.forminfo.gender = data.idcard.sex=='男' ? 0 : 1;
						_this.forminfo.nation = data.idcard.nationality;
						_this.forminfo.address = data.idcard.address;
						_this.isRotate = false;
					}).catch((err) => {
						uni.showModal({
							title: '信息提示',
							content: err,
							showCancel: false
						});
						_this.isRotate = false;
					});
				}).catch((err) => {
					uni.showModal({
						title: '信息提示',
						content: err,
						showCancel: false
					});
					_this.isRotate = false;
				});
			},
			
			statusEntry: function() {
				if(_this.isRotate){
					return false;
				}
				_this.isRotate = true;
				_this.$req.ajax({
					path: "broker/statusentry",
					data: {
						brokerid: _this.brokerinfo.id,
						entryid: _this.entry.id
					}
				}).then((data) => {
					uni.showModal({
						title: '信息提示',
						content: "报备信息确认成功。",
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								_this.entry = data.entry;
							}
						}
					});
					_this.isRotate = false;
				}).catch((err) => {
					uni.showModal({
						title: '信息提示',
						content: err,
						showCancel: false
					});
					_this.isRotate = false;
				});
			},
			
			deleteEntry: function() {
				if(_this.isRotate){
					return false;
				}
				_this.isRotate = true;
				_this.$req.ajax({
					path: "broker/deleteentry",
					data: {
						brokerid: _this.brokerinfo.id,
						entryid: _this.entry.id
					}
				}).then((data) => {
					uni.showModal({
						title: '信息提示',
						content: "报备信息删除成功。",
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								uni.redirectTo({
								    url: '/pages/broker/myentry'
								});
							}
						}
					});
					_this.isRotate = false;
				}).catch((err) => {
					uni.showModal({
						title: '信息提示',
						content: err,
						showCancel: false
					});
					_this.isRotate = false;
				});
			},
			
			addEntry: function() {
				if(_this.isRotate){
					return false;
				}
				_this.isRotate = true;
				_this.$req.ajax({
					path: "broker/addentry",
					data: {
						brokerid: _this.brokerinfo.id,
						realname: _this.forminfo.realname,
						mobile: _this.forminfo.mobile,
						idcard: _this.forminfo.idcard,
						gender: _this.forminfo.genderarr[_this.forminfo.gender],
						nation: _this.forminfo.nation,
						address: _this.forminfo.address,
						factoryid: _this.factorylist[_this.forminfo.factoryid].id,
						startdate: _this.forminfo.startdate,
						enddate: _this.forminfo.enddate,
						reftype: _this.forminfo.reftypearr[_this.forminfo.reftype],
						refpolicy: _this.forminfo.refpolicy,
						agentremark: _this.forminfo.agentremark,
						status: parseInt(_this.forminfo.status) + 1,
					}
				}).then((data) => {
					uni.showModal({
						title: '信息提示',
						content: "报备信息提交成功。",
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								_this.entry = data.entry;
							}
						}
					});
					_this.isRotate = false;
				}).catch((err) => {
					uni.showModal({
						title: '信息提示',
						content: err,
						showCancel: false
					});
					_this.isRotate = false;
				});
			},
			
			factoryidChange: function(e) {
				_this.forminfo.factoryid = e.detail.value
			},
			statusChange: function(e) {
				_this.forminfo.status = e.detail.value
			},
			reftypeChange: function(e) {
				_this.forminfo.reftype = e.detail.value
			},
			startdateChange: function(e) {
				_this.forminfo.startdate = e.detail.value
			},
			enddateChange: function(e) {
				_this.forminfo.enddate = e.detail.value
			},
			genderChange: function(e) {
				_this.forminfo.gender = e.detail.value
			},
			bindInput: function(e) {
				var dataval = e.currentTarget.dataset.val;
				_this.forminfo[dataval] = e.detail.value;
			},
			
			makePhone: function() {
				uni.makePhoneCall({
					phoneNumber: _this.user.mobile
				});
			}
		}
	}
</script>

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

