<template>
	<view class="content">
		<view class='content1'>
			<view style='padding:5px 0 10px;border-bottom:1px solid #eee;' class="flex width100">
				<view style='width:100px;font-size:14px;'>姓名</view>
				<view class="flex">
					<u--input placeholder="输入姓名" inputAlign='left' border="none" v-model="dataDetails.name">
					</u--input>
				</view>
			</view>
			<view style='padding:10px 0 5px;border-bottom:1px solid #eee;' class="flex width100">
				<view style='width:100px;font-size:14px;'>联系电话</view>
				<view class="flex">
					<u--input disabledColor='#fff' disabled placeholder="输入联系电话" inputAlign='left' border="none"
						v-model="dataDetails.phone">
					</u--input>
				</view>
			</view>
			<view class="flex row" style='padding:10px 0 5px;border-bottom:1px solid #eee;'>
				<view style='width:100px;font-size:14px;'>性别</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 class="flex row row-ch" style='padding:10px 0 5px;'>
				<view style='width:100px;font-size:14px;'>称呼</view>
				<u--input placeholder="输入姓氏" class="ch-style" inputAlign='left' border="none"
					v-model="dataDetails.call">
				</u--input>
				<view class="flex align-center" v-if="dataDetails.sex=='男'">先生</view>
				<view class="flex align-center" v-else>女士</view>
			</view>
		</view>
		<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 class="flex row">
				<view class="left-text">身份证号</view>
				<u--input placeholder="请输入身份证号" inputAlign='left' border="none" v-model="dataDetails.cardNumber">
				</u--input>
			</view>

			<view class="flex row noborder">
				<view class="left-text">身份证截止日期</view>
				<view style='font-size:16px;width:50%;' class='flex flex-space-between' @click="selectValidityPeriod">
					<view :style="{'color':dataDetails.cardValidityDate ? '#000':'#C6CBD5'}">
						{{dataDetails.cardValidityDate?dataDetails.cardValidityDate:'选择身份证截止日期'}}
					</view>
					<view>
						<u-icon name="arrow-right" color="#7E7E7E" size="10"></u-icon>
					</view>
				</view>
			</view>
			<view class="bz">注：个人信息审核通过后自动认证成为个人货主。</view>
			<u-picker :show="isShowcardValidity" ref="uPicker" :columns="validityPeriod"
				@confirm="confirmValidityPeriod" @change="changeHandler" @cancel="isShowcardValidity=false">
			</u-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;' @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="submit()">
				提交
			</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>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import upload from '@/components/upload.vue';
	import uploadImage from '@/components/ossutil/uploadFile.js';
	var _this;
	export default {
		components: {},
		data() {
			return {
				radioCustomStyle: {
					margin: '0 0 0 20rpx'
				},
				radiolist1: [{
						name: '男',
						disabled: false
					},
					{
						name: '女',
						disabled: false
					},
				],
				uploadType: '',
				isShowimgType: false,
				dataDetails: {
					name: '',
					phone: '',
					commonId: '',
					cardNumber: '',
					cardAddressUrl: '',
					cardBackAddressUrl: '',
					cardValidityDate: '',
					driverSex: '男'
				},
				checked: false,
				isShowcardValidity: false,
				imagesrc: '',
				validityPeriod: [],
				isShowimgType: false,
				index: '',
			};
		},
		onLoad(options) {
			var _this = this;
			this.dataDetails = options
			this.validityPeriod = this.$helper.makeValidityPeriod(0, '长期')
			this.dataDetails.phone = this.userInfo.phone
			this.dataDetails.commonId = this.userInfo.id
		},
		computed: {
			...mapState(['hasLogin', 'userInfo']),
		},
		methods: {
			// 性别切换
			radioChange(n) {
				console.log('radioChange', n);
				this.dataDetails.sex = n
			},
			consent() {
				this.checked = !this.checked
			},
			selectValidityPeriod() {
				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);
				}
			},
			//保存图片到相册，方便核查
			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(e) {
				console.log('confirm', e)
				if (e.value[0] == '长期') {
					this.dataDetails.cardValidityDate = e.value[0]
				} else {
					this.dataDetails.cardValidityDate = e.value[0] + '-' + e.value[1] + '-' + e.value[2]
				}

				this.isShowcardValidity = false
			},
			uploadImg(type, index) {
				this.uploadType = type
				this.isShowimgType = true
				this.index = index
			},
			submit() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				var _this = this
				this.$request.baseRequest('post', '/cargoOwnerInfo/api/editCargoOwner', _this.dataDetails).then(res => {
						uni.hideLoading()
						uni.$u.toast('提交成功')
						uni.$u.route('/pages/mine/cargoowner/cargoowner')
					})
					.catch(res => {
						uni.hideLoading()
						uni.showToast({
							title: res.message,
							icon: 'none',
							duration: 2000
						})
					});
			},
			photograph() {
				console.log('拍照')
				let that = this;
				uni.chooseImage({
					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
				console.log(val)
				console.log(this.uploadType)
				if (val.name == '相册') {
					uni.chooseImage({
						count: 1,
						sourceType: this.$helper.chooseImage.sourceType,
						success: function(res) {
							console.log(JSON.stringify(res.tempFilePaths));
							uploadImage(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,
													}).then(res => {
													that.dataDetails.cardNumber = res.data.recPersonNo
													if (!that.dataDetails.name) {
														that.dataDetails.name = res.data.recPerson
													}
													that.$forceUpdate()
												})
												.catch(res => {
													uni.$u.toast(res.message);
												});
											break
											// 身份反面
										case 2:
											that.dataDetails.cardBackAddressUrl = result
											that.$forceUpdate()
											break
									}
								}
							)
						}
					});

				} else {
					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);
	// 	}
	// }
	.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;
	}
</style>
