<template>
	<view class="page">
		<navBar :backType="navBarTitle" :title="navBarTitle?navBarTitle:'定制我家的无主灯'" :back="true" color="black"
			background="white" />
		<image class="topImg" :src="picBase + topPic" mode=""></image>

		<view class="cen">
			<image class="water" src="/static/index/water.png" mode=""></image>
			<image class="halfC" src="/static/index/halfCircle_275.png" mode=""></image>
			定制我家的无主灯
		</view>

		<view class="parmasBox">
			<view class="con">
				<view class="top">
					<view class="item">
						<view class="name">
							户型
						</view>
						<view class="des">
							{{selHx.value}}
						</view>
					</view>
					<view class="item">
						<view class="name">
							空间
						</view>
						<view class="des">
							{{selKj.value}}
						</view>
					</view>
					<view class="item">
						<view class="name">
							品牌
						</view>
						<view class="des">
							{{selPp.value}}
						</view>
					</view>
					<view class="item">
						<view class="name">
							系列
						</view>
						<view class="des">
							{{selXl.value}}
						</view>
					</view>
					<view class="item">
						<view class="name">
							版本
						</view>
						<view class="des">
							{{selBb.value}}
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="parmasBox" v-if="navBarTitle&&(shareCon.name||shareCon.phone||shareCon.address||shareCon.remark)">
			<view class="con">
				<view class="top">
					<view class="item" v-if="shareCon.name">
						<view class="name">
							收货人
						</view>
						<view class="des">
							{{shareCon.name}}
						</view>
					</view>
					<view class="item" v-if="shareCon.phone">
						<view class="name">
							电话
						</view>
						<view class="des">
							{{shareCon.phone}}
						</view>
					</view>
					<view class="item" v-if="shareCon.address">
						<view class="name">
							收货地址
						</view>
						<view class="des">
							{{shareCon.address}}
						</view>
					</view>
					<view class="item" v-if="shareCon.remark">
						<view class="name">
							备注
						</view>
						<view class="des">
							{{shareCon.remark}}
						</view>
					</view>
				</view>
			</view>
		</view>


		<!-- <image v-if="!navBarTitle" @click="back" src="/static/index/backBtnImg.png" mode="" class="rule_img"></image> -->
		<view class="bot" v-if="!navBarTitle">
			<!-- <view class="left" @click="showPreviewConfig=true"> -->
			<view class="left" @click="">
				<view class="price">
					￥{{totalPrice.toFixed(0)}}
				</view>
				<image v-if="!showPreviewConfig" src="/static/index/arrDown.png" mode=""
					@click="showPreviewConfig=true"></image>
				<image v-else src="/static/index/arrUp.png" mode="" @click="showPreviewConfig=false"></image>
			</view>


			<view class="right">
				<button open-type="share" v-if="orderSuc" class="btn" :plain="true">
					分享
				</button>
				<view class=" btn" v-else @click="showFixInfo=true">
					确认配置
				</view>
				<view class="btn" v-if="!navBarTitle" @click="back" style="margin-left: 20rpx;">
					返回
				</view>
			</view>
		</view>




		<u-popup :show="showFixInfo" @close="close" @open="open" round='10'>
			<view class="topBar" style="">
				<view class="left">

				</view>
				<view class="cen">
					信息填写
				</view>
				<view class="close" @click="showFixInfo=false">
					<u-icon name="close"></u-icon>
				</view>
			</view>
			<view class="pupopCon">
				<view class="item">
					<view class="label">
						收货人
					</view>
					<u--input clearable placeholder="请输入收货人" border="surround" v-model="name"
						@change="change"></u--input>
				</view>
				<view class="item">
					<view class="label">
						电话
					</view>
					<u--input clearable placeholder="请输入电话" border="surround" v-model="phone"
						@change="change"></u--input>
				</view>
				<view class="item" style="margin-bottom: 24rpx;height: auto;">
					<view class="label">
						收货地址
					</view>
					<u--textarea v-model="addr" placeholder="请输入收货地址"></u--textarea>
				</view>

				<view class="item" style="margin-bottom: 64rpx;">
					<view class="label">
						备注
					</view>
					<u--input clearable placeholder="请输入备注" border="surround" v-model="remark"
						@change="change"></u--input>
				</view>

				<button open-type="share" class="orderBtn" :plain="true">
					确认下单
				</button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		postOrderReq,

		getOrderDetailReq
	} from '@/api/test/index.js'

	import {
		getBannerDataReq,
	} from '@/api/index/index.js'
	export default {
		components: {

		},
		data() {
			return {
				picUrl: this.$picUrl,
				picBase: this.$picBase,

				showFixInfo: false,

				showPreviewConfig: false,

				selHx: null,
				selKj: null,
				selPp: null,
				selXl: null,
				selBb: null,

				totalPrice: 0,

				name: '',
				phone: '',
				addr: '',
				remark: '',

				orderId: '',
				pConfig: -1,

				shareCon: null,
				navBarTitle: '',
				orderSuc: false,
				topPic: ''
			}
		},
		async onLoad(o) {
			let res11 = await getBannerDataReq()
			if (res11.code == 0) {
				console.log('1-----图片配置数据', res11);

				this.topPic = res11.data.find((item, index) => {
					return item.title == '无主灯'
				}).head_image
			} else {
				this.$toast(res11.message)
			}



			// o.orderId = '2061b7c74224ad067b2d0f1ab8c3f351'
			if (o.orderId) {
				this.orderId = o.orderId
				this.navBarTitle = '首页'
				console.log('o.orderId', o.orderId);

				let res = await getOrderDetailReq({
					orderId: o.orderId
				})
				if (res.code == 0) {
					console.log('邀请订单详情返回值-无主灯: ', res);

					this.shareCon = res.data

					this.selHx = res.data.config.house_type_data
					this.selKj = res.data.config.space_data
					this.selPp = res.data.config.brand_data
					this.selXl = res.data.config.series_data
					this.selBb = res.data.config.version_data
				} else {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				}
			} else {
				this.selHx = getApp().selHx
				this.selKj = getApp().selKj
				this.selPp = getApp().selPp
				this.selXl = getApp().selXl
				this.selBb = getApp().selBb

				this.pConfig = getApp().pConfig

				this.totalPrice = getApp().totalPrice
			}
		},
		async onShareAppMessage(params) {
			if (params.from === 'button' || params.from === 'menu') {
				if (params.from === 'button' || params.from === 'menu') {
					let phoneReg = /^[1][3,4,5,7,8,9][0-9]{9}$/
					// if (!this.name) {
					// 	return this.$toast('请填写收货人')
					// }
					// if (!this.phone) {
					// 	return this.$toast('请填写电话')
					// }
					if (this.phone && !phoneReg.test(this.phone)) {
						return this.$toast('手机号格式不正确')
					}
					// if (!this.addr) {
					// 	return this.$toast('请填写收货地址')
					// }
					// if (!this.remark) {
					// 	return this.$toast('请填写备注')
					// }
					let res = await postOrderReq({
						address: this.addr,
						phone: this.phone,
						name: this.name,
						remark: this.remark,
						config_id: this.pConfig,
						type: 1
					})
					if (res.code == 0) {
						console.log('提交订单返回值: ', res);
						this.orderId = res.data.orderId
						this.showFixInfo = false

						return {
							title: '定制我家的无主灯',
							imageUrl: this.picBase + this.topPic,
							path: '/pages/index/customNoMian_step2/index?orderId=' + this.orderId,
							mpId: this.$appId,
							type: this.$shareType,
						}

					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
				}


				// return {
				// 	title: '定制我家的无主灯',
				// 	imageUrl: this.picUrl + '/static/other/noMain_img4.png',
				// 	path: '/pages/index/customNoMian_step2/index?orderId=' + this.orderId,
				// 	mpId: this.$appId,
				// 	type: this.$shareType,
				// }
			}
		},
		methods: {
			back() {
				uni.navigateBack()
			},
			async postOrder() {
				let phoneReg = /^[1][3,4,5,7,8,9][0-9]{9}$/
				if (!this.name) {
					return this.$toast('请填写收货人')
				}
				if (!this.phone) {
					return this.$toast('请填写电话')
				}
				if (!phoneReg.test(this.phone)) {
					return this.$toast('手机号格式不正确')
				}
				if (!this.addr) {
					return this.$toast('请填写收货地址')
				}
				if (!this.remark) {
					return this.$toast('请填写备注')
				}

				console.log('无主灯下单参数', {
					address: this.addr,
					phone: this.phone,
					name: this.name,
					remark: this.remark,
					// config_id: this.selHx.id + ',' + this.selKj.id + ',' + this.selPp.id + ',' + this.selXl
					// 	.id + ',' + this.selBb.id,
					config_id: this.pConfig,
					type: 1
					// lamp_id: '2,4,6'
				});
				let res = await postOrderReq({
					address: this.addr,
					phone: this.phone,
					name: this.name,
					remark: this.remark,
					config_id: this.pConfig,
					type: 1
					// lamp_id: '2,4,6'
				})
				if (res.code == 0) {
					console.log('提交订单返回值: ', res);
					this.$toast('提交成功')

					this.orderId = res.data.orderId
					this.showFixInfo = false

					this.orderSuc = true
				} else {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import "./index.scss";
</style>