<template>
	<view class="addr-page">
		<view v-if="loading" class="addr-page__loading">
			<text>加载中…</text>
		</view>
		<view v-else-if="list.length === 0" class="addr-empty">
			<text class="addr-empty__txt">暂无收货地址，请添加</text>
			<button class="mine-btn-primary addr-empty__btn" @click="goAdd">新增地址</button>
		</view>
		<view v-else class="addr-list">
			<view v-for="item in list" :key="item.addressId" class="addr-card">
				<view class="addr-card__head">
					<text class="addr-card__name">{{ item.consigneeName }}</text>
					<text class="addr-card__mobile">{{ item.mobile }}</text>
					<text v-if="item.isDefault === '1'" class="addr-card__tag">默认</text>
				</view>
				<text class="addr-card__addr">{{ item.fullAddress || formatAddr(item) }}</text>
				<view class="addr-card__actions">
					<text
						v-if="item.isDefault !== '1'"
						class="addr-card__act"
						@click="setDefault(item)"
					>设为默认</text>
					<text class="addr-card__act" @click="goEdit(item)">编辑</text>
					<text class="addr-card__act addr-card__act--danger" @click="remove(item)">删除</text>
				</view>
			</view>
		</view>
		<view v-if="list.length > 0" class="addr-footer">
			<button class="form-footer__btn" @click="goAdd">新增地址</button>
		</view>
	</view>
</template>

<script setup>
import { ref } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import { getAddressList, deleteAddress, setDefaultAddress } from '@/api/member'
import { ensureApiToken } from '@/utils/apiAuth'
import { PAGE_ADDRESS_EDIT } from '@/utils/pageRoute'

const list = ref([])
const loading = ref(true)

onShow(() => {
  if (!ensureApiToken(false)) return
  loadList()
})

function loadList() {
  loading.value = true
  getAddressList()
    .then((res) => {
      const rows = res.data || []
      list.value = rows.sort((a, b) => {
        if (a.isDefault === '1' && b.isDefault !== '1') return -1
        if (b.isDefault === '1' && a.isDefault !== '1') return 1
        return 0
      })
    })
    .catch(() => {
      list.value = []
    })
    .finally(() => {
      loading.value = false
    })
}

function formatAddr(item) {
  if (item.fullAddress) {
    return item.fullAddress
  }
  const region = (item.regionName || '').replace(/\//g, '')
  return region + (item.detailAddress || '')
}

function goAdd() {
  uni.navigateTo({ url: `${PAGE_ADDRESS_EDIT}?mode=add` })
}

function goEdit(item) {
  uni.navigateTo({
    url: `${PAGE_ADDRESS_EDIT}?mode=edit&id=${item.addressId}`
  })
}

function setDefault(item) {
  setDefaultAddress(item.addressId)
    .then(() => {
      uni.showToast({ title: '已设为默认', icon: 'none' })
      loadList()
    })
    .catch(() => {})
}

function remove(item) {
  uni.showModal({
    title: '提示',
    content: '确定删除该收货地址吗？',
    success: (res) => {
      if (!res.confirm) return
      deleteAddress(item.addressId)
        .then(() => {
          uni.showToast({ title: '已删除', icon: 'none' })
          loadList()
        })
        .catch(() => {})
    }
  })
}
</script>

<style lang="scss" scoped>
@import '@/styles/mine.scss';

.addr-page {
  min-height: 100vh;
  padding: 24rpx;
  padding-bottom: 160rpx;
  background: #f0ebe5;
  box-sizing: border-box;
}
.addr-empty {
  padding: 120rpx 48rpx;
  text-align: center;
  background: #fff;
  border-radius: 16rpx;
}
.addr-empty__txt {
  font-size: 28rpx;
  color: #9a938c;
}
.addr-empty__btn {
  margin-top: 40rpx;
  width: 100%;
}
.addr-card {
  margin-bottom: 20rpx;
  padding: 28rpx;
  background: #fff;
  border-radius: 16rpx;
}
.addr-card__head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12rpx;
}
.addr-card__name {
  font-size: 30rpx;
  font-weight: 600;
  color: #333;
}
.addr-card__mobile {
  font-size: 28rpx;
  color: #666;
}
.addr-card__tag {
  padding: 4rpx 12rpx;
  font-size: 22rpx;
  color: #2e7d32;
  background: #e8f5e9;
  border-radius: 6rpx;
}
.addr-card__addr {
  display: block;
  margin-top: 12rpx;
  font-size: 26rpx;
  color: #666;
  line-height: 1.5;
}
.addr-card__actions {
  margin-top: 20rpx;
  display: flex;
  justify-content: flex-end;
  gap: 32rpx;
}
.addr-card__act {
  font-size: 26rpx;
  color: #2e7d32;
}
.addr-card__act--danger {
  color: #d32f2f;
}
.addr-footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 24rpx 32rpx;
  padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
  background: #fff;
}
.addr-page__loading {
  padding: 80rpx;
  text-align: center;
  color: #999;
}
</style>
