<template>
	<view class="page__view">

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

		<view class="main">

      <view class="card" v-for="(item, index) in list" :key="item.id">
        <addressCard 
          :data="item" 
          @defaultChange="onDefaultChange(index, $event)"
          @click="onSelect(item)"
          @edit="onEdit(item)"
          @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 {
        // todo
				mixinsListApi: '',
      }
    },
    methods: {
			getData() {
				this.list = [
          {
            id: '001',
            name: '郑文锦',
            phone: '18108341643',
            area: ['海南省', '海口市', '秀英区'],
            address: '秀英街道5单元183室',
            default: true,
          },
          {
            id: '002',
            name: '周俊',
            phone: '13293992217',
            area: ['贵州省', '遵义市', '道真仡佬族苗族自治县'],
            address: '洛龙镇5幢172室',
            default: false,
          },
          {
            id: '003',
            name: '何炜',
            phone: '18108341643',
            area: ['新疆维吾尔自治区', '乌鲁木齐市', '沙依巴克区'],
            address: '仓房沟片区街道4单元50室',
            default: false,
          },
          {
            id: '004',
            name: '赵萸艳',
            phone: '15022123314',
            area: ['海南省', '海口市', '秀英区'],
            address: '海南省海口市秀英区秀英街道5单元183室',
            default: false,
          },
          {
            id: '005',
            name: '周俊',
            phone: '13293992217',
            area: ['贵州省', '遵义市', '道真仡佬族苗族自治县'],
            address: '贵州省遵义市道真仡佬族苗族自治县洛龙镇5幢172室',
            default: false,
          },
          {
            id: '006',
            name: '何炜',
            phone: '18108341643',
            area: ['新疆维吾尔自治区', '乌鲁木齐市', '沙依巴克区'],
            address: '新疆维吾尔自治区乌鲁木齐市沙依巴克区仓房沟片区街道4单元50室',
            default: false,
          },
          {
            id: '007',
            name: '赵萸艳',
            phone: '15022123314',
            area: ['海南省', '海口市', '秀英区'],
            address: '海南省海口市秀英区秀英街道5单元183室',
            default: false,
          },
        ]
				this.total = this.list.length
			},
      onDefaultChange(index) {
        console.log('onDefaultChange', index)

        this.list.forEach((item, idx) => {
          if (idx === index) {
            item.default = true
          } else {
            item.default = false
          }
        })
      },
      onSelect(data) {
				this.$store.commit('setAddressInfo', data)

        this.$utils.navigateBack()
      },
      onDelete(id) {
        
        uni.showToast({
          icon: 'loading',
          title: '正在删除',
        });

        setTimeout(() => {
          this.list = this.list.filter(item => item.id !== id)
          this.total = this.list.length

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

          // uni.showToast({
          //   icon: 'error',
          //   title: '删除失败',
          // });
        }, 1000)
      },
      onEdit(data) {
        this.$refs.addressPopup.open(data)
      },
      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;
		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>