<template>
  <view class="flex-col page">
    <view class="flex-col group_4">
      <view class="flex-col section_2">
        <view class="top-group flex-row">
          <text class="text_2 lf-m-r-18">收货人</text>
			<u-input class="input" v-model="address.name" placeholder="请填写收货人姓名" border="surround" :clearable="true" ></u-input>
        </view>
        <view class="top-group flex-row">
          <text class="text_4 lf-m-r-18">手机号码</text>
		  <u-input class="input" v-model="address.phone" placeholder="请填写收货人手机号码" border="surround" maxlength="11" :clearable="true" ></u-input>
        </view>
        <view class="flex-col group_5">
			<picker mode="region" :value="address.region" @change="pickerChnage">
				<view class="top-group justify-between">
				  <view class="flex-row">
				    <text class="text_6 lf-m-r-18">所在地区</text>
				    <text class="text_7" :class="{'black-color': regionNotEmpty}">{{ address.region.length > 0 ? address.region.join(',') : '请选择所在地区' }}</text>
				  </view>
				  <image
				    src="https://project-user-resource-1256085488.cos.ap-guangzhou.myqcloud.com/62677e395a7e3f03107ffc5f/62677e4a35a7e10011e93a80/16509497893779891395.png"
				    class="image_6"
				  />
				</view>
			</picker>
          <view class="flex-row group_7" style="align-items: flex-start;">
            <text class="text_8 lf-m-r-18 lf-m-t-12">详细地址</text>
			<u-textarea v-model="address.desc" placeholder="请填写详细地址：如道路、门牌号、楼栋号、单元室等" ></u-textarea>
          </view>
        </view>
		<view class="top-group flex-row">
		  <text class="text_4 lf-m-r-18">默认地址</text>
		  <u-switch v-model="address.default" style="margin-left: 18rpx;" activeColor="#e7a23f"></u-switch>
		</view>
      </view>
      <view class="flex-col group_8">
        <view class="flex-col items-center button" @click="save">
          <text>保存</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
	import { addNewAddress, addressDetail } from '@/service/address.js';
	
  export default {
    data() {
      return {
		  id: null,
		  address: {
			  name: '',
			  phone: '',
			  region: [],
			  regionCode: '',
			  desc: '',
			  default: false
		  }
	  };
    },
	computed: {
		regionNotEmpty(){
			return this.address.region.length > 0;
		}
	},
	onLoad(options){
		this.id = options.id || null;
		this.id && this.getAddressDetail();
	},
	onReady(){
		let title = '新增收货地址';
		if(this.id){
			title = '编辑收货地址';
		}
		uni.setNavigationBarTitle({
			title: title
		})
	},
	methods: {
		// 编辑进入，根据地址id查询后做修改 
		async getAddressDetail(){
			let res = await addressDetail(this.id);
			let details = res.data.datas;
			this.address = {
				name: details.name,
				phone: details.tel,
				region: details.area_name.split(','),
				regionCode: details.area_id,
				desc: details.desc,
				default: Boolean(details.default)
			}
		},
		pickerChnage(event){
			console.log(event);
			this.address.region = event.detail.value;
			this.address.regionCode = event.detail.code.splice(2).join();
		},
		// 新增/编辑 保存
		save(){
			console.log(this.address)
			let address = this.address;
			if(!address.name) return this.$msg('请填写收货人');
			if(!address.phone) return this.$msg('请填写手机号');
			if(address.region.length === 0) return this.$msg('请选择地区');
			if(!address.desc) return this.$msg('请填写详细信息');
			
			let data = {
				tel: address.phone,
				name: address.name,
				desc: address.desc,
				default: Number(address.default),
				area_id: address.regionCode
			}
			if(this.id){ // 编辑地址，多传入原地址id
				data.addr_id = this.id;
			}
			
			addNewAddress(data).then(res => {
				console.log(res);
				this.$msg('操作成功', {icon: 'success'}).then(() => {
					this.$toBack();
				})
			});
		}
	}
  };
</script>

<style scoped lang="css">
  .top-group {
    padding: 40rpx 0 38rpx;
    border-bottom: solid 2rpx rgb(239, 239, 239);
  }
  .page {
    background-color: #f6f6f6;
    width: 100%;
    overflow-y: auto;
    height: 100%;
  }
  
  .group_4 {
    padding-top: 2rpx;
    flex: 1 1 auto;
    overflow-y: auto;
  }
  .section_2 {
    padding: 0 32rpx;
    background-color: rgb(255, 255, 255);
  }
  .group_8 {
    margin-top: 64rpx;
    padding: 16rpx 32rpx;
    color: rgb(231, 162, 63);
    font-size: 32rpx;
    font-weight: 600;
    line-height: 44rpx;
    white-space: nowrap;
  }
  .group_5 {
    padding-top: 2rpx;
  }
  .button {
    padding: 24rpx 0;
    border-radius: 10rpx;
    border: solid 2rpx rgb(231, 162, 63);
  }
  .text_2 {
    color: rgb(51, 51, 51);
    font-size: 32rpx;
    font-weight: 500;
    line-height: 44rpx;
    white-space: nowrap;
	width: 140rpx;
  }
  .text_3 {
    margin-left: 72rpx;
    color: rgb(195, 195, 195);
    font-size: 32rpx;
    line-height: 44rpx;
    white-space: nowrap;
  }
  .text_4 {
    color: rgb(51, 51, 51);
    font-size: 32rpx;
    font-weight: 500;
    line-height: 44rpx;
    white-space: nowrap;
	width: 140rpx;
  }
  .text_5 {
    margin-left: 40rpx;
    color: rgb(195, 195, 195);
    font-size: 32rpx;
    line-height: 44rpx;
    white-space: nowrap;
  }
  .group_7 {
    margin-right: 6rpx;
    padding: 40rpx 0;
  }
  .image_6 {
    width: 44rpx;
    height: 44rpx;
  }
  .text_8 {
    color: rgb(51, 51, 51);
    font-size: 32rpx;
    font-weight: 500;
    line-height: 44rpx;
    white-space: nowrap;
	width: 140rpx;
  }
  .text_9 {
    margin-left: 40rpx;
    flex: 1 1 auto;
    color: rgb(195, 195, 195);
    font-size: 32rpx;
    line-height: 44rpx;
    text-align: left;
  }
  .text_6 {
    color: rgb(51, 51, 51);
    font-size: 32rpx;
    font-weight: 500;
    line-height: 44rpx;
    white-space: nowrap;
	width: 140rpx;
  }
  .text_7 {
    margin-left: 18rpx;
    color: rgb(195, 195, 195);
    font-size: 28rpx;
    line-height: 44rpx;
    white-space: nowrap;
	max-width: 440rpx;
	overflow:hidden;
	text-overflow:ellipsis;
  }
  .flex-row{
	  display: flex;
	  align-items: center;
  }
  .black-color{
	  color: #303133 !important;
  }
</style>