<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">
					<picker mode="region" @change="bindTimeChange" style="width: 400rpx;">
						<input
								border="bottom"
								placeholder="请选择省市区"
								type="text"   v-model="addressParam.region"
								:disabled="true"/>
					</picker>
          <image
            class="image pos"
            src="https://carbon2.obs.cn-north-4.myhuaweicloud.com:443/feiyi%2Ftest%2F0%2FkYDyjpPh-dingwei.png"
						@click="chooseLocation()"
          />
        </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, onShow } from '@dcloudio/uni-app';
import emitter from '../../../utils/emitter';
const addressParam = ref({
	name: "",
	phone: "",
	region: "",
	detailAddress: "",
	isDefault: 0
})
const isAdd = ref(false)
//接受编辑按钮传来的地址信息
onLoad((options)=>{
	if(JSON.stringify(options) != '{}') {
		addressParam.value = JSON.parse(options.editInfo) //将原来的地址信息赋值给原来的addressParam
	} else {
		isAdd.value = true
	}
	// console.log(options);
})
onShow(()=>{
})
onMounted(()=>{
	emitter.on('region',(val)=>{  //将地址弹窗的值传到当前页面
		addressParam.value.region = val
		// console.log('val--->',val);
	})
	if(!isAdd.value) emitter.emit('addRegion', addressParam.value.region)
})
//勾选默认地址的选项
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;
	}
	console.log(addressParam.value);
	if(addressParam.value.id != undefined) {
		const res = await uni.request({
			url: baseUrl + '/address/update',
			method: 'POST',
			header: {
				'cookie': wx.getStorageSync('cookie')
			},
			data: { ...addressParam.value }
		})
		console.log('res1==>',res.data);
		sucRes(res.data.code)
	} else {
		const res = await uni.request({
			url: baseUrl + '/address/add',
			method: 'POST',
			header: {
				'cookie': wx.getStorageSync('cookie')
			},
			data: {...addressParam.value}
		})
		console.log('res2==>',res.data);
		sucRes(res.data.code)
	}
}
const sucRes =(res)=>{  //请求成功执行的方法
	if(res === 1) {
		uni.navigateBack({  //返回上一页
			
		})
	} else {
		uni.showToast({
			icon: 'error',
			title: '新增地址失败'
		})
		return;
	}
}
const chooseLocation = () => {  //定位获取地址
	uni.chooseLocation({
		success: (res) => {
			// console.log('详细地址:' + res.address);
			// console.log('位置名称:' + res.name);
			addressParam.value.region = res.address
			addressParam.value.detailAddress = res.name
		}
	})
}
const bindTimeChange = (e) => {  //picker省市区选择
	// console.log('省--->',e.detail.value[0]);
	// console.log('市--->',e.detail.value[1]);
	// console.log('区--->',e.detail.value[2]);
	addressParam.value.region = e.detail.value[0] + e.detail.value[1] + e.detail.value[2]
}
</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;
}
.zujian{  //地址组件样式
	height: 100rpx;
	width: 450rpx;
	margin-left: 50rpx;
}
	@import url(../../../common/css/global.css);
</style>