<template>
  <view class="address-list">
    <Navbar title="我的地址" :showRightText="true" :isBack="false" :text="'新增地址'" @clickIcon="addAddress"/>
    <view
      v-for="(item, index) in addressList"
      :key="index"
      class="address-item"
      @click="handleEdit(item)"
    >
      <view class="address-info">
        <text class="address-text">{{ item.address }}</text>
        <view class="contact-info">
          <text class="contact-text">{{ item.name }}</text>
          <text class="contact-text">{{ item.phone }}</text>
        </view>
      </view>
	  <span size="10">{{item.ifDefault=="1"?"默认":""}}</span>
      <u-icon name="edit-pen-fill" color="#c8c9cc" size="22"></u-icon>
    </view>
  </view>
</template>

<script>
	import { queryCustAddrApi } from '@/api/mine/information.js';
export default {
  data() {
    return {
      addressList: [
        {
          address: '府前西街与站前北街交叉口南200米',
          name: '冯先生1',id: '1',
          phone: '153****5876',
		  ifDefault:"1"
        },
        {
          address: '怡馨家园30号楼(近达人街商城)',
          name: '冯先生',id: '2',ifDefault:"0",
          phone: '153****5876'
        },
        {
          address: '西辛南区46号楼2单元101室',
          name: '冯先生',id: '3',ifDefault:"0",
          phone: '153****5876'
        }
      ]
    }
  },
  onLoad() {
  	this.queryCustAddrList()
  },
  methods: {
	  async queryCustAddrList() {
		try {
				let param = {
					"custId":uni.getStorageSync('custId'),
				}
				const res = await queryCustAddrApi(param)
				console.log('🚀 ~ getList ~ res:', res) 
				this.addressList = res.data;
		} catch (error) {
				console.log(error)
		}
	  },
	  async updateDefaultAddr() {
	  		try {
	  				let param = {
	  					"custId":uni.getStorageSync('custId'),
	  				}
	  				const res = await updateDefaultAddrApi(param)
	  				console.log('🚀 ~ getList ~ res:', res) 
	  				if(res.code==200){
						uni.$u.toast(`操作成功！`)
					}else{
						uni.$u.toast(`操作成功！`)
					}
	  		} catch (error) {
	  				console.log(error)
	  		}
	  },
    handleEdit(address) {
      // 处理编辑事件
      // uni.$u.toast(`编辑第${index + 1}个地址`)
	  console.log('🚀 ==='+address)
		uni.navigateTo({
		  url: `/pages/mine/me/upAddress?address=${encodeURIComponent(JSON.stringify(address))}`
		})
    },
    addAddress() {
      uni.navigateTo({
        url: `/pages/mine/me/upAddress`
      })
    },
	
  }
}
</script>

<style lang="scss" scoped>
.address-list {
  min-height: 100vh;
  background-color: #f8f8f8;
  padding: 30rpx;
}

.address-item {
  background-color: #ffffff;
  border-radius: 8rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.address-info {
  flex: 1;

  .address-text {
    font-size: 28rpx;
    color: #333333;
    line-height: 1.5;
    margin-bottom: 8rpx;
  }

  .contact-info {
    display: flex;
    align-items: center;

    .contact-text {
      font-size: 26rpx;
      color: #999999;

      &:first-child {
        margin-right: 20rpx;
      }
    }
  }
}
</style>