<template>
	<view class="w-full">
		<send-type @sendTypeChange="sendTypeChange" />
		<view class="gap"></view>
		<!-- <view class="address">
			<u-cell-group>
				<u-cell title="添加收货地址" :border="false" url='' isLink>
					<u-icon slot="icon" size="24" name="plus"></u-icon>
				</u-cell>
			</u-cell-group>
		</view> -->
		<view class="padding-sm dflex-b bg-main" @click="show = true">
			<view class="dflex">
				<!-- <view class="addr-label">配送至</view> -->
				<view class="w-full dflex-wrap-w send-label">
					取餐时间
				</view>
			</view>
			<u-icon slot="icon" size="20" name="arrow-right" :label="value" labelPos="left"></u-icon>
			<!-- <view class="iconfont iconjiantou-01 fs-sm"></view> -->
		</view>
		<u-datetime-picker :show="show" v-model="value" mode="time" @confirm="onConfirm"
			@cancel="onCancel"></u-datetime-picker>
		<view class="gap"></view>
		<view class="padding-sm dflex-b bg-main">
			<view class="dflex">
				<!-- <view class="addr-label">配送至</view> -->
				<view class="w-full dflex-wrap-w send-label">
					取餐方式
				</view>
			</view>
			<view>
				<uni-data-checkbox v-model="val" :localdata="list" @change="radioChange"
					selectedColor="#F54319"></uni-data-checkbox>
				<!-- <radio style="margin: 0 10px; transform: scale(0.85)" value="r1" :checked="typePicking" color="#ff0000" class="radio">店内用餐
				</radio >
				<radio style="transform: scale(0.85)" value="r1" :checked="typePickings" color="#ff0000" class="radio">打包带走
				</radio> -->
			</view>
			<!-- <view class="iconfont iconjiantou-01 fs-sm"></view> -->
		</view>
		<view class="gap"></view>
		<view class="goods-order-list w-full padding-lr-sm">
			<view class="shop dflex-b ">
				<view class="shop-check">
					<text class="shop-name">{{cList.name}}</text>
				</view>
			</view>
			<view class="goods-cart w-full">
				<view class="goods-cont padding-tb" v-for="(item, index) in data.carlist" :key="index">
					<my-goods :item="item">
						<view class="good-num">×{{item.quantity}}</view>
					</my-goods>
				</view>

			</view>
		</view>
		<!-- 金额明细 -->
		<view class="gap"></view>
		<view class="bg-main">
			<view class="dflex-b padding-lr padding-tb-sm">
				<view class="flex1">总金额</view>
				<view class="price">{{data.total}}</view>
			</view>
			<!-- <view class="dflex-b padding-lr padding-tb-sm">
				<view class="flex1">运费 (总重:4.960 kg )</view>
				<view class="ft-base">￥10.00</view>
			</view>
			
			<view class="dflex-b padding-lr padding-tb-sm">
				<view class="margin-right-xl">备注</view>
				<input class="flex1 padding-sm" type="text" v-model="order_desc" placeholder="请填写买家备注"
					placeholder-class="placeholder" />
			</view> -->
		</view>
		<view class="gap"></view>
		<!-- <my-gap :height="130" /> -->
		<view class="submit-bar padding-sm dflex-b">
			<view class="submit-btn dflex-c background-gradient" @click="toBuy">立即下单</view>
		</view>
	</view>
</template>

<script>
	import {
		multiply
	} from '@/utils/accuracy.js'
	export default {
		data() {
			return {
				data: [],
				show: false,
				value: '',
				cList: {},
				val: null,
				vals: null,
				list: [{
						text: "店内用餐",
						value: 0
					},
					{
						text: "打包带走",
						value: 1
					}
				]
			}
		},
		onLoad(option) {
			const e = uni.getStorageSync('carl')
			this.cList = e
			console.log('233333333333333', e)
			// 解码 URL 编码的字符串
			var dataString = decodeURIComponent(option.data);
			// 尝试将字符串转换为 JSON 对象
			var data = JSON.parse(dataString);
			if(data.carlist[0].imgUrl.url) {
				data.carlist[0].imgUrl = data.carlist[0].imgUrl.url;
			}
			
			this.data = data
			// 打印解码后的数据
			console.log('eeeeeeeeeeeee============data', data);
		},
		methods: {
			sendTypeChange(v) {},
			radioChange(v) {
				console.log('2=================', v)
				console.log('2=================22222', this.val)
			},
			onConfirm(v) {
				console.log('vvvvvvvvvvvvvvv', v)
				this.value = v.value
				this.show = false
			},
			onCancel() {
				this.show = false
			},
			toBuy() {
				console.log('this.val', this.val)
				if (this.value == '') {
					uni.showToast({
						title: '请选择取餐时间',
						icon: 'error',
						duration: 1000
					})
					return
				}
				if (this.val == null && this.val == undefined && this.val == '') {
					uni.showToast({
						title: '请选择取餐方式',
						icon: 'error',
						duration: 1000
					})
					return
				}
				const userId = uni.getStorageSync('appUserId');
				const userName = uni.getStorageSync('userName');
				const itemListArray = this.data.carlist.map(item => ({
					skuId: item.skuId,
					gift: 1, // 假设所有商品都不是礼物
					basePrice: Number(item.basePrice),
					price: Number(item.price),
					quantity: item.quantity,
					sellerType: 'C', // 假设所有商品卖家类型都是 'C'
					spuId: item.spuId,
					skuName: item.skuName,
					title: item.skuTitle,
					picUrl: item.imgUrl,
					skuProperties: item.skuProperties,
					totalFee: multiply(Number(item.price), item.quantity),
					adjustFee: 0
				}))

				const params = {
					orderState: 1,
					orderType: 1,
					serviceType: 1,
					payAmount: Number(this.data.total),
					totalAmount: Number(this.data.total),
					discountAmount: 0,
					adjustAmount: 0,
					postAmount: 0,
					receivedAmount: Number(this.data.total),
					commissionAmount: 0,
					payOnDeliveryAmount: 0,
					payOnDelivery: 1,
					payType: 1,
					payChannel: 1,
					invoice: 1,
					storeId: this.cList.id,
					storeName: this.cList.name,
					sellerNick: this.cList.name,
					buyerNick: userName,
					customerId: userId,
					orderItemList: itemListArray,
					shippingType: 4,
					orderPickup: {
						pickupType: this.val,
						verify: 2,
						// writeOffTime: this.value
					}
				}
				console.log('params', params);
				// uni.navigateTo({
				// 	url: `/pages/order/orderPaid/orderPaid`
				// })
				this.$request('post', `/order/tradeOrder/addTradeOrder`, params, true).then(response => {
					// 请求成功
					console.log('response', response)
					if (response.code == 200) {
						const ids = this.data.carlist.map(car => car.id);
						this.$request('delete', `/front/shoppingCart/${ids}`)
						console.log('response.data', response.data)
						uni.$emit('register_refresh');
						uni.showToast({
							title: '已下单，因目前是演示阶段，暂时不能付款',
							icon: 'success',
							duration: 2000,
							complete: () => {
								// 页面跳转放在complete回调中
								uni.navigateTo({
									url: `/pages/order/myOrder/myOrder?type=0`
								})
							}
						})
						// uni.navigateTo({
						// 	url: `/pages/order/orderPaid/orderPaid?id=${response.data}`
						// })
					} else if (response.code == 500) {
						uni.showToast({
							title: response.msg,
							icon: 'error',
							duration: 2000
						});
					}
				}).catch(error => {
					// 请求失败
					console.error('请求失败:', error);
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.addr-label {
		padding: 6upx 0;
		border-radius: 3px;
		background-color: #F6390D;
		color: #fff;
		width: 120upx;
		text-align: center;
		flex-shrink: 0;
		margin-right: 14upx;
	}

	.order-spec {
		padding-bottom: 10upx;
	}

	.shop {
		padding-top: 20upx;
	}

	.goods-num {
		font-size: 16px;
		padding-left: 20upx;
	}

	.goods-cont {
		width: 100%;
		box-sizing: border-box;
	}

	.total-goods {
		font-size: 12px;
		color: #999;
	}

	.submit-bar {
		height: 130rpx;
		width: 100%;
		background-color: #FFF;
		position: fixed;
		left: 0;
		bottom: 0;
		box-shadow: 0px -4px 16px 0px rgba(0, 0, 0, 0.15);
		z-index: 10;

		.submit-btn {
			background-color: #0581FE;
			height: 100%;
			width: 100%;
			color: #FFF;
			font-size: 14px;
			// font-weight: 700;
			border-radius: 35upx;
		}
	}

	.good-num {
		font-size: 12px;
		padding-top: 65rpx;
	}

	.send-label {
		font-size: 14px;
		font-weight: 700;
	}
</style>