<template>
	<view v-if="$isRight(goods_details)">
		<view class="bg-white" v-if="enter_type!=1">
			<!-- <view class="cu-bar1 padding-lr">
				<text class="lf-color-555 lf-font-28">联系人</text>
				<input type="text" v-model="contact" class="lf-color-999 lf-font-28 lf-text-left" style="position:absolute;right:180rpx" placeholder="请输入联系人" />
			</view>
			<view class="cu-bar1 padding-lr">
				<text class="lf-color-555 lf-font-28">联系方式</text>
				<view class="lf-row-between">
					<input type="text" v-model="phone" class="lf-color-999 lf-font-28 lf-text-left" style="position:absolute;right:180rpx" placeholder="请输入联系电话" maxlength="11" />
				</view>
				<button style="position: absolute;right: 0rpx;" class="lf-bg-white" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
					<u-icon name="phone"></u-icon>
				</button>
			</view> -->
			<view class="padding-lr lf-m-b-40 lf-m-t-30" :class="item.type == 'text'?'lf-row-between':''" v-for="(item,index) of form_list" :key="index">
				<text class="lf-color-555 lf-font-28" v-once>{{item.field}}</text>
				<input type="text" v-if="item.type == 'text'" v-model="info[item.field]" @blur="computedAge()" class="lf-color-999 lf-font-28 lf-text-left" style="position:absolute;right:180rpx" :placeholder="`请输入`+item.field"/>
				<view class="lf-m-t-20" v-if="item.type == 'radio'">
					<u-radio-group v-model="info[item.field]" @change="radioGroupChange">
						<u-radio 
							@change="radioChange" 
							v-for="(item2, index2) in item.options" :key="index2" 
							:name="item2"
						>
							{{item2}}
						</u-radio>
					</u-radio-group>
				</view>
				
				<view class="lf-m-t-20" v-if="item.type == 'checkbox'">
					<u-checkbox-group @change="checkboxGroupChange()">
						<u-checkbox 
							@change="checkboxChange(item.field)" 
							shape="circle"
							v-model="item2.checked" 
							v-for="(item2, index2) in item.options" :key="index2" 
							:name="item2.name"
						>{{item2.name}}</u-checkbox>
					</u-checkbox-group>
				</view>
				
				<view class="lf-m-t-20" v-if="item.type == 'datetime'" @click="showDateTime = true">
					<text>{{ info[item.field] || '请选择日期' }}</text>
					<u-picker mode="time" v-model="showDateTime" :params="dateTimeParams" @confirm="dateTimeConfirm($event, item.field)"></u-picker>
				</view>
				
				<view class="lf-m-t-20" v-if="item.type == 'textarea'">
					<view class="cu-self menu" style="margin-top: 30rpx; position: relative;">
						<textarea :cursor-spacing="120" maxlength="300" :adjust-position="true" type="text" v-model="info[item.field]" style="background-color: #F8F8F8;border-radius: 20rpx;" class="text-left lf-font-28 area-self lf-w-100 lf-p-20" placeholder="请输入内容" />
					</view>
				</view>
				
				<view class="grid col-3 grid-square flex-sub lf-m-t-30" v-if="item.type == 'image'">
					<view style="width: 212rpx;height: 212rpx;border-radius: 10rpx;" class="bg-img" v-for="(item,index) in img_list" :key="index" @tap="showImg(index)" :data-url="img_list[index]">
					 <image :src="img_list[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg(index)" :data-index="index">
							<!-- <text class='cuIcon-close'></text> -->
							<text class="lf-iconfont lf-icon-cuowu lf-font-40 lf-color-price"></text>
						</view>
					</view>
					<view style="width: 212rpx;height: 212rpx;border-radius: 10rpx;" class="solids" @tap="ChooseImage(item.field)" v-if="img_list.length<3">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
		</view>
		<self-line/>
		<view class="lf-bg-white lf-p-t-30 lf-p-b-30 lf-p-l-32 lf-p-r-32">
			<view class="lf-row-between">
				<image :src="goods_details.picture" mode="aspectFill" style="width: 240rpx; height: 240rpx;border-radius: 20rpx;"></image>
				<view class="flex-sub padding-left-sm">
					<view class="bref-box lf-font-32 lf-color-333 lf-line-2" style="height: 88rpx;line-height: 44rpx;">
						{{goods_details.title}}
					</view>
					<view class="lf-row-between">
						<view class="flex lf-m-t-25 align-center text-center">
							<text class="block lf-color-gray lf-font-24" style="line-height: 40rpx;">数量</text>
							<text class="lf-m-l-10 lf-color-gray lf-font-24">x {{buy_num}}</text>
						</view>
						<view>
							<u-number-box :min="1" v-model="buy_num" @change="valChange"></u-number-box>
						</view>
					</view>
					<view class="flex align-center text-center lf-m-t-25">
						<lf-price :price="goods_details.price" /> 
						<view class="lf-m-l-20 lf-line-through lf-color-gray">
							{{goods_details.original_price}}
						</view>
					</view>
				</view>
			</view>
		</view>
	<!-- 	<self-line/>
		<view class="bg-white">
			<view class="cu-bar padding-lr">
				<text class="lf-color-555 lf-font-28">优惠</text>
				<view v-if="goods_details.coupon != ''">
					<text class="lf-color-price lf-font-28 lf-m-l-10" v-for="(item,index) of goods_details.coupon">{{item.tag}}</text>
				</view>
				<view v-else>
					<text class="lf-color-price lf-font-28 lf-m-l-10">暂无优惠</text>
				</view>
			</view>
		</view> -->
		<self-line/>
		<view class="bg-white" v-if="enter_type!=1">
			<!-- 酒店 -->
			<view v-if="goods_type == 1">
				<view class="cu-bar padding-lr">
					<text class="lf-color-555 lf-font-28">套餐类型</text>
					<text class="lf-font-28" style="color: #131315;">{{goods_typetext}}</text>
				</view>
				<view class="cu-bar padding-lr">
					<text class="lf-color-555 lf-font-28">酒店住房日期</text>
					<text class="lf-font-28" style="color: #131315;">{{check_in_time}}~{{check_out_time}} <text style="color: #131315;" class="lf-font-28 lf-m-l-20">共{{totalLive}}晚</text></text>
				</view>
				<view class="cu-bar padding-lr">
					<text class="lf-color-555 lf-font-28">到店时间</text>
					<text class="lf-font-28" style="color: #131315;">{{arrival_time}}</text>
				</view>
			</view>
			<!-- 旅游路线 -->
			<view v-if="goods_type == 0 || goods_type == 4 || goods_type == 5 || goods_type == 2">
				<view class="cu-bar padding-lr">
					<text class="lf-color-555 lf-font-28">套餐类型</text>
					<text class="lf-font-28" style="color: #131315;">{{goods_typetext}}</text>
				</view>
				<view class="cu-bar padding-lr">
					<text class="lf-color-555 lf-font-28">开始时间</text>
					<text class="lf-font-28" style="color: #131315;">{{departure_time}}</text>
				</view>
			</view>
		</view>
		<self-line/>
		<view class="bg-white" v-if="enter_type!=1">
			<view class="lf-p-t-30 lf-p-l-32 lf-p-b-36">
				<view class="lf-font-28 lf-color-555">支付方式</view>
			</view>
			
			<view class="lf-p-r-32 lf-p-l-32">
				<button class="cu-btn bg-white margin-self" :class="pay_type==0?'border-green':'border'" @tap="payStatus(0)">
					<text class="lf-iconfont lf-icon-weixin text-green lf-font-44 lf-m-r-23"></text>
					<text class="lf-font-32" :class="pay_type==0?'text-green':'lf-color-333'">微信支付</text>
					<text class="lf-iconfont lf-icon-dui lf-font-44 lf-color-primary" v-if="pay_type==0" style="position: absolute;right: 22rpx;"></text>
				</button>
			</view>
			
			<view class="lf-p-r-32 lf-p-l-32 lf-m-t-30">
				<button class="cu-btn bg-white margin-self" :class="pay_type==1?'border-green':'border'" @tap="payStatus(1)">
					<text class="lf-font-32" :class="pay_type==1?'text-green':'lf-color-333'">线下支付</text>
					<text class="lf-iconfont lf-icon-dui lf-font-44 lf-color-primary" v-if="pay_type==1" style="position: absolute;right: 22rpx;"></text>
				</button>
			</view>
			
			<view class="lf-p-r-32 lf-p-l-32 lf-m-t-30" v-if="goods_details.deposit != '0.00'">
				<button class="cu-btn bg-white margin-self" :class="pay_type==2?'border-green':'border'" @tap="payStatus(2)">
					<text class="lf-font-32" :class="pay_type==2?'text-green':'lf-color-333'">订金支付</text>
					<text class="lf-iconfont lf-icon-dui lf-font-44 lf-color-primary" v-if="pay_type==2" style="position: absolute;right: 22rpx;"></text>
				</button>
			</view>
			
			<view class="lf-p-r-32 lf-p-l-32 lf-m-t-30">
				<button class="cu-btn bg-white margin-self" v-if="goods_details.earnest != '0.00'"  :class="pay_type==3?'border-green':'border'" @tap="payStatus(3)">
					<text class="lf-font-32" :class="pay_type==3?'text-green':'lf-color-333'">定金支付</text>
					<text class="lf-iconfont lf-icon-dui lf-font-44 lf-color-primary" v-if="pay_type==3" style="position: absolute;right: 22rpx;"></text>
				</button>
			</view>
			
			<!-- <view class="lf-p-r-32 lf-p-l-32 lf-m-t-30">
				<button class="cu-btn bg-white margin-self" :class="pay_type==4?'border-green':'border'" @tap="payStatus(4)">
					<text class="lf-font-32" :class="pay_type==4?'text-green':'lf-color-333'">首款支付</text>
					<text class="lf-iconfont lf-icon-dui lf-font-44 lf-color-primary" v-if="pay_type==4" style="position: absolute;right: 22rpx;"></text>
				</button>
			</view> -->
		</view>
		
		<view style="height: 200rpx;"></view>
		<view class="btn-bottom solid-top1">
			<view class="padding-lr lf-p-t-10 lf-p-b-10  bg-white flex justify-between align-center shadow">
				<view class="flex align-center">
					<text class="lf-font-24 lf-font-555">应付款：</text>
					<lf-price :price="goods_details.final_price" /> 
				</view>
				
				<button class="btn" @tap="submit">
					<text class="lf-font-32 text-white" v-if="btn_type == 1">下单付款</text>
					<text class="lf-font-32 text-white" v-else>支付尾款</text>
				</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goods_id: 0,
				goods_details: {},
				contact: '',
				phone: '',
				pay_type: 0,
				btn_type: 1,
				ifPay: true,
				order_id: 0,
				enter_type: 0,
				payState: true,
				pay_order_id: 0,
				vertify_code: '',
				sex: '',
				age: '',
				goods_type: 0,
				spec_id: 0,
				goods_typetext: '',
				//景区
				enter_time: '',
				//旅游路线
				departure_time: '',
				return_time: '',
				//酒店
				check_in_time: '',
				check_out_time: '',
				arrival_time: '',
				totalLive: '',
				hotel_specid: [],
				//多行文本
				applyInfo: '',
				//图片上传
				hostImg: '',
				img_list: [], // 反馈问题图片
				form_type: '',
				form_list: [],
				info: {},
				image_name: '',
				if_image_complatet: false,
				check_box_name: '',
				buy_num: 0,
				showDateTime: false ,// 日期时间选择器是否显示
				dateTimeParams: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: false
				}
			}
		},
		onLoad(e) {
			//0旅游路线1酒店2景区3餐厅
			this.goods_id = e.goods_id;
			this.goods_type = e.goods_type;
			this.btn_type = e.btn_type;
			this.buy_num = e.buy_num;
			console.log('出来的数量',this.buy_num)
			this.order_id = e.order_id;
			this.form_type = JSON.parse(decodeURIComponent(e.form_type));
			// 修复原先checkbox数组格式错误导致报错
			let form_list = this.form_type.fields.map(item => {
				if(item.type == 'checkbox'){
					item.options = item.options.map(o => {
						return {name: o, checked: false};
					})
				}
				return item;
			})
			
			this.form_list = form_list;
			console.log('表单格式',this.form_list)
			var arr = e.spec_id.split(",")
			console.log(arr)
			this.hotel_specid = arr
			console.log('数组id',this.hotel_specid)
			this.enter_type = e.enter_type;
			this.goods_typetext = e.goods_typetext;
			if(this.goods_id) {
				this.getGoodsData(this.pay_type);
				if(this.goods_type == 0 || this.goods_type == 4 || this.goods_type == 5 || this.goods_type == 2) {
					this.departure_time = e.departure_time;
				}else if(this.goods_type == 1) {
					this.check_in_time = e.check_in_time;
					this.check_out_time = e.check_out_time;
					this.arrival_time = e.arrival_time;
					this.totalLive = e.totalLive;
					console.log(this.check_in_time,this.check_out_time,this.arrival_time)
				}
			}
			if(this.order_id && this.enter_type == 1) {
				console.log('con列表执行')
				this.payOnce()
			}
		},
		onShow(e) {
			let that = this
			uni.$on('payState',(data) => {
				that.payState = data.msg;
				that.pay_order_id = data.payorder_id;
				console.log('传来的订单id',that.pay_order_id)
				if(that.payState == true) {
					that.$url('/pages/payState/paystate?ifSuccess=1&order_id='+that.pay_order_id,{type: 'redirect'})
				}else {
					that.$url('/pages/payState/paystate?ifSuccess=2&order_id='+that.pay_order_id,{type: 'redirect'})
				}
			})
		},
		onUnload() {
			uni.$off('payState');
		},
		methods: {
			// 日期时间选择完成
			dateTimeConfirm(event, info_key){
				let { year, month, day, hour, minute } = event;
				this.info[info_key] = `${year}-${month}-${day} ${hour}:${minute}`;
			},
			valChange(e) {
				this.buy_num = e.value;
				this.getGoodsData(this.pay_type);
			},
			dynamicLength(){
				return parseInt(this.applyInfo.length);
			},
			//多选
			checkboxGroupChange(e) {
				this.info[this.check_box_name] = e
				console.log(this.info)
	
			},
			// 选中某个复选框时，由checkbox时触发
			checkboxChange(name) {
				this.check_box_name = name
				console.log('chenckshushush',this.check_box_name)
			},
			//单选
			radioChange(e) {
				console.log(e);
			},
			// 选中任一radio时，由radio-group触发
			radioGroupChange(e) {
				console.log(e);
			},
			computedAge() {
				this.$check(this.vertify_code,'card');
				let judge_sex = this.vertify_code[16];
				let judge_birthday = this.vertify_code.substring(6,14);
				let now_date = new Date();
				let now_date1 = this.$shared.recordTime(now_date, '-', 'date').replace(/-/g, "");
				let age1 = now_date1-judge_birthday;
				this.age  = JSON.stringify(age1).substring(0,2)
				if(judge_sex%2) {
					this.sex = '男';
				}else {
					this.sex = '女';
				}
				if(this.vertify_code == '') {
					this.sex = '请输入身份证号码获取';
					this.age = '请输入身份证号码获取';
				}
			},
			getPhoneNumber(event){
				if(event.detail.errMsg == 'getPhoneNumber:ok'){
					let encryptedData = event.detail.encryptedData;
					let iv = event.detail.iv;
					this.$http(this.API.API_WECHAT_SETPHONE, {
						encryptedData,
						iv,
						// token: userInfo.token // 已在公共参数传
					}).then(res => {
						this.phone = res.data.mobile;
					})
				}
			},
			payStatus(type) {
				if(type == this.pay_type) {
					this.pay_type = type;
					return
				}else {
					this.pay_type = type;
					this.getGoodsData(type);
				}
			},
			getGoodsData(type){
				this.$http(this.API.API_CONFIRMDETAILS,{id: this.goods_id,num: this.buy_num,pay_type: type,spec_id: this.hotel_specid}).then(res => {
					this.goods_details = res.data;
					console.log('数据',res)
				})
			},
			submit(url) {
				if(this.order_id && this.enter_type == 1) {
					this.payOnce()
					return
				}
				this.upload()
				if(!this.ifPay) return;
				this.ifPay = false;
				this.$http(this.API.API_CREATEORDER,{
						id: this.goods_id,
						num: this.buy_num,
						pay_type: this.pay_type,
						name:this.contact,
						mobile:this.phone,
						spec_id: this.hotel_specid,
						enter_time: this.enter_time,
						sex: this.sex,
						age: this.age,
						id_card: this.vertify_code,
						departure_time: this.departure_time,
						return_time: this.return_time,
						check_in_time: this.check_in_time,
						check_out_time: this.check_out_time,
						arrival_time: this.arrival_time,
						info: this.info
					}).then(res => {
					this.pay_order_id = res.data.id
					
					if(this.pay_type == 1) {
						this.ifPay = true
						this.$url('/pages/payState/paystate?ifSuccess=1&order_id='+res.data.id,{type: 'redirect'})
					}else if(res.data.jump == true) {
						let that = this;
						wx.navigateToMiniProgram({
							appId: res.data.jump_appid,
							path: '/pages/order/confirm_atonce',
							envVersion: 'release',
							extraData: {
								order_id: res.data.id
							},
							success(res) {
								console.log('---res---');
								that.ifPay = true;
								console.log(res);
							},
							fail(err) {
								console.log('---err---');
								console.log(err);
								that.ifPay = true;
							}
						})
					}
					
					}).catch(err => {
						this.ifPay = true
					})
			},
			checkImgInfo(tempFilePath, suc){
				uni.getImageInfo({
					src: tempFilePath,
					success (res) {
						let type = res.type;
						console.log('checkImgInfo...', type);
						if(type == 'png' || type == 'jpeg' || type == 'jpg'){
							suc && suc(true);
						} else {
							suc && suc(false);
						}
					},
					fail(err) {
						suc && suc(false);
					}
				})
			},
			// 选择图片
			ChooseImage(name,e) {
				let that = this;
				uni.chooseImage({
					count: 1,
					sizeType: ['original'], // 可以指定是原图original还是压缩图compressed ，默认二者都有
					sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
					success: res => {
						that.hostImg = res.tempFilePaths[0];
						let tempFile = res.tempFiles.shift();
						let tempFilePath = res.tempFilePaths.shift();
						console.log('临时路径',res)
						that.checkImgInfo(tempFilePath, (res) => {
							// 过滤不是图片不是png、jpeg 格式
							if(res){
								if(tempFile.size > 10000000){
									uni.showModal({
										title: '',
										content: '您选择的图片过大：'+ (tempFile.size / 1024000).toFixed(2) +"M，请点击确定重新上传",
										success: res2 => {
											if(res2.confirm){
												uni.chooseImage({
													count: 1,
													sizeType: ['compressed'],
													sourceType: ['album', 'camera'],
													success: (res3) => {
														let tempFilePath = res3.tempFilePaths.shift();
														that.is_wx_reduce = true;
														that.img_list.push(tempFilePath);
													}
												})
											}
										}
									})
								} else{
									that.img_list.push(tempFilePath);
									console.log('图片数sadad组',name)
									that.image_name = name
									this.upload()
								}
							} else {
								uni.showModal({
									title: '',
									content: '选择的图片须为jpg、jpeg或png格式',
									showCancel: false,
									confirmColor: '#1697EE'
								})
							}
						});
					}
				});
			},
			
			// 上传图片到服务器
			upload(url){
				let that = this;
				let uploads = [];
				// 商品banner图上传
				if (that.img_list.length > 0) {
					for (let i = 0; i < that.img_list.length; i++) {
						let upload_img = new Promise((resolve, reject) => {
							that.uploadFile(that.img_list[i], (res) => {
								resolve(res);
							}, (err) => {
								reject(err);
							});	
						})
						uploads.push(upload_img);
					}
				}
				if(uploads.length == 0) {
					// that.submit([]);
					return
				}
				Promise.all(uploads).then((result) => {
					let img_url_list = [];
					if(result.length > 0){
						img_url_list = result;
					}
					if(img_url_list) {
						console.log('传来的名字',that.image_name)
						that.info[that.image_name] = img_url_list;
						console.log('=====图片数组',that.info)
					}
				}).catch(err => {
					that.is_publish = false; // 恢复提交按钮
					uni.showModal({
						title: '',
						content: '图片上传失败，请重新提交',
						confirmColor: '#1697EE'
					})
				})
			},
			// 预览图片
			showImg(index) {
				this.$u.throttle(() => {
					let goods_banner = this.img_list || [];
					let banners = goods_banner.map(item => item);
					uni.previewImage({
						urls: banners,
						current: index
					})
				}, 200);
			},
			
			// 删除图片
			DelImg(index) {
				uni.showModal({
					title: '提示',
					content: '即将取消上传这张图片，请确认？',
					success: e => {
						if (!e.confirm) return;
						this.img_list.splice(index, 1);
						this.upload()
					}
				});
			},
			payOnce() {
				this.$http(this.API.API_ORDERPAY,{id: this.order_id}).then(res => {
					if(res.data.jump) {
						wx.navigateToMiniProgram({
							appId: res.data.jump_appid,
							path: '/pages/order/confirm_atonce',
							envVersion: 'release',
							extraData: {
								order_id: res.data.id
							},
							success(res) {
								console.log('---res---')
								console.log(res)
							},
							fail(err) {
								console.log('---err---')
								console.log(err)
							}
						})
					}
				}).catch(err => {})
			}
		}
	}
</script>

<style scoped lang="scss">
	.cu-bar1 {
		display: flex;
		position: relative;
		align-items: center;
		min-height: 100rpx;
	}
	.btn{
		margin: 0;
		padding: 0;
		width: 212rpx;
		height: 82rpx;
		background-color: #1998FE;
		color: #FFFFFF;
		line-height: 80rpx;
		font-size: 32rpx;
		border-radius: 41rpx;
	}
	.bref-box {
		text-overflow: -o-ellipsis-lastline;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}
	.font-change{
		position: absolute;
		right: 10rpx;
		bottom: 10rpx;
		width: max-content;
		color: #777777;
	}
</style>
