<template>
  <view class="pwd-retrieve-container">
    <uni-forms ref="form" :value="user" labelWidth="80px">
	  <uni-easyinput type="text" style="display: none;" v-model="user.id"/>
      <uni-forms-item name="address" label="地址">
        <uni-easyinput type="text" v-model="user.address" placeholder="请输入" />
      </uni-forms-item>
      <uni-forms-item name="name" label="用户">
        <uni-easyinput type="text" v-model="user.name" placeholder="请输入" />
      </uni-forms-item>
      <uni-forms-item name="phone" label="电话号码">
        <uni-easyinput type="text" v-model="user.phone" placeholder="请输入" />
      </uni-forms-item>
	  <uni-forms-item name="phone" label="默认">
		  <radio-group @change="radioChange">
		    <radio value="1" :checked="!user.selected">是</radio>
		    <radio value="0" :checked="user.selected">否</radio>
		  </radio-group>
	  </uni-forms-item>  
      <button type="primary" @click="submit">提交</button>
    </uni-forms>
  </view>
</template>

<script>
  import { updateCustAddrApi } from '@/api/mine/information.js';
  import { updateDefaultAddrApi } from '@/api/mine/information.js';

  export default {
    data() {
      return {
        user: {
		  id: "",
		  ifDefault:0,
          address: undefined,
          name: undefined,
          phone: undefined,
		  selected:true
		  
        },
		sexShow: false,
		columns: [['是', '否']],
        rules: {
          address: {
            rules: [{
              required: true,
              errorMessage: '不能为空'
            }]
          },
          name: {
            rules: [{
                required: true,
                errorMessage: '不能为空',
              },
              {
                minLength: 1,
                maxLength: 10,
                errorMessage: '长度在 2 到 20 个字符'
              }
            ]
          },
          phone: {
            rules: [{
                required: true,
                errorMessage: '不能为空'
              }
            ]
          }
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
	 onLoad(options) {
		const addressStr = options.address;
		console.log("addressStr=="+addressStr)
		var address={};
		if(addressStr!=undefined&&""!=addressStr){
			address = JSON.parse(decodeURIComponent(addressStr));
			console.log('Received user data:', address);
		}else{
			address={
			  id: undefined,
			  ifDefault:0,
			  address: undefined,
			  name: undefined,
			  phone: undefined,
			  selected:true
			}
		}
		if(address.ifDefault==1){
			address.selected=false;
		}
		this.user=address;
	  },
    methods: {
      submit() {
		const id=this.user.id;
		if(id!=undefined&&id!=""){
			updatedata(this.user);
		}else{
			insertdata(this.user);
		}
      },
	  radioChange(e) {
	  	console.log('radioChange', e.detail.value)
		if(e.detail.value=="1"){
			this.user.ifDefault=1;
		}else{
			this.user.ifDefault=0;
		}
	  },
	 async updatedata(user){
		  try {
		  		let param = {
		  			"custId":uni.getStorageSync('custId'),
					"id":this.user.id,
					"ifDefault":this.user.ifDefault,
					"address": this.user.address,
					"name": this.user.name,
					"phone": this.user.phone
		  		}
		  		const res = await updateCustAddrApi(param);
				await updateDefaultAddrApi(param);
		  		console.log('🚀 ~ getList ~ res:', res) 
		  		if(res.code==200){
		  			uni.$u.toast(`操作成功！`)
		  		}else{
		  			uni.$u.toast(`操作成功！`)
		  		}
		  } catch (error) {
		  		console.log(error)
		  }
	  },
	 async insertdata(user){
		  try {
		  		let param = {
		  			"custId":uni.getStorageSync('custId'),
		  			"id":this.user.id,
		  			"ifDefault":this.user.ifDefault,
		  			"address": this.user.address,
		  			"name": this.user.name,
		  			"phone": this.user.phone
		  		}
		  		const res = await updateCustAddrApi(param);
		  		await updateDefaultAddrApi(param);
		  		console.log('🚀 ~ getList ~ res:', res) 
		  		if(res.code==200){
		  			uni.$u.toast(`操作成功！`)
		  		}else{
		  			uni.$u.toast(`操作成功！`)
		  		}
		  } catch (error) {
		  		console.log(error)
		  }
	  }
    }
  }
</script>

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

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