<script setup lang="ts">
import { deleteAddressApi, getAddressApi, putAddressApi } from '@/api/address'
import { pageUrl } from '@/utils/constants'
import { useAddressStore } from '@/stores/modules/address'
import type { addressItem } from '@/types/address'
import { onShow } from '@dcloudio/uni-app'
import { nextTick, ref } from 'vue'

const addressStore = useAddressStore()
// eslint-disable-next-line no-undef
const paging = ref<ZPagingInstance>()
const addressList = ref<addressItem[]>([])
const queryList = (page: number, page_size: number) => {
  getAddressApi({
    page,
    page_size,
  }).then((res) => {
    paging.value?.complete(res.result.data)
  })
}

onShow(() => {
  nextTick(() => {
    paging.value?.reload()
  })
})

//删除地址
const deleteAddress = (id: string | number) => {
  deleteAddressApi(id).then((res) => {
    addressList.value.splice(
      addressList.value.findIndex((item) => item.id == id),
      1,
    )
  })
  if (addressStore.selectedAddress?.id == id) {
    addressStore.clearSelectedAddress()
  }
  setTimeout(() => {
    uni.showToast({
      icon: 'success',
      title: '删除成功',
    })
  }, 1000)
}

//选中收货地址
const onSelectedAddress = (address: addressItem) => {
  if (addressStore.scene === 'order') {
    addressStore.changeSelectedAddress(address)
    uni.navigateBack()
  }
}

const setDefault = async (item: addressItem) => {
  await putAddressApi({
    ...item,
    is_default: 1,
  })
  addressStore.changeSelectedAddress(item)
  paging.value?.reload()
  uni.showToast({
    icon: 'success',
    title: '设置成功',
  })
}

const toAdd = () => {
  uni.navigateTo({
    url: `${pageUrl['address-form']}`,
  })
}
</script>

<template>
  <shop-paginate-list
    class="viewport"
    ref="paging"
    v-model="addressList"
    @query="queryList"
    :options="{
      auto: false,
      emptyViewText: '暂无收货地址',
    }"
  >
    <view class="address">
      <uni-swipe-action class="address-list">
        <uni-swipe-action-item v-for="item in addressList" :key="item.id">
          <view class="item" @tap="onSelectedAddress(item)">
            <view class="item-content">
              <view class="locate"
                >{{ item.full_location }}
                <text v-if="item.is_default" class="badge">默认</text>
              </view>
              <view class="locate-address">{{ item.address }}</view>
              <view class="user">
                {{ item.name }}
                <text class="contact">{{ item.phone }}</text>
              </view>

              <navigator
                class="edit"
                hover-class="none"
                :url="`${pageUrl['address-form']}?id=${item.id}`"
                @tap.stop="() => {}"
                @tap.prevent="() => {}"
              >
                <uni-icons type="compose" size="24"></uni-icons>
              </navigator>
            </view>
          </view>
          <template #right>
            <view class="button">
              <button @tap="setDefault(item)" class="default-button">设为默认</button>
              <button @tap="deleteAddress(item.id)" class="delete-button">删除</button>
            </view>
          </template>
        </uni-swipe-action-item>
      </uni-swipe-action>
    </view>
    <template #bottom>
      <shop-submit-button title="新建地址" @tap="toAdd" />
    </template>
  </shop-paginate-list>
</template>
<style lang="scss">
page {
  height: 100%;
  overflow: hidden;
}

.delete-button {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 60px;
  height: 100%;
  font-size: 28rpx;
  color: #fff;
  padding: 0;
  border-radius: 0;
  background-color: #cf4444;
}

.default-button {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 80px;
  height: 100%;
  font-size: 28rpx;
  color: #fff;
  padding: 0;
  border-radius: 0;
  background-color: #3c3c3c;
}

.button {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: #f4f4f4;
}

.address {
  margin: 20rpx 20rpx;
  border-radius: 10rpx;
  background-color: #fff;

  .item-content {
    line-height: 1;
    padding: 40rpx 10rpx 40rpx 20rpx;
    border-bottom: 1rpx solid #ddd;
    position: relative;

    .edit {
      position: absolute;
      top: 100rpx;
      right: 10rpx;
      padding: 2rpx 0 2rpx 20rpx;
      border-left: 1rpx solid #666;
      font-size: 26rpx;
      color: #666;
      line-height: 1;
    }
  }

  .user {
    font-size: 28rpx;
    margin-bottom: 20rpx;
    color: #333;
    line-height: 1.6;

    .contact {
      padding-left: 20rpx;
      color: #666;
    }
  }

  .locate {
    line-height: 1.6;
    font-size: 26rpx;
    color: #333;

    .badge {
      display: inline-block;
      padding: 4rpx 10rpx 2rpx 14rpx;
      margin: 2rpx 0 0 10rpx;
      font-size: 20rpx;
      color: #fff;
      border-radius: 6rpx;
      background-color: #ff5f3c;
    }
  }

  .locate-address {
    line-height: 1.6;
    font-size: 30rpx;
    color: #000000;
    font-weight: bolder;
    max-width: 90%;
  }
}

.blank {
  margin-top: 300rpx;
  text-align: center;
  font-size: 32rpx;
  color: #888;
}

.add-btn {
  height: 80rpx;
  text-align: center;
  line-height: 80rpx;
  margin: 30rpx 20rpx;
  color: #fff;
  border-radius: 80rpx;
  font-size: 30rpx;
  background-color: #ff5f3c;
}
</style>
