<template>
	<view class="content">
		<view class="content2 content-other">
			<view class="title">上传身份证人像面</view>
			<view @click.stop="uploadImg(1,index)" class="picture picture1" v-if="!dataDetails.cardAddressUrl">
				<image class="xj-image" src="@/static/mine/ic_shanchuan@2x.png"></image>
				<view class="text">上传身份证人像面</view>
			</view>
			<view v-if="dataDetails.cardAddressUrl" @click.stop="uploadImg(1,index)" class="preview-card-img picture">
				<view @click.stop="delCard(1)">
					<image class='del-card' src="@/static/images/common/quxiao@2x.png">
					</image>
				</view>
				<image class="" :src="dataDetails.cardAddressUrl" mode="aspectFit" style=""></image>
			</view>

			<view class="title">上传身份证国徽面</view>
			<view @click="uploadImg(2,index)" class="picture picture2" v-if="!dataDetails.cardBackAddressUrl">
				<image class="xj-image" src="@/static/mine/ic_shanchuan@2x.png"></image>
				<view class="text">上传身份证国徽面</view>
			</view>

			<view v-if="dataDetails.cardBackAddressUrl" @click.stop="uploadImg(2,index)"
				class="preview-card-img picture">
				<!-- <image class="card-img" :src="dataDetails.cardAddressUrl"></image> -->
				<view @click.stop="delCard(2)">
					<image class='del-card' src="@/static/images/common/quxiao@2x.png">
					</image>
				</view>

				<image class="" :src="dataDetails.cardBackAddressUrl" mode="aspectFit"></image>
			</view>
			<view style='border-bottom:1px solid #eee;' class="flex row flex-space-between width100">
				<view class="left-text" style='font-size:14px;'>姓名</view>
				<view style='padding-right:10px;' class="flex">
					<u--input @input='nameinput' placeholder="输入姓名" inputAlign='right' border="none"
						v-model="dataDetails.name">
					</u--input>
				</view>
			</view>
			<view class="flex row flex-space-between" style='border-bottom:1px solid #eee;'>
				<view class="left-text" style='font-size:14px;'>性别</view>
				<view>
					<u-radio-group v-model="dataDetails.sex" placement="row">
						<u-radio :customStyle="radioCustomStyle" v-for="(item, index) in radiolist1" :key="index"
							:label="item.name" :name="item.name" @change="radioChange">
						</u-radio>
					</u-radio-group>
				</view>
			</view>
			<view style='margin-top:10px;padding-bottom:11px;' class="flex row flex-space-between">
				<view class="left-text" style='font-size:14px;'>称呼</view>
				<view style='padding-right:10px;' class='flex flex-space-between'>
					<view style="width: 160rpx;">
						<u--input style='padding-right:4px;' placeholder="输入姓氏" class="ch-style" inputAlign='right'
							border="none" v-model="dataDetails.call">
						</u--input>
					</view>
					<view style='position:relative;top:0px;font-size:15px;' class="flex align-center"
						v-if="dataDetails.sex=='男'">先生</view>
					<view style='position:relative;top:0px;font-size:15px;' class="flex align-center" v-else>女士</view>
				</view>
			</view>

			<view style='border-bottom:1px solid #eee;margin-top:10px;padding-bottom:11px;'
				class="flex row width100 flex-space-between">
				<view class="left-text" style='font-size:14px;'>联系电话</view>
				<view style='padding-right:10px;' class="flex">
					<u--input disabledColor='#fff' disabled placeholder="输入联系电话" inputAlign='right' border="none"
						v-model="dataDetails.phone" maxlength="11">
					</u--input>
				</view>
			</view>
			<view style='padding-right:10px;margin-top:10px;padding-bottom:11px;' class="flex row">
				<view class="left-text">身份证号</view>
				<u--input :disabled='dataDetails.id&&firstAuthentication.passOnce?true:false' placeholder="请输入身份证号"
					inputAlign='right' border="none" v-model="dataDetails.cardNumber" maxlength="18">
				</u--input>
			</view>

			<view style='padding-right:10px;' class="flex flex-space-between row noborder">
				<view class="left-text">身份证截止日期</view>
				<view style='font-size:16px;align-items: center;' class='flex' @click="selectValidityPeriod">
					<view :style="{'color':dataDetails.cardValidityDate ? '#000':'#C6CBD5'}">
						{{dataDetails.cardValidityDate?dataDetails.cardValidityDate:'选择身份证截止日期'}}
					</view>
					<!-- <image class='right' src="@/static/mine/right.png" mode=""></image> -->
					<u-icon name="arrow-right" color="#333333" size="16"></u-icon>
				</view>
			</view>
			<view class="bz">注：个人信息审核通过后自动认证成为个人货主。</view>
			<itmister-date-picker :overdueContent="'身份证已过期'" :dateStatus="1" :periodOfValidity="true" :futureYear="30"
				:checkYear="year" :checkMonth="month" :checkDay="day" ref="dateValidityPeriod"
				@dateConfirm="confirmValidityPeriod"></itmister-date-picker>
		</view>

		<view class="content4">
			<view style='margin:0 0 18px;font-size:12px;color:#999;' class='flex items-center'>
				<u--image style='margin-right:5px;margin-top: 4rpx;' @click='consent'
					:src="checked?'../../../static/mine/duihao@2x.png':'../../../static/mine/wxz.png'" width="12px"
					height="12px"></u--image>
				我已阅读并同意全部细则
			</view>
			<view class='line'></view>
			<view class="next-btn" @click="$u.throttle(submit(), 1000)">
				提交
			</view>
		</view>
		<!-- <view class="flex flex-space-between width100">
				<view>收款人</view>
				<view class="flex">
					<u--input placeholder="输入收款人姓名" inputAlign='right' border="none" v-model="dataDetails.name">
					</u--input>
				</view>
			</view> -->
		<u-action-sheet :actions="$helper.imgTypeList" :title="$helper.imgType" :show="isShowimgType"
			@select="imgTypeSelect" @cancel='isShowimgType=false' :closeOnClickOverlay="true" :closeOnClickAction="true"
			@close="isShowimgType=false">
		</u-action-sheet>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import upload from '@/components/upload.vue';
	import permision from "@/js_sdk/wa-permission/permission.js"
	import uploadImage from '@/components/ossutil/uploadFile.js';
	var _this;
	export default {
		components: {},
		data() {
			return {
				radioCustomStyle: {
					margin: '0 20rpx 0 0'
				},
				submitstatus: false,
				radiolist1: [{
						name: '男',
						disabled: false
					},
					{
						name: '女',
						disabled: false
					},
				],
				year: '',
				month: '',
				day: '',
				uploadType: '',
				isShowimgType: false,
				dataDetails: {
					name: '',
					phone: '',
					sex: '男',
					commonId: '',
					cardNumber: '',
					cardAddressUrl: '',
					cardBackAddressUrl: '',
					cardValidityDate: '',
					driverSex: '男',
					cardValidityDate: "",
				},
				checked: false,
				isShowcardValidity: false,
				imagesrc: '',
				validityPeriod: [],
				isShowimgType: false,
				index: '',
				firstAuthentication: {}
			};
		},
		onLoad(options) {
			// this.get_camera_permission()
			var _this = this;
			// console.log(options)
			if (JSON.stringify(options) != "{}") {
				this.dataDetails = options
				if (this.dataDetails.cardValidityDate && this.dataDetails.cardValidityDate != '长期') {
					const arr = this.dataDetails.cardValidityDate.split('-')
					this.year = arr[0]
					this.month = arr[1]
					this.day = arr[2]
				}
				if (this.dataDetails.cardValidityDate == '长期') {
					this.year = '长期'
				}
			}
			this.dataDetails.phone = this.userInfo.phone
			this.dataDetails.commonId = this.userInfo.id
		},
		onShow() {
			this.$request.baseRequest('get', '/cargoOwnerInfo/firstAuthentication', {
				phone: this.userInfo.phone
			}).then(res => {
				uni.setStorageSync("firstAuthentication", res.data)
				this.firstAuthentication = res.data
			})
		},
		computed: {
			...mapState(['hasLogin', 'userInfo']),
		},
		methods: {
			async get_camera_permission() {
				var photol = await permision.requestAndroidPermission("android.permission.CAMERA")
				if (photol == false) {
					uni.showModal({
						title: '提示',
						content: '您已经关闭相机权限,去设置',
						success: function(res) {
							if (res.confirm) {
								permision.gotoAppPermissionSetting()
								// plus.runtime.openURL("app-settings:");
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			},
			nameinput() {
				this.dataDetails.call = this.dataDetails.name[0]
			},
			// 性别切换
			radioChange(n) {
				console.log('radioChange', n);

				this.dataDetails.sex = n
				this.$forceUpdate()
				console.log(this.dataDetails.sex)
			},
			consent() {
				this.checked = !this.checked
			},
			selectValidityPeriod() {
				this.$refs.dateValidityPeriod.show();
				// this.isShowcardValidity = true
			},
			//设置图片
			setImage(e) {
				// 
				console.log(e);
				//显示在页面
				//this.imagesrc = e.path;
				if (e.dotype == 'idphoto') {
					_this.zjzClipper(e.path);
				} else if (e.dotype == 'watermark') {
					_this.watermark(e.path);
				} else {
					_this.savePhoto(e.path);
				}
			},
			delCard(status) {
				if (status == 1) {
					this.dataDetails.cardAddressUrl = ''
					this.$forceUpdate()
				} else {
					this.dataDetails.cardBackAddressUrl = ''
					this.$forceUpdate()
				}
			},
			//保存图片到相册，方便核查
			savePhoto(path) {
				// 
				this.imagesrc = path;
				uploadImage(path, 'appData/',
					result => {
						// 上传成功
						console.log('图片地址', result)
					}
				)
				//保存到相册
				// uni.saveImageToPhotosAlbum({
				// 	filePath: path,
				// 	success: () => {
				// 		uni.showToast({
				// 			title: '已保存至相册',
				// 			duration: 2000
				// 		});
				// 	}
				// });
			},
			changeHandler(e) {
				const {
					columnIndex,
					value,
					values,
					index,
					picker = this.$refs.uPicker
				} = e

				// if (columnIndex === 0) {
				// 	
				// 	if (e.index != 0) {
				// 		picker.setColumnValues(1, this.validityPeriod[1].shift())
				// 	}

				// } else if (columnIndex === 1) {
				// 	if (e.index != 0) {
				// 		picker.setColumnValues(2, this.validityPeriod[2].shift())
				// 	}
				// }
			},
			confirmValidityPeriod(date) {
				this.dataDetails.cardValidityDate = date.date
			},
			uploadImg(type, index) {
				this.get_camera_permission()
				this.uploadType = type
				this.index = index
				this.imgTypeSelect()
				// this.isShowimgType = true

			},
			validate() {
				if (uni.$u.test.isEmpty(this.dataDetails.name)) {
					this.$refs.uToast.show({
						type: 'error',
						message: "姓名不能为空！",
					})
					return true
				}
				if (this.dataDetails.name.length < 2 || this.dataDetails.name.length > 10) {
					this.$refs.uToast.show({
						type: 'error',
						message: "姓名输入错误！",
					})
					return true
				}
				if (this.dataDetails.call.length == 0) {
					this.$refs.uToast.show({
						type: 'error',
						message: "称呼不能为空！",
					})
					return true
				}
				if (this.dataDetails.call.length > 4) {
					this.$refs.uToast.show({
						type: 'error',
						message: "称呼输入错误！",
					})
					return true
				}

				if (uni.$u.test.isEmpty(this.dataDetails.cardValidityDate)) {
					this.$refs.uToast.show({
						type: 'error',
						message: "请选择身份证截止日期！",
					})
					return true
				}

				if (this.dataDetails.cardValidityDate != '长期') {
					var arr = this.dataDetails.cardValidityDate.split('-')
					if (!this.$helper.periodOfValidity(arr[0], arr[1], arr[2])) {
						this.$refs.uToast.show({
							type: 'error',
							message: "身份证已过期！",
						})
						return true
					}
				}
				if (uni.$u.test.isEmpty(this.dataDetails.cardNumber)) {
					this.$refs.uToast.show({
						type: 'error',
						message: "身份证号不能为空！",
					})
					return true
				}
				if (uni.$u.test.isEmpty(this.dataDetails.cardAddressUrl)) {
					this.$refs.uToast.show({
						type: 'error',
						message: "请上传身份证人像面！",
					})
					return true
				}
				if (uni.$u.test.isEmpty(this.dataDetails.cardBackAddressUrl)) {
					this.$refs.uToast.show({
						type: 'error',
						message: "请上传身份证国徽面！",
					})
					return true
				}
				if (this.dataDetails.cardNumber.length != 18) {
					this.$refs.uToast.show({
						type: 'error',
						message: "身份证号输入错误！",
					})
					return true
				}
				if (!this.checked) {
					this.$refs.uToast.show({
						type: 'error',
						message: "请勾选细则！",
					})
					return true
				}
			},
			submit() {
				if (this.validate()) return
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				var _this = this
				delete _this.dataDetails.hyCargoOwnerCompInfoList
				_this.dataDetails.accountNumber = uni.getStorageSync("userInfo").phone
				_this.$request.baseRequest('post', '/cargoOwnerInfo/api/editCargoOwner', _this.dataDetails).then(res => {
						// console.log(res)
						// uni.$u.route('/pages/mine/cargoowner/cargoowner')
						uni.hideLoading()
						uni.$u.toast('提交成功')
						uni.navigateBack({
							delta: 1
						})
					})
					.catch(res => {
						console.log(res)
						uni.hideLoading()
						uni.showToast({
							title: res.message,
							icon: 'none',
							duration: 2000
						})
					});
			},
			photograph() {
				console.log('拍照')
				let that = this;
				uni.chooseImage({
					sizeType: ['compressed'],
					count: 1,
					success: function(res) {
						console.log(JSON.stringify(res.tempFilePaths));
						uploadImage(res.tempFilePaths[0], 'appData/',
							result => {
								// 上传成功
								console.log('图片地址', result)
							}
						)
					}
				});

			},

			imgTypeSelect(val) {
				let that = this
				uni.chooseImage({
					count: 1,
					success: function(res) {
						console.log(JSON.stringify(res.tempFilePaths));
						uploadImage("image", res.tempFilePaths[0], 'appData/',
							result => {
								// 上传成功回调函数
								console.log('图片地址', result)

								switch (that.uploadType) {
									// 身份正面
									case 1:
										that.dataDetails.cardAddressUrl = result
										// 识别
										that.$request.baseRequest('get',
												'/driverInfo/personShibie', {
													personImg: result,
													flag: 1
												}).then(res => {
												// if(!that.dataDetails.id){
												if (res.data.recPersonNo) {
													that.dataDetails.cardNumber = res.data.recPersonNo
														.replace(/\s*/g, "")
													if (that.dataDetails.cardNumber[16] % 2 == 0) {
														that.dataDetails.sex = '女'
													} else {
														that.dataDetails.sex = '男'
													}
												}
												if (res.data.recPerson) {
													that.dataDetails.name = res.data.recPerson
													that.dataDetails.call = that.dataDetails.name[0]
												}
												// }


												that.$forceUpdate()
											})
											.catch(res => {
												uni.$u.toast(res.message);
											});
										break
										// 身份反面
									case 2:
										that.dataDetails.cardBackAddressUrl = result
										// 识别
										that.$request.baseRequest('get',
												'/driverInfo/personShibie', {
													personImg: result,
													flag: 2
												}).then(res => {
												if (res.data.idCardValidity) {
													let date = res.data.idCardValidity.split("-")[1]
													that.dataDetails.cardValidityDate = date.replace(
														'.', '-').replace('.', '-')
												}
												that.$forceUpdate()
											})
											.catch(res => {
												uni.$u.toast(res.message);
											});
										break
								}
							}
						)
					}
				});
				// 	switch (this.uploadType) {
				// 		case 1:
				// 			uni.$u.route('/pages/mine/camera/idcard/idcard?dotype=idcardface');
				// 			console.log('身份正面')
				// 			break
				// 		case 2:
				// 			uni.$u.route('/pages/mine/camera/idcard/idcard?dotype=badge');
				// 			console.log('身份反面')
				// 			break

			},
			//身份证正面
			getImgUrl1(src) {
				// console.log(src)
				// console.log('------------res-----------')
				// let that = this;
				// that.id[0] = src
				// that.id1 = src
				// that.certificates = false
				// that.personImgs.personImg = that.id[0]
				// that.$api.doRequest('get', '/driverViewInfo/personShibie', that
				// 	.personImgs).then(res => {
				// 	if (res.data.data.recPerson != null) {
				// 		if (res.data.data.recPerson != "") {
				// 			that.$set(that.DriverViewInfo, 'driverName', res
				// 				.data.data.recPerson)
				// 		}
				// 	}
				// 	if (res.data.data.recPersonNo != null) {
				// 		if (res.data.data.recPersonNo != "") {
				// 			that.$set(that.DriverViewInfo, 'numberCard', res
				// 				.data.data.recPersonNo)
				// 		}
				// 	}
				// }).catch(res => {
				// 	uni.showToast({
				// 		title: res.data.message,
				// 		icon: 'none',
				// 		duration: 2000
				// 	})
				// })
			},
		},
	};
</script>

<style lang="scss" scoped>
	// .preview-card-img {
	// 	/deep/uni-image>div, uni-image>img {
	// 	    transform: scale(1.5) rotate(-90deg);
	// 	}
	// }
	/deep/.uni-input-input:disabled {
		background: #fff;
	}

	.picture {
		margin-top: 20rpx;
		background: #F5F6FA;
		width: 212rpx;
		height: 212rpx;
		border-radius: 20rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		color: #6A7282;
	}

	.content1 {
		background: white;
		margin: 20rpx;
		padding: 20rpx;
		border-radius: 20rpx;

		.row-ch {
			padding-right: 180rpx;
			box-sizing: border-box;
			// background-color: red;
		}
	}

	.content2 {
		margin: 10px 0 95px 0;

		.left-text {
			// background: red;
			width: 290rpx;
			color: #333333;
			display: flex;
			align-items: center;
			font-size: 14px;
		}

		.row {
			border-bottom: 1px solid #EEEEEE;
			padding-bottom: 28rpx;
			margin-top: 26rpx;

			.ch-style {
				text-align: left;
			}
		}

		.row-ch {
			padding-right: 180rpx;
			box-sizing: border-box;
			// background-color: red;
		}
	}

	.content2,
	.content3 {
		.del-bank {
			position: absolute;
			top: 0;
			right: 0;
			width: 80rpx;
			height: 80rpx;
		}

		.picture {
			width: 100%;
			height: 440rpx;
			position: relative;

			.text {
				margin-top: 20rpx;
			}
		}

		.picture1 {
			background: url(../../../static/mine/zm.png);
			background-size: 100% 100%;
		}

		.picture2 {
			background: url(../../../static/mine/gh.png);
			background-size: 100% 100%;
		}

		.picture3 {
			background: url(../../../static/mine/yhkzm.png);
			background-size: 100% 100%;
		}

		.xj-image {
			width: 100rpx;
			height: 100rpx;
		}

		.title {
			color: #999999;
			margin: 20rpx 0;
			font-size: 14px;
		}
	}

	.u-input {
		font-size: 16px;
	}

	.content4 {
		position: fixed;
		bottom: 0;
		background: white;
		width: 100%;
		text-align: center;
		left: 0;
		box-sizing: border-box;
		;
		padding: 10px 15px;

		.line {
			position: absolute;
			left: 0;
			margin-top: -9px;
			border-top: 1px solid #eee;
			width: 100%;
		}

		.next-btn {
			background: #2772FB;
			width: 85%;
			padding: 20rpx 20rpx;
			text-align: center;
			color: white;
			border-radius: 50rpx;
			margin: 0 auto;
			margin-top: 10px;
		}
	}

	.del-card {
		position: absolute;
		top: -10rpx;
		right: -6rpx;
		width: 80rpx;
		height: 80rpx;
		z-index: 9;
	}

	.bz {
		font-size: 28rpx;
	}

	.right {
		width: 16px;
		height: 16px;
		position: relative;
		top: 1px;
	}
</style>
