<template>
	<page-meta :page-font-size="fontValue+'px'" :root-font-size="fontValue+'px'"></page-meta>
	
  <view class="pwd-retrieve-container">
	  <Navbar :title="NavbarTitle" :showRightText="false" :isBack="false"/>
    <uni-forms ref="form" :value="address" labelWidth="80px"> 
	  <uni-forms-item name="contactName" label="联系人" required="true">
	    <uni-easyinput type="text" v-model="address.contactName" maxlength="20" placeholder="请输入" />
	  </uni-forms-item>
	  <uni-forms-item name="mobile" label="电话号码" required="true">
	    <uni-easyinput type="text" v-model="address.mobile"  maxlength="11" placeholder="请输入" />
	  </uni-forms-item>
     <uni-forms-item name="addrFullName" label="主要地址"  style="display: flex;align-items: center;" required="true"> 
		 <uni-easyinput type="text" v-model="address.addrFullName" maxlength="50" placeholder="主要地址" /> 
      </uni-forms-item>
	  <uni-forms-item name="detailAddr" label="详细地址" required="true">
	    <uni-easyinput type="text" v-model="address.detailAddr" maxlength="50" placeholder="小区,楼号,门牌" />
	  </uni-forms-item>
	  
	  <view @click="submit" style="width: 90%;height: 80rpx;margin: 10px auto;background: #ff8b38;color: #fff;border-radius: 10rpx;text-align: center;line-height: 80rpx;" >
		  提交
	  </view>
	  <view v-show="address.id!=''" @click="delAddress" style="width: 90%;height: 80rpx;margin: 10px auto;background: #fff;border-radius: 10rpx;text-align: center;line-height: 80rpx;border: 1px solid #ccc;" >
		  删除
	  </view>
	  
     <!-- <button type="primary" @click="submit">提交</button>
      <button type="danger" >删除</button> -->
    </uni-forms>
	 
  </view>
</template>

<script>
  import { addCustAddrForAppApi,deleteCustAddrForAppApi,updateCustAddrApi } from '@/api/mine/address.js';
  
  export default {
    data() {
      return { 
		fontValue:uni.getStorageSync('fontSize') || 8,
		NavbarTitle:"收货地址",
		address:{
			id: "",
			// ifDefault:0,
			addrFullName: "", 
			detailAddr: "",
			contactName: "",
			mobile: "", 
		},
        rules: {
          addrFullName: {
            rules: [{
              required: true,
              errorMessage: '不能为空'
            }]
          },
		  detailAddr: {
		    rules: [{
		      required: true,
		      errorMessage: '不能为空'
		    }]
		  },
          contactName: {
            rules: [{
                required: true,
                errorMessage: '不能为空',
              },
              {
                minLength: 1,
                maxLength: 20,
                errorMessage: '长度在 2 到 20 个字符'
              }
            ]
          },
          mobile: {
            rules: [{
                required: true,
                errorMessage: '不能为空'
              }
            ]
          }
        }
      }
    }, 
	onLoad(options) {  
		// const addressStr = options.address;
		// address = JSON.parse(decodeURIComponent(addressStr));
		this.address = JSON.parse(options.address) 
	},
    methods: { 
		submit() {
			const regex = /^1[3-9]\d{9}$/;
			// console.log(this.address)
			if(this.address.contactName==""){
				uni.$u.toast(`联系人不能为空`) 
			}else if(this.address.mobile==""){
				uni.$u.toast(`手机号不能为空`) 
			}else if(!regex.test(this.address.mobile)){
				uni.$u.toast(`请输入正确手机号`) 
			}else if(this.address.addrFullName==""){
				uni.$u.toast(`地址不能为空`) 
			}else if(this.address.detailAddr==""){
				uni.$u.toast(`详细地址不能为空`) 
			}else{
				const id=this.address.id;
				if(id!=undefined&&id!=""){
					this.updatedata();
				}else{
					this.insertdata();
				}
			}
		}, 
		async updatedata(){
			let param = { 
				"id":this.address.id,
				"userId":uni.getStorageSync('userId'),
				"contactName": this.address.contactName, 
				"mobile": this.address.mobile,
				"addrFullName": this.address.addrFullName, 
				"detailAddr": this.address.detailAddr, 
				"areaCode":1,
				"ifDefault":this.address.ifDefault,
			}
			const res = await updateCustAddrApi(param); 
			console.log('?? ~ getList ~ res:', res) 
			if(res.code==200){
				uni.$u.toast(`操作成功！`)
				setTimeout(()=>{
					uni.navigateBack()
				},500)
			}else{
				uni.$u.toast(`操作失败！`)
			}
		},
		async insertdata(){ 
			let param = {
				"userId":uni.getStorageSync('userId'),
				"contactName": this.address.contactName, 
				"mobile": this.address.mobile,
				"addrFullName": this.address.addrFullName, 
				"detailAddr": this.address.detailAddr, 
				"areaCode":1,
				"ifDefault":2,
			}
			const res = await addCustAddrForAppApi(param); 
			console.log('?? ~ getList ~ res:', res) 
			if(res.code==200){
				uni.$u.toast(`新增成功！`)
				setTimeout(()=>{
					uni.navigateBack()
				},500)
			}else{
				uni.$u.toast(res.msg)
			}
		}, 
		async delAddress(){ 
			const res = await deleteCustAddrForAppApi({id:this.address.id}); 
			if(res.code==200){
				uni.$u.toast(`删除成功！`)
				setTimeout(()=>{
					uni.navigateBack()
				},500)
			}else{
				uni.$u.toast(res.msg)
			}
			
		}
	
	}
  }
</script>

<style lang="scss">
  page {
    background-color: #ffffff;
  }

  .pwd-retrieve-container {
    padding-top: 36rpx;
    padding: 15px;
  }
</style>
