<template>
	<view class="page__view">

		<navbar title="选择地址" leftClick @leftClick="$utils.navigateBack" color="#191919" bgColor="#FFFFFF" />

		<view class="main">

      <view class="card" v-for="item in list" :key="item.id">
        <addressCard 
          :data="item" 
          @defaultChange="onDefaultChange(item.id, $event)"
          @click="onSelect(item)"
          @edit="onEdit(item.id)"
          @delete="onDelete(item.id)"
        ></addressCard>
      </view>

    </view>
  
		<view class="flex bottom">
      <button class="btn" @click="onAdd">新建地址</button>
    </view>

    <addressPopup ref="addressPopup" @submitted="getData"></addressPopup>

  </view>
  
</template>

<script>
	import mixinsList from '@/mixins/list.js'

  import addressCard from './addressCard.vue'
  import addressPopup from '@/pages_order/address/addressPopup.vue'

  export default {
		mixins: [mixinsList],
    components: {
      addressCard,
      addressPopup,
    },
    data() {
      return {
				mixinsListApi: 'getAddressList',
        queryParams: {
          pageNo: 1,
          pageSize: 10,
        },
      }
    },
    onLoad() {
      this.queryParams.userId = this.userInfo.id
      this.getData()
    },
    methods: {
      async onDefaultChange(addressId) {
        console.log('onDefaultChange', addressId)
        try {
          await this.$fetch('setAddressDefault', { addressId })
          this.getData()
        } catch (err) {

        }
      },
      onSelect(data) {
				this.$store.commit('setAddressInfo', data)

        this.$utils.navigateBack()
      },
      async onDelete(addressId) {
        
        uni.showToast({
          icon: 'loading',
          title: '正在删除',
        });
        
        try {
          await this.$fetch('deleteAddress', { addressId })

          uni.showToast({
            icon: 'success',
            title: '删除成功',
          });

          this.getData()

        } catch (err) {

        }
      },
      onEdit(id) {
        this.$refs.addressPopup.open(id)
      },
      onAdd() {
        this.$refs.addressPopup.open()
      },
    },
  }
</script>

<style scoped lang="scss">
  .page__view {
		width: 100vw;
		min-height: 100vh;
		background-color: $uni-bg-color;
		position: relative;

    /deep/ .nav-bar__view {
      position: fixed;
      top: 0;
      left: 0;
    }
  }

  .main {
    padding: calc(var(--status-bar-height) + 160rpx) 40rpx 254rpx 40rpx;
	}

  .card {
    & + & {
      margin-top: 40rpx;
    }
  }

  .bottom {
		position: fixed;
		left: 0;
		bottom: 0;

    align-items: flex-start;

		width: 100vw;
		// height: 214rpx;
		padding: 32rpx 40rpx;
		padding-bottom: calc(env(safe-area-inset-bottom) + 32rpx);
		background: #FFFFFF;
		box-sizing: border-box;

    .btn {
      width: 100%;
      padding: 16rpx 0;
      box-sizing: border-box;
      font-family: PingFang SC;
      font-weight: 500;
      font-size: 36rpx;
      line-height: 1;
      color: #FFFFFF;
      background-image: linear-gradient(to right, #4B348F, #845CFA);
      border-radius: 41rpx;
    }
	}
</style>