<template>
<view class="flex-col page">
  <view class="flex-col group">
    <text class="self-start font_2 text">收货信息</text>
    <view class="flex-col self-stretch section mt-15">
      <view class="flex-row items-center group_2">
        <text class="font_2">收货人</text>
        <input class="text-wrapper ml-27" v-model="addressParam.name" placeholder="请输入姓名" />
      </view>
      <view class="flex-row items-center group_3">
        <text class="font_2 text_3">手机号码</text>
        <input class="text-wrapper_2 ml-12" v-model="addressParam.phone" placeholder="请输入手机号码" />
      </view>
      <view class="flex-row items-center group_4">
        <text class="font_2 text_5">所在地区</text>
        <view class="flex-col justify-start flex-1 relative group_5 ml-13">
          <input class="text-wrapper_3" v-model="addressParam.region" placeholder="请输入地区" />
          <image
            class="image pos"
            :src="dingwei"
          />
        </view>
      </view>
      <view class="flex-row items-center group_6">
        <text class="font_2 text_7">详细地址</text>
        <textarea class="section_2 ml-12" v-model="addressParam.detailAddress" placeholder="输入详细地址" />
      </view>
			<!-- 暂时用不了 -->
				<view class="flex-row justify-between items-center group_7">
					<checkbox-group @change="defaultAddress">
					<text class="font_2">设为默认收货地址</text>
					<!-- <image
								class="image_2"
								:src="selected"
								/> -->
					<checkbox value="1" />
					</checkbox-group>
				</view>
    </view>
  </view>
  <view class="flex-col justify-start items-center section_3 mt-462">
    <view class="flex-col justify-start items-center text-wrapper_4" @click="newAddress"><text class="text_8">保存并使用</text></view>
  </view>
</view>
</template>

<script setup>
import {ref , onMounted} from 'vue'
import { testUrl , baseUrl , suiUrl } from '../../../api/request';
import { onLoad } from '@dcloudio/uni-app';
import dingwei from '@/pages/Shopping-cart/newaddress_Info/img/dingwei.png'
import selected from '@/pages/Shopping-cart/newaddress_Info/img/selected.png'
const userInfo = ref({})
const addressParam = ref({
	userId: 0,
	name: "",
	phone: "",
	region: "",
	detailAddress: "",
	isDefault: 0
})
//接受编辑按钮传来的地址信息
onLoad((options)=>{
	addressParam.value = JSON.parse(options.editInfo) //将原来的地址信息赋值给原来的addressParam
	// console.log('待编辑地址--->',addressParam.value);
})
onMounted(()=>{
	userInfo.value = uni.getStorageSync("userInfo")
	// console.log('userInfo-->', userInfo.value.id);
})
//勾选默认地址的选项
const defaultAddress =(event)=>{
	event.detail.value[0] ? addressParam.value.isDefault = 1 : addressParam.value.isDefault = 0
}
//发送添加新增地址的请求
const newAddress = async ()  =>{
	console.log(addressParam.value);
	addressParam.value.userId = userInfo.value.id 
	const values =  Object.values(addressParam.value);
	// 使用some()方法来检查是否有任何值为空
	if (values.some(value => value === null || value === undefined || value === '')) {
		await uni.showToast({
			icon: 'error',
			title: "字段不能为空"
		})
		return;
	}
	const res = await uni.request({
		url: baseUrl + '/address/add',
		method: 'POST',
		header: {
			'cookie': wx.getStorageSync('cookie')
		},
		data: {...addressParam.value}
	})
	console.log('res==>',res.data);
	if(res.data.code === 1) {
		uni.navigateBack({  //返回上一页
			
		})
	} else {
		uni.showToast({
			icon: 'error',
			title: "新增地址失败"
		})
		return;
	}
	
}
//跳转回订单页面
const jump =()=> {
	uni.navigateTo({
		url: '../../../pages/Shopping-cart/address/address'
	})
}
</script>

<style lang="scss" scoped>
.mt-15 {
  margin-top: 28.13rpx;
}
.ml-27 {
  margin-left: 50.63rpx;
}
.ml-13 {
  margin-left: 24.38rpx;
}
.mt-462 {
  margin-top: 866.25rpx;
}
.page {
  padding-top: 26.44rpx;
  background-color: #fffaf0;
  width: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  // height: 100vh;
  overflow: hidden;
}
.group {
  padding: 0 23.4rpx;
}
.font_2 {
  font-size: 26.25rpx;
  font-family: Open Sans;
  line-height: 24.34rpx;
  color: #323232;
}
.text {
  line-height: 24.49rpx;
}
.section {
  margin-left: 2.85rpx;
  margin-right: 14.1rpx;
  padding-left: 20.49rpx;
  padding-right: 15.13rpx;
  background-color: #fffef8;
  border-radius: 9.38rpx;
  border: solid 1.88rpx #818181;
}
.group_2 {
  padding: 24.24rpx 4.65rpx 12.19rpx;
  border-bottom: solid 1.88rpx #efefef;
}
.text-wrapper {
  flex: 1 1 0;
  margin-right: 29.1rpx;
}
.group_3 {
  padding: 14.06rpx 5.04rpx 12.19rpx;
  border-bottom: solid 1.88rpx #efefef;
}
.text_3 {
  line-height: 24.17rpx;
}
.text-wrapper_2 {
  flex: 1 1 0;
  margin-right: 28.71rpx;
}
.group_4 {
  padding: 12.19rpx 4.29rpx 10.31rpx;
  border-bottom: solid 1.88rpx #efefef;
}
.text_5 {
  line-height: 24.21rpx;
}
.group_5 {
  margin-right: 12.58rpx;
}
.text-wrapper_3 {
  margin-right: 16.88rpx;
}
.image {
  width: 48.75rpx;
  height: 48.75rpx;
}
.pos {
  position: absolute;
  right: 0;
  bottom: 0;
}
.group_6 {
  padding: 14.06rpx 4.8rpx 8.44rpx;
  border-bottom: solid 1.88rpx #efefef;
}
.text_7 {
  line-height: 24.26rpx;
}
.section_2 {
  width: 300rpx; 
  height: 150rpx;
  flex: 1 1 0;
  margin-right: 19.57rpx;
}
.group_7 {
  padding: 25.31rpx 4.88rpx 32.01rpx;
}
.image_2 {
  margin-right: 19.5rpx;
  width: 33.75rpx;
  height: 33.75rpx;
}
.section_3 {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  margin-right: 13.13rpx;
  padding: 16.88rpx 0;
  background-color: #fffef8;
}
.text-wrapper_4 {
  padding: 27.24rpx 0 23.61rpx;
  background-color: #ffa948;
  border-radius: 46.88rpx;
  width: 618.75rpx;
}
.text_8 {
  color: #ffffff;
  font-size: 30rpx;
  font-family: Open Sans;
  line-height: 27.9rpx;
}
	@import url(../../../common/css/global.css);
</style>