<template>
	<page-meta :page-style="'overflow:'+(showinfo?'hidden':'visible')"></page-meta>
	<view class="cotainer">
		<view class="orderInfo">
			<view class="orderInfo-title">
				<view>{{cardInfo.sku?cardInfo.sku.name:''}}</view>
			</view>
			<view class="orderInfo-info">
				<image :src="require('@/static/share.jpg')"></image>
				<view>
					<view>
						{{cardInfo.sku?cardInfo.sku.specs:''}}
					</view>
					<view>
						<view>今日剩余可提货量</view>
						<view>99</view>
					</view>
					<view>
						<view>卡券状态</view>
						<view class="status" v-html="cardInfo.status_label"></view>
					</view>

				</view>
			</view>
			<view class="orderInfo-status">
				<view>提货时间</view>
				<view @click="openDatePicker">
					<view class="">
						<view>顺丰快递</view>
						<view>9月28日 [周三] 09:00-15:00</view>
					</view>
					<view>
						<u-icon name="arrow-down-fill" color="#ccc" size='28'></u-icon>
					</view>
				</view>
			</view>
		</view>
		<date-picker ref="datePickers" :data="listDates" :express="listExpress"></date-picker>


		<view class="orderForm">
			<u-form :model="form" ref="uForm" :error-type="['border-bottom']">
				<!-- <u-form-item label="预约提货时间" required prop="send_date" label-position="top">
					<u-input v-model="form.send_date" placeholder="请选择预约提货时间" @click="showCalendar = true"
						type="hidden" />
				</u-form-item> -->
				<view class="orderForm-wrap">
					<u-form-item label="收货人" label-width="180" prop="contact" label-position="left">
						<u-input v-model="form.contact" placeholder="请填写收货人姓名" />
					</u-form-item>
					<u-form-item label="手机号码" label-width="180" prop="mobile" label-position="left">
						<u-input v-model="form.mobile" placeholder="请填写手机号码" />
					</u-form-item>
					<u-form-item label="备用号码" label-width="180" prop="mobile2" label-position="left">
						<u-input v-model="form.mobile2" placeholder="请填写备用号码" />
					</u-form-item>
					<u-form-item label="所在地区" prop="area" label-width="180" label-position="left">
						<u-input v-model="form.area" placeholder="请选择所在地区" @click="openArea" />
						<view slot="right" class="arearight" @click="changeLocation">
							<u-icon name="map"></u-icon>
							定位
						</view>
					</u-form-item>
					<u-form-item label="详细地址" label-width="180" prop="address" label-position="left">
						<u-input v-model="form.address" placeholder="请填写详细地址" />
					</u-form-item>
					<u-form-item label="设置默认地址" :border-bottom="false" label-width="180" prop="checked"
						label-position="left">
						<u-switch slot="right" v-model="form.checked" active-color="#ba8b45" inactive-color="#adadac"
							size="40"></u-switch>
					</u-form-item>
				</view>
				<!-- 				<view class="orderForm-address">
					<text><text>*</text>收货地址</text>
					<u-button @click="changeLocation" size="mini">获取位置</u-button>
				</view> -->
				<view class="orderForm-wrap">
					<u-form-item label="是否本人收货" label-width="180" prop="myself" label-position="left">
						<u-input v-model="myself" placeholder="选填" @click="showIsMySelf = true" type="select" />
					</u-form-item>
					<u-form-item label="购买人姓名" label-width="180" prop="buy_name" label-position="left">
						<u-input v-model="form.buy_name" placeholder="选填" />
					</u-form-item>
					<u-form-item label="购买人电话" :border-bottom="false" label-width="180" prop="buy_mobile"
						label-position="left">
						<u-input v-model="form.buy_mobile" placeholder="选填" />
					</u-form-item>
				</view>

			</u-form>
			<view class="orderForm-btn">
				<view @click="submit">点击提货</view>
			</view>
		</view>
		
		<!-- <u-calendar max-date="2050-12-31" v-model="showCalendar" @change="changeDate" :mode="'date'"></u-calendar> -->
		<!-- <u-picker mode="region" v-model="showArea" @confirm="changeArea"></u-picker> -->
		<aui-picker ref="pickers" :title="'地区选择'" :data="listArea" @callback="changeArea"></aui-picker>

		<u-select @confirm="changeDate" v-model="showCalendar" :list="listDates"></u-select>

		<u-select @confirm="changeIsMySelf" v-model="showIsMySelf" :list="listIsMySelf"></u-select>
		<!-- 确认信息 -->
		<view class="showinfo" v-if="showinfo">
			<view class="content">
				<view class="showinfo-info">
					<view>您提货的商品是</view>
					<u-image :src="require('@/static/share.jpg')" width="450" height="450"></u-image>
					<view>{{cardInfo.sku?cardInfo.sku.name:''}}{{cardInfo.sku?cardInfo.sku.specs:''}}</view>
				</view>
				<view class="showinfo-wrap">
					<view>发货时间</view>
					<view style="text-align: center;">{{momentDay(date)}}</view>
				</view>
				<view class="showinfo-wrap">
					<view>收货信息</view>
					<view>
						<view>
							<text>收货人：</text>
							<text>{{form.contact}}</text>
						</view>
						<view>
							<text>收货人电话：</text>
							<text>{{form.mobile}}</text>
						</view>
						<view>
							<text>收货地址：</text>
							<text>{{form.area}}{{form.address}}</text>
						</view>
					</view>
				</view>
				<view class="showinfo-btn">
					<u-button @click="showinfo = false">返回修改</u-button>
					<u-button type="primary" @click="submit">确认提货</u-button>
				</view>
			</view>
		</view>
		<!-- 选择地址 -->
		<view class="addressinfo" v-if="showAddress">
			<view class="content">
				<view class="content-close" @click="showAddress=false">
					<u-icon name="close-circle" color="#000" size="50"></u-icon>
				</view>
				<view class="content-wrap">

					<u-radio-group v-model="addressInfo" @change="changeAddress">
						<u-radio v-for="(item, index) in listAddress" :key="index" :name="item.id">
							<view class="content-contact">{{item.contact}} {{item.mobile}}</view>
							<view class="content-address">{{item.area}}{{item.address}}</view>
						</u-radio>
					</u-radio-group>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import {
		toast,
		isMobile,
		isNull
	} from '@/common/util.js'
	import solarLunar from '@/node_modules/solarlunar';
	import auiPicker from '@/components/aui-picker.vue';
	import datePicker from '@/components/date-picker/date-picker.vue'
	export default {
		components: {
			auiPicker,
			datePicker
		},
		data() {
			return {
				showinfo: false,
				showAddress: false,
				listAddress: [],
				addressInfo: '',
				cardInfo: {},
				form: {
					card_number: '',
					password: '',
					send_date: '',
					area: '',
					address: '',
					province_id: '',
					city_id: '',
					district_id: '',
					contact: '',
					mobile: '',
					mobile2: '',
					checked: false,
					myself: 1,
					buy_name: '',
					buy_mobile: ''
				},
				listExpress:[{
					id:0,
					value:'顺丰快递'
				},{
					id:1,
					value:'中通快递'
				}],
				listDates: [],
				showCalendar: false,
				date: '',
				listArea: [],
				showArea: false,
				showIsMySelf: false,
				myself: '是',
				listIsMySelf: [{
					value: 1,
					label: '是'
				}, {
					value: 0,
					label: '否'
				}],
				rules: {
					send_date: [{
						required: true,
						message: '请选择预约提货时间',
						trigger: ['change', 'blur'],
					}],
					area: [{
						required: true,
						message: '请选择收货地址',
						trigger: ['change', 'blur'],
					}],
					address: [{
						required: true,
						message: '请填写详细地址',
						trigger: ['blur'],
					}],
					contact: [{
						required: true,
						message: '请填写收货人姓名',
						trigger: ['blur'],
					}],
					mobile: [{
						required: true,
						message: '请填写收货人电话',
						trigger: ['blur'],
					}, {
						validator: (rule, value, callback) => {
							return this.$u.test.mobile(value);
						},
						message: '手机号码不正确',
						trigger: ['blur'],
					}]
				}
			}
		},
		onReady() {
			// this.$refs.uForm.setRules(this.rules);

		},
		onLoad(option) {
			if (option.hasCard) {
				this.cardInfo = uni.getStorageSync("vuex_card") ? uni.getStorageSync("vuex_card").card : {}
				this.form.card_number = uni.getStorageSync("vuex_card") ? uni.getStorageSync("vuex_card").card_number : ''
				this.form.password = uni.getStorageSync("vuex_card") ? uni.getStorageSync("vuex_card").password : ''
				// uni.removeStorageSync("vuex_card")
				this.getAddress()
				this.getDates()
				this.getArea()
			} else {
				toast("未查询到卡券信息")
				uni.removeStorageSync("vuex_card")
				uni.redirectTo({
					url: '/pages/change/change'
				})
			}
		},
		methods: {
			async getAddress() {
				const res = await this.$u.api.getUserAddress()
				this.listAddress = res.address
				this.showAddress = this.listAddress.length > 0 ? true : false
			},
			changeAddress(e) {
				console.log("e", e)
				if (e) {
					this.listAddress.map(item => {
						if (item.id == e) {
							this.form.address_id = item.id
							this.form.area = item.area
							this.form.address = item.address
							this.form.province_id = item.province_id
							this.form.city_id = item.city_id
							this.form.district_id = item.district_id
							this.form.contact = item.contact
							this.form.mobile = item.mobile
							this.showAddress = false
						}
					})
				}
			},
			
			async getDates() {
				await this.$u.api.getDates({
					card_number: this.form.card_number,
					password: this.form.password,
				}).then(res => {
					console.log("dare", res)
					let data = res.card
					if (data.open_dates) {
						for (var k in data.open_dates) {
							let _k = this.momentDay(k)
							this.listDates.push(_k)
						}
					}
					console.log("this.listDates",this.listDates)
				}).then(res => {

				})
			},
			momentDay(date) {
				if (date) {
					const weeks = ["周日", '周一', '周二', '周三', '周四', '周五', '周六', ]
					let dateArr = date.split("-")
					let ymd = this.$moment(date).format("M月DD日")
					let week = weeks[this.$moment(date).day()]
					const solar2lunarData = solarLunar.solar2lunar(dateArr[0], dateArr[1], dateArr[2]);
					return {
						ymd:ymd,
						week:week,
						lunarData:`农历${solar2lunarData.monthCn}${solar2lunarData.dayCn}`,
					}
				}
			
			},
			openDatePicker(){
				this.$refs.datePickers.show = true
			},
			changeDate(e) {
				console.log(e)
				if (e) {
					// this.form.send_date = e.result
					// this.date = e
					this.date = e[0].label
					this.form.send_date = e[0].value
				}
			},
			async getArea() {
				const res = await this.$u.api.getRegion()
				this.listArea = res.regions

				console.log(res)
				console.log(this.listArea)
			},
			openArea() {
				this.$refs.pickers.open().then(function() {
					console.log('picker打开');
				});
			},
			changeArea(e) {
				console.log(e)
				// return
				if (e) {
					let data = e.data
					let _this = this
					data.forEach(function(item, index) {
						_this.form.area += item.value + '   ';
					});
					this.form.province_id = data[0] ? data[0].id : ''
					this.form.city_id = data[1] ? data[1].id : ''
					this.form.district_id = data[2] ? data[2].id : ''
					console.log(this.form)
					// this.form.area = e.province.label + e.city.label + e.area.label
					// this.$u.api.matchRegion({
					// 	province: e.province.label,
					// 	city: e.city.label,
					// 	district: e.area.label
					// }).then(res => {
					// 	this.form.province_id = res.province_id ? res.province_id.id : ''
					// 	this.form.city_id = res.city_id ? res.city_id.id : ''
					// 	this.form.district_id = res.district_id ? res.district_id.id : ''
					// })
				}
			},
			changeIsMySelf(e) {
				console.log(e)
				if (e) {
					this.myself = e[0].label
					this.form.myself = e[0].value
				}
			},
			changeLocation() {
				console.log("location")
				let _this = this
				uni.authorize({
					scope: 'scope.userLocation',
					success: (res) => {
						console.log("authorize", res)

						uni.chooseLocation({
							success(res) {
								console.log("res", res)
								_this.form.area = res.address
								_this.form.address = res.name
							},
							fail(res) {
								console.log("fail", res)

							}
						})
					},
					fail(res) {
						console.log("authorize-fail", res)
					}
				})
			},
			submit() {
				if ((!isNull(this.form.mobile2) && this.form.mobile2.length > 0) || (!isNull(this.form.buy_mobile) && this
						.form.buy_mobile.length > 0)) {
					if (!isMobile(this.form.mobile2)) {
						toast('备用电话不正确')
						return
					}
					if (!isMobile(this.form.buy_mobile)) {
						toast('购买人电话不正确')
						return
					}
				}
				this.$refs.uForm.validate(valid => {
					console.log('验证通过');
					if (valid) {
						if (!this.showinfo) {
							this.showinfo = true
							console.log("this.showinfo", this.showinfo)
						} else {
							console.log('submit');
							this.form.show = 0
							this.$u.api.addUserOrder(this.form).then(res => {
								console.log("res", res)
								uni.removeStorageSync('vuex_card')
								toast(res.msg)
								setTimeout(function() {
									uni.redirectTo({
										url: '/packages/myorder/myorder'
									})
								}, 2000)

							}).then(res => {
								console.log('res1', res)

							})
						}

					} else {
						console.log('验证失败');
					}
				});
			}
		}

	}
</script>

<style lang="scss">
	.cotainer {
		padding: 29rpx 54rpx;
		background-color: #f0efed;
		padding-bottom: 200rpx;
		.addressinfo {
			position: fixed;
			top: 0;
			right: 0;
			bottom: 0;
			left: 0;
			background: rgba(0, 0, 0, .5);
			z-index: 9999999;
			display: flex;
			align-items: center;
			justify-content: center;

			.content {
				width: 80%;
				height: 600rpx;
				position: relative;

				&-wrap {
					box-sizing: border-box;
					background: #fff;
					border-radius: 20rpx;
					overflow: scroll;
					width: 100%;
					height: 100%;
					padding: 40rpx;
				}

				&-close {
					position: absolute;
					top: -20rpx;
					right: -20rpx;
				}

				.u-radio {
					align-items: baseline;
				}

				&-contact {}

				&-address {
					color: #ccc;
					line-height: 1.2;
					font-size: 28rpx;
				}
			}
		}

		.showinfo {
			position: fixed;
			top: 0;
			right: 0;
			bottom: 0;
			left: 0;
			background: rgba(0, 0, 0, .5);
			z-index: 9999999;
			display: flex;
			align-items: center;
			justify-content: center;

			.content {
				width: 100vw;
				height: 100vh;
				padding: 40rpx;
				box-sizing: border-box;
				background: #fff;
				// border-radius: 16rpx;
				overflow: scroll;
			}

			&-info {
				display: flex;
				flex-wrap: wrap;
				justify-content: center;

				>view {
					margin: 20rpx;
					font-size: 36rpx;

					&:last-child {
						width: 350rpx;
						color: #ccc;
						font-size: 28rpx;
						text-align: center;
					}

				}
			}

			&-wrap {
				>view {
					&:first-child {
						text-align: center;
						margin: 20rpx;
					}

					&:last-child {
						border: 1px solid #333;
						padding: 20rpx;
						border-radius: 15rpx;
						font-weight: bold;

						>view {
							margin-bottom: 10rpx;
						}
					}
				}
			}

			&-btn {
				display: flex;
				align-items: center;
				justify-content: center;
				margin: 30rpx;

				u-button {
					margin: 0 20rpx;
				}
			}
		}

		.orderInfo {
			padding: 40rpx 24rpx;
			border-radius: 10rpx;
			background-color: #fff;
			font-size: 24rpx;
			color: #000;

			&-title {

				>view {
					font-size: 24rpx;
					font-weight: bolder;
					margin-bottom: 20rpx;
				}
			}

			&-info {
				display: flex;

				font-weight: normal;
				margin-bottom: 40rpx;

				image {
					width: 150rpx;
					height: 150rpx;
					border: 1px solid #ba8b45;
					border-radius: 10rpx;
					margin-right: 20rpx;
				}

				>view {
					&:last-child {
						width: calc(100% - 170rpx);

						>view {
							display: flex;
							justify-content: space-between;
							margin-bottom: 20rpx;

							.status {
								background-color: #ba8b45;
								color: #fff;
								padding: 5rpx 20rpx;
								border-radius: 10rpx;
							}

						}
					}

				}
			}

			&-status {
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;

				>view {
					&:last-child {
						text-align: right;
						display: flex;
						align-items: center;

						>view {
							&:first-child {
								view {
									margin-bottom: 15rpx;

									&:last-child {
										margin-bottom: 0rpx
									}
								}
							}

							&:last-child {
								margin-left: 20rpx
							}
						}
					}
				}
			}
		}

		.orderForm {
			&-wrap {
				padding: 40rpx 24rpx;
				border-radius: 10rpx;
				background-color: #fff;
				font-size: 24rpx;
				color: #000;
				margin-top: 24rpx;

				.u-form-item {
					padding-bottom: 0
				}

				.arearight {
					color: #000
				}
			}

			&-address {
				height: 72rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				line-height: 72rpx;
				padding-top: 20rpx;
				position: relative;

				text {
					text {
						position: absolute;
						left: -16rpx;
						vertical-align: middle;
						color: #fa3534;
						padding-top: 6rpx;
					}
				}
			}

			&-btn {
				background: #fff;
				position: fixed;
				bottom: 0;
				left: 0;
				width: 100%;
				padding: 40rpx;
				z-index: 9;

				>view {
					background-color: #ba8b45;
					color: #fff;
					border-radius: 40rpx;
					width: 60%;
					margin: 0 auto;
					height: 75rpx;
					text-align: center;
					line-height: 75rpx;
				}
			}
		}
	}
</style>