<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">
        <travelerCard 
          :data="item" 
          @defaultChange="onDefaultChange(item.id, $event)"
          @selectChange="onSelectChange(item.id, $event)"
          @edit="onEdit(item.id)"
          @delete="onDelete(item.id)"
        ></travelerCard>
      </view>

    </view>
  
		<view class="flex bottom">
      <button class="btn" @click="onAdd">添加出行人</button>
    </view>

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

  </view>
  
</template>

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

  import travelerCard from './travelerCard.vue'
  import travelerPopup from './travelerPopup.vue'

  export default {
		mixins: [mixinsList],
    components: {
      travelerCard,
      travelerPopup,
    },
    data() {
      return {
        // todo: check key
				mixinsListApi: '',
        queryParams: {
          pageNo: 1,
          pageSize: 10,
        },
        selectedIdList: [],
      }
    },
    onLoad(arg) {
      const { selectIds } = arg
      this.selectedIdList = selectIds?.split?.(',') || []

      this.getData()
    },
    onUnload() {

      const list = this.selectedIdList.map(id => this.list.find(item => item.id === id))

      this.$store.commit('setTravelerList', list)
    },
    methods: {
      updateSelectIdList() {
        this.selectedIdList = this.list.filter(item => item.isSelected).map(item => item.id)
      },
      // todo: delete
      getData() {
        let records = [
          {
            id: '001',
            name: '李梓发',
            idNo: '430223********9999',
            type: 0,
            isSelected: false,
            isDefault: false,
          },
          {
            id: '002',
            name: '吴彦谦',
            idNo: '430223********9999',
            type: 0,
            isSelected: false,
            isDefault: false,
          },
          {
            id: '003',
            name: '冯云',
            idNo: '430223********9999',
            type: 1,
            isSelected: false,
            isDefault: false,
          },
          {
            id: '004',
            name: '冯思钗',
            idNo: '430223********9999',
            type: 2,
            isSelected: false,
            isDefault: false,
          },
          {
            id: '005',
            name: '李书萍',
            idNo: '430223********9999',
            type: 0,
            isSelected: false,
            isDefault: false,
          },
          {
            id: '006',
            name: '冯艺莲',
            idNo: '430223********9999',
            type: 1,
            isSelected: false,
            isDefault: false,
          },
        ]

        this.selectedIdList.forEach(id => {
          const target = records.find(item => item.id === id)
          target.isSelected = true
        })

        this.list = records
        this.updateSelectIdList()
      },
      async onDefaultChange(id, val) {
        try {
          // todo: fetch
          // await this.$fetch('setAddressDefault', { addressId })

          const target = this.list.find(item => item.id === id)

          target.isDefault = val

          this.updateSelectIdList()
        } catch (err) {

        }
      },
      onSelectChange(id, val) {
        const target = this.list.find(item => item.id === id)

        target.isSelected = val

        this.updateSelectIdList()
      },
      async onDelete(id) {
        
        uni.showToast({
          icon: 'loading',
          title: '正在删除',
        });
        
        try {
          // todo: check api & key
          // await this.$fetch('deleteAddress', { id })

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

          this.getData()

        } catch (err) {

        }
      },
      onEdit(id) {
        this.$refs.travelerPopup.open(id)
      },
      onAdd() {
        this.$refs.travelerPopup.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: 14rpx 0;
      box-sizing: border-box;
      font-family: PingFang SC;
      font-weight: 500;
      font-size: 36rpx;
      line-height: 1.4;
      color: #FFFFFF;
      background-image: linear-gradient(to right, #21FEEC, #019AF9);
      border: 2rpx solid #00A9FF;
      border-radius: 41rpx;
    }
	}
</style>