<template>
	<view class="page__view">

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

		<view class="main">
      <uv-checkbox-group v-model="selectedIdList" placement="column">
        <view class="card" v-for="item in list" :key="item.id">
          <travelerCard 
            :data="item" 
            @defaultChange="onDefaultChange(item.id, $event)"
            @edit="onEdit(item.id)"
            @delete="onDelete(item.id)"
          ></travelerCard>
        </view>
		  </uv-checkbox-group>
    </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 {
				mixinsListApi: 'queryTouristList',
        queryParams: {
          pageNo: 1,
          pageSize: 10,
        },
        selectedIdList: [],
      }
    },
    onLoad(arg) {
      const { selectIds } = arg
      this.selectedIdList = selectIds?.split?.(',')?.filter(val => val) || []

      this.getData()
    },
    onUnload() {

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

      this.$store.commit('setTravelerList', list)
    },
    methods: {
      getDataThen(records) {
        this.list = records.map(item => {
          return {
            ...item,
            // todo: transform periodId to type and type desc
            type: 0, // 0-成人 1-青少年 2-儿童
            isDefault: item.isDefault == '1' ? true : false
          }
        })
      },
      async onDefaultChange(id, val) {
        try {
          const target = this.list.find(item => item.id === id)

          const params = {
            ...target,
            isDefault: val ? '1' : '0',
          }
          
          await this.$fetch('updateTourist', params)

          target.isDefault = val
        } catch (err) {

        }
      },
      async onDelete(id) {
        
        uni.showToast({
          icon: 'loading',
          title: '正在删除',
        });
        
        try {
          // todo: check api & key
          // await this.$fetch('deleteTourist', { 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: $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>