<template>
	<view class="now-order">
		<navbar :title="$t('pageTitle.immediateOrder')" leftClick @leftClick="$utils.navigateBack" />

		<view class="frame">
			<view class="title">
				<span
					style="width: 10rpx;height: 40rpx;background-color: #1f1c39;border-radius: 10rpx;overflow: hidden;"></span>
				<span>{{ $t('other.aluminumProducts') }}</span>
			</view>

			<view class="basic-info">
				<!--商品规格-->
				<view class="item">
					<view>{{ $t('components.productSpe') }}</view>
					<view>
						{{$t('other.aluminumProducts') }}
					</view>
				</view>

				<!--<uv-divider text="" :hairline="true"></uv-divider>-->

				<!--提货地点-->
        <!--<view class="currentRegion">-->
        <!--	<view>{{ $t('components.pickupLocation') }}</view>-->
        <!--	&lt;!&ndash; <view @click.stop="selectAddr">-->
        <!--		<input class="input" disabled v-model="form.currentRegion" placeholder="请选择所在地区"></input>-->
        <!--		<view class="orientation">-->
        <!--			<img src="../../static/image/address/selectIcon.png"-->
        <!--				style="width:30rpx;height: 30rpx;margin:5rpx 5rpx 5rpx 5rpx;">-->
        <!--		</view>-->
        <!--	</view> &ndash;&gt;-->
        <!--	<view class="">-->
        <!--		{{ fatherData.address }}-->
        <!--	</view>-->
        <!--</view>-->

        <view class="addressA" @click="openAddress">
          <view class="title">提货地点</view>
          <view class="address" style="width: 70%">
            <image mode="" src="/static/image/address/selectIcon.png"></image>
            <view class="">
              {{ address.name }}
            </view>
            <view class="icon">
              <uv-icon name="arrow-right" size="30rpx"></uv-icon>
            </view>
          </view>
        </view>


        <!--详细地址-->
        <view v-if="address.addressDetail" class="item">
          <view>{{ $t('components.detailedAddress') }}</view>
          <view>
            <view class="">
              {{ address.address + ' ' + address.addressDetail }}
              <!--</view>-->
              <!--<view class="">-->
              <!--  {{ address.addressDetail }}-->
            </view>
          </view>
        </view>

        <!--&lt;!&ndash;详细地址&ndash;&gt;-->
        <!--<view class="item">-->
        <!--	<view>{{ $t('components.detailedAddress') }}</view>-->
        <!--	<view>-->
        <!--		&lt;!&ndash; <input v-model="form.address" placeholder="请输入详细地址" clearable></input> &ndash;&gt;-->
        <!--	</view>-->
        <!--</view>-->



				<!--交货日期-->
				<view class="delivery-date">
					<view>{{ $t('components.deliveryDate') }}</view>
					<view class="value">
						<view class="dateTimeCls"
						 @click="startDateOpen">
							<view class="date">
								{{ form.takeTime }}
								<uv-datetime-picker ref="startDateRef" 
								v-model="form.takeTime" 
								mode="date"
								:minDate="$dayjs(fatherData.transactionTime).valueOf()" 
								@confirm="startDateChange"></uv-datetime-picker>
							</view>
							<view class="img">
								>
								<!--<img src="../static/address/1.svg" style="width: 40rpx;-->
								<!--height: 40rpx;">-->
							</view>
						</view>
					</view>
				</view>

				<!--暂定数量-->
				<view class="tentativeQuantity">
					<view class="key">{{ $t('other.tentativeQuantity') }}</view>
					<view class="value">
						<uv-number-box
							inputWidth="200rpx" v-model="form.num" :max="fatherData.num"></uv-number-box>
					</view>
				</view>

				<!--单价-->
				<view class="item">
					<view>{{ $t('components.unitPrice') }}</view>
					<view>
						<input v-model="fatherData.price" disabled clearable></input>
					</view>
				</view>

				<!--履约保证金-->
				<view class="performanceBond">
					<view class="key">{{ $t('other.performanceDeposit') }}</view>
					<view class="value">
						<!-- <input v-model="form.deposit" clearable></input> -->
						{{ deposit }}
					</view>
				</view>

				<view class="Tip">
					<span class="span">{{ $t('other.priceQuantityPercentage', [10]) }}</span>
				</view>
				
				<!--立即下单和联系我们-->
				<view class="btns">
					<span @click="ljxd" class="ljxd">
						{{ $t('components.immediateOrder') }}
					</span>
					<span @click="contactUs" class="lxwm">
						{{ $t('components.contactUs') }}
					</span>
					<span class="tip">{{ $t('components.contactUsIfAnyProblems') }}</span>
				</view>


			</view>
		</view>

		<!-- 联系客服弹框 -->
		<customerServicePopup ref="customerServicePopup" />


    <!-- 地址选择弹框 -->
    <uv-popup ref="addressPopup" :round="30">
      <addressList ref="addressList" height="60vh" @select="selectAddress"/>
    </uv-popup>
	</view>
</template>

<script>
import topbar from "@/components/base/topbar.vue";
import {mapGetters} from 'vuex'
import Position from '@/utils/position.js'
import dayjs from "dayjs";
import customerServicePopup from '@/components/config/customerServicePopup.vue'
import AddressList from "@/pages_order/components/address/addressList.vue";

export default {
		name: "nowOrder",
		components: {
      AddressList,
			topbar,
			customerServicePopup,
		},
		computed: {
			...mapGetters(['userShop']),
			deposit(){
				return (this.fatherData.price * this.form.num * (0.1)).toFixed(2)
			},
		},
		onLoad(options) {
			// if (options.params) {
				// const fatherData = JSON.parse(decodeURIComponent(options.params));
				
				// 从vuex里面拿
				const fatherData = this.$store.state.productDetail
				
				console.log(fatherData, "fatherData==========")
				this.fatherData = fatherData;
				
				// 计算保证金 这个改成计算属性
				// this.form.deposit = (fatherData.price * this.form.num * (0.02)).toFixed(2);
				
			// }
		},
		data() {
			return {
				fatherData: {},
				form: {
					"address": "",
					"addressId": "",
					"deposit": 0,
					"id": "",
					"num": 1,
					"price": 0,
					"productId": "",
          "takeTime": dayjs(new Date()).format("YYYY-MM-DD"),
				},
				id : 0,
        address: {
          name: '请选择地址',
          addressDetail: '',
        },
			}
		},
		
		methods: {

			// 立即下单
			ljxd() {
				// var itemStr = encodeURIComponent(JSON.stringify(this.form));
				// uni.navigateTo({
				// 	url: `/pages_order/tradingPlatform/confirmOrder`,
				// });
				
				this.form.deposit = this.deposit
				this.form.productId = this.fatherData.id
				
				if(this.$utils.verificationAll(this.form, {
					"takeTime": '请选择交货时间',
				})){
					return
				}
				
				this.$api('addProductOrder', this.form, res => {
					if(res.code == 200){
						// 从vuex里面拿
						this.$store.state.cartInfo = res.result
						
						uni.navigateTo({
							url: `/pages_order/tradingPlatform/confirmOrder`,
						});
					}
				})
			},
			// 联系我们
			contactUs() {
				this.$refs.customerServicePopup.open()
			},

      // 选择地址
      selectAddress(e) {
        this.address = e
        this.$refs.addressPopup.close()
      },

			startDateChange(val) {
				this.$nextTick(() => {
					this.form.takeTime = dayjs(val.value).format("YYYY-MM-DD")
				});
			},
			startDateOpen() {
				console.log(1);
				this.$refs.startDateRef.open();
			},

			//地图上选择地址
			selectAddr() {
				Position.getLocation(res => {
					Position.selectAddress(res.longitude, res.latitude, success => {
						this.setAddress(success)
					})
				})
				Position.getLocationDetail(res => {
					console.log(res);
					this.form.address = res
				})
			},
      openAddress() {
        this.$refs.addressPopup.open('bottom')
        // this.getAddressList()

        this.$nextTick(() => {
          this.getAddressListA()
        })
      },

      //获取地址列表
      getAddressListA() {
        this.$refs.addressList
            .getAddressList()
            .then(res => {
              if (res == 0) {
                this.$refs.popup.close()
                return uni.navigateTo({
                  url: '/pages_order/mine/address?type=back'
                })
              }
            })

      },
			//提取用户选择的地址信息复制给表单数据
			setAddress(res) {
				//经纬度信息
				this.form.latitude = res.latitude
				this.form.longitude = res.longitude

				if (!res.address && res.name) { //用户直接选择城市的逻辑
					return this.form.currentRegion = res.name

				}
				if (res.address || res.name) {
					return this.form.currentRegion = res.address + res.name
				}
				this.form.currentRegion = '' //用户啥都没选就点击勾选
			},
		}
	}
</script>

<style scoped lang="scss">
	.now-order {
		background-color: #FFF;
		height: 100vh;

		.frame {
			.title {
				display: flex;
				font-size: 30rpx;
				font-weight: 700;
				padding: 0rpx 0 0rpx 20rpx;
				margin: 20rpx 0 0 0;

				>span:nth-of-type(1) {
					margin: 4rpx 0 0 8rpx;
					background-color: #FFF;
				}

				>span:nth-of-type(2) {
					margin: 0 0 0 8rpx;
					background-color: #FFF;
				}
			}

			.basic-info {
				display: flex;
				flex-direction: column;
				gap: 5rpx;
				background-color: #FFF;
				//margin-top: 20rpx;
				padding: 20rpx 20rpx 20rpx 20rpx;

				.delivery-date {
					display: flex;
					align-items: center;
					background-color: #FFF;
					height: 80rpx;
					// margin: 10rpx 0 0 0;
					padding: 10rpx 0 0 20rpx;

					>view:nth-of-type(1) {
						width: 30%;
						// font-weight: 700;
					}


					.value {
						width: 70%;
						border-radius: 10rpx;
						overflow: hidden;

						.dateTimeCls {
							display: flex;
							align-items: center;
							justify-content: space-between;
							width: 100%;
							height: 80%;
							//border: 1px solid #b0b2b3;
							padding: 5rpx;
							border-radius: 20rpx;

							.date {
								font-size: 30rpx;
								display: flex;
								align-items: center;
								width: 80%;
								height: 100%;
								color: #000;

							}

							.img {
								display: flex;
								justify-content: center;
								align-items: center;
								//margin-left: 50rpx;
								width: 20%;
								//height: 100%;
							}
						}

					}


				}

				.tentativeQuantity {
					display: flex;
					align-items: center;
					background-color: #FFF;
					height: 80rpx;
					// margin: 10rpx 0 0 0;
					padding: 10rpx 0 0 20rpx;

					.key {
						width: 30%;
					}

					.value {
						width: 70%;
						border-radius: 10rpx;
						overflow: hidden;
					}

				}


				.Tip {
					display: flex;
					align-items: center;
					justify-content: center;

					.span {
						color: #999;
						font-size: 24rpx;
					}
				}

				.btns {
					width: 100%;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					gap: 20rpx;

					.ljxd {
						display: flex;
						align-items: center;
						justify-content: center;
						width: 500rpx;
						height: 70rpx;
						border-radius: 40rpx;
						color: #FFF;
						font-size: 28rpx;
						margin: 20rpx 10rpx 0 0;
						background: $uni-color;
						//margin-top: 20rpx;
						border-radius: 40rpx;
					}

					.lxwm {
						display: flex;
						align-items: center;
						justify-content: center;
						width: 500rpx;
						height: 70rpx;
						border-radius: 40rpx;
						color: #000000;
						font-size: 28rpx;
						margin: 20rpx 10rpx 0 0;
						background: #f1f1f1;
						//margin-top: 20rpx;
						border-radius: 40rpx;
					}

					.tip {
						display: flex;
						align-items: center;
						justify-content: center;
						font-size: 24rpx;
						color: #999;
						//width: 500rpx;
					}
				}

				.item {
					display: flex;
					align-items: center;
					background-color: #FFF;
					height: 80rpx;
					// margin: 10rpx 0 0 0;
					padding: 10rpx 0 0 20rpx;

					>view:nth-of-type(1) {
						width: 30%;
						// font-weight: 700;
					}

					>view:nth-of-type(2) {
						width: 70%;
						border-radius: 10rpx;
						overflow: hidden;


						input {
							background-color: #FFF;
							font-size: 28rpx;
							padding: 16rpx 8rpx 16rpx 15rpx;
						}
					}
				}

				.performanceBond {
					display: flex;
					align-items: center;
					background-color: #FFF;
					height: 80rpx;
					// margin: 10rpx 0 0 0;
					padding: 10rpx 0 0 20rpx;

					.key {
						width: 30%;
						// font-weight: 700;
					}

					.value {
						width: 70%;
						border-radius: 10rpx;
						overflow: hidden;


						input {
							background-color: #FFF;
							font-size: 28rpx;
							padding: 16rpx 8rpx 16rpx 15rpx;
						}
					}
				}

        .addressA {
          display: flex;
          align-items: center;
          background-color: #FFF;
          height: 80rpx;
          // margin: 10rpx 0 0 0;
          //padding: 10rpx 0 0 20rpx;

          .title {
            display: flex;
            align-items: center;
            justify-content: flex-start;
            font-weight: 400;
            width: 30%;
          }

          .address {
            width: 70%;
            display: flex;
            padding: 20rpx;
            background-color: #fff;

            image {
              width: 30rpx;
              height: 30rpx;
              margin: 10rpx;
            }

            view {

              //margin: 20rpx;
              overflow: hidden; //超出的文本隐藏
              text-overflow: ellipsis; //溢出用省略号显示
              white-space: nowrap; //溢出不换行
            }

            .icon {
              display: flex;
              justify-content: center;
              align-items: center;
              margin-left: auto;
            }
          }
        }

				.currentRegion {
					display: flex;
					align-items: center;
					background-color: #FFF;
					height: 80rpx;
					// margin: 10rpx 0 0 0;
					padding: 10rpx 0 0 20rpx;

					>view:nth-of-type(1) {
						width: 30%;
						// font-weight: 700;
					}

					>view:nth-of-type(2) {
						width: 70%;
						padding: 0 20rpx 0 0;
						display: flex;

						.input {
							background-color: #f5f5f5;
							// color: #a4a4a4;
							font-size: 28rpx;
							padding: 8rpx 8rpx 8rpx 15rpx;
							width: 350rpx;
						}

						.orientation {
							display: flex;
							padding: 10rpx 10rpx 10rpx 20rpx;
							font-size: 30rpx;
							color: #FBAB32;
							flex-direction: column;
							justify-content: center;
							align-items: center;
						}


					}
				}

			}
		}
	}
</style>