<template>
  <view class="page__view">
		<navbar title="商品详情页" leftClick @leftClick="$utils.navigateBack" color="#191919" bgColor="#FFFFFF" />

    <view class="main">
      <uv-swiper :list="bannerList" indicator indicatorMode="dot" height="680rpx" keyName="image"></uv-swiper>

      <view class="summary">
        <view class="card info">
          <view class="name">{{ detail.fullName }}</view>
          <view class="flex tags">
            <view class="tag" v-for="(tag, tIdx) in detail.tags" :key="tIdx">
              {{ tag }}
            </view>
          </view>
          <view class="flex price">
            <view class="flex price-val">¥<text class="highlight">{{ (detail.price || 0).toFixed(2) }}</text>/份</view>
            <view class="price-bef">¥<text>{{ detail.originalPrice }}</text>/份</view>
          </view>
        </view>
        <view class="card bar">
          <view class="flex row">
            <view class="flex row-content">
              <view class="label">品类：</view>
              <view class="value">{{ detail.category }}</view>
            </view>
          </view>
          <view class="flex row" @click="openPicker">
            <view class="flex row-content">
              <view class="label">规格：</view>
              <view class="value">{{ detail.countDesc || '请选择规格' }}</view>
            </view>
            <uv-picker ref="picker" :columns="[detail.options]" keyName="label" confirmColor="#7451DE" @confirm="onChange"></uv-picker>
            <uv-icon name="arrow-right" color="#C6C6C6" size="24rpx"></uv-icon>
          </view>
          <view class="flex row">
            <view class="flex row-content">
              <view class="label">服务：</view>
              <view class="value">{{ detail.deliverService }}</view>
            </view>
          </view>
        </view>
      </view>
      
      <view class="detail">
  			<uv-parse :content="detail.details"></uv-parse>
      </view>

      <view class="comment">
        <view class="header">
          <view class="highlight">用户评价</view>
          <view>User reviews</view>
        </view>
        <view class="comment-item" v-for="item in commentList" :key="item.id">
          <commentCard :data="item"></commentCard>
        </view>
      </view>
    </view>

		<view class="flex bottom">
      <button class="flex flex-column btn btn-simple" @click="jumpToComment">
        <image class="icon" src="@/pages_order/static/product/comment.png" mode="widthFix"></image>
        <view>评价</view>
      </button>
      <button class="flex btn btn-palin" @click="onAddCart">加入购物车</button>
      <button class="flex btn btn-primary" @click="onBuy">立即购买</button>
    </view>

    <countSelectPopup ref="countSelectPopup" :data="detail" @confirm="onSelectCount"></countSelectPopup>

    <agreementPopup ref="agreementPopup" @confirm="jumpToCreateOrder"></agreementPopup>

  </view>
</template>

<script>
  import commentCard from '@/pages_order/comment/commentCard.vue'
  import agreementPopup from './agreementPopup.vue'
  import countSelectPopup from './countSelectPopup.vue'

  export default {
    components: {
      commentCard,
      agreementPopup,
      countSelectPopup,
    },
    data() {
      return {
        id: null,
        detail: {},
        commentList: [],
      }
    },
    computed: {
			bannerList() {
				const { image } = this.detail

				if (!image) {
					return []
				}

				return Array.isArray(image) ? image : image.split(',')
			},
    },
    onLoad(arg) {
      console.log('onLoad', arg)
      this.id = arg.id

      // todo: fetch detail data

      this.detail = {
        id: '001',
        image: new Array(4).fill('/pages_order/static/product/product-detail.png'),
        name: 'NMN',
        fullName: 'NMN 是 NAD* 的前体，在细胞能量代谢、DNA 修复和抗衰老等方面发挥重要作用。提高 NAD*水平能改善代谢健康1-8。',
        tags: ['专业设备', '科学流程', '质量保证'],
        sales: 24770,
        price: 688.00,
        originalPrice: 1664,
        category: '逆龄抗衰',
        count: null,
        countDesc: null,
        options: [
          { id: '001', label: '每天1颗', count: 1, value: 688, originValue: 1664 },
          { id: '002', label: '每天2颗', count: 2, value: 688*2, originValue: 1664*2 },
          { id: '003', label: '每天3颗', count: 3, value: 688*3, originValue: 1664*3 },
          { id: '004', label: '每天4颗', count: 4, value: 688*4, originValue: 1664*4 },
        ],
        deliverService: '上门取件·送货上门',
        details: `<p>商品详情↓<br/><br/><br/><br/>商品详情↑</p>`
      }

      this.commentList = [
        {
          id: '001',
          userName: '战斗世界',
          avatar: '/pages_order/static/report/avatar.png',
          countDesc: '三月装',
          createTime: '2023-04-18',
          content: '包装很精致，性价比非常高，终于收到啦，期待效果，每日一包，超级方便，真的能坚持服用',
          images: [
            '/pages_order/static/index/medicine-1.png',
            '/pages_order/static/index/medicine-2.png',
            '/pages_order/static/index/medicine-3.png',
          ],
          productServeScore: 5,
          questionExperienceScore: 4,
          deliverySpeedScore: 4.5,
        },
        {
          id: '002',
          userName: '战斗世界',
          avatar: '/pages_order/static/report/avatar.png',
          countDesc: '三月装',
          createTime: '2023-04-18',
          content: '包装很精致，性价比非常高，终于收到啦，期待效果，每日一包，超级方便，真的能坚持服用',
          images: [
            '/pages_order/static/index/medicine-1.png',
            '/pages_order/static/index/medicine-2.png',
            '/pages_order/static/index/medicine-3.png',
          ],
          productServeScore: 5,
          questionExperienceScore: 4,
          deliverySpeedScore: 4.5,
        },
        {
          id: '003',
          userName: '战斗世界',
          avatar: '/pages_order/static/report/avatar.png',
          countDesc: '三月装',
          createTime: '2023-04-18',
          content: '包装很精致，性价比非常高，终于收到啦，期待效果，每日一包，超级方便，真的能坚持服用',
          images: [
            '/pages_order/static/index/medicine-1.png',
            '/pages_order/static/index/medicine-2.png',
            '/pages_order/static/index/medicine-3.png',
          ],
          productServeScore: 5,
          questionExperienceScore: 4,
          deliverySpeedScore: 4.5,
        },
      ]
    },
    methods: {
			openPicker() {
				this.$refs.picker.open();
			},
      onChange(e) {
        const target = e.value[0]
        console.log('onChange', target)

        this.detail.price = target.value
        this.detail.count = target.count
        this.detail.countDesc = target.label
      },
      jumpToComment() {
        this.$utils.navigateTo('/pages_order/comment/commentRecords')
      },
      jumpToCreateOrder() {
        // todo
      },
      onSelectCount(optionId) {
        const { options } = this.detail

        let target = options.find(item => item.id === optionId)

        this.detail.price = target.value
        this.detail.count = target.count
        this.detail.countDesc = target.label

        this.onBuy()
      },
      onAddCart(id) {
				this.$store.commit('addCart', this.detail)
      },
      onBuy() {

        const { count, options } = this.detail

        if (!count && options?.length) {
          this.$refs.countSelectPopup.open()
          return
        }

        // todo

        this.$refs.agreementPopup.open()

				this.$store.commit('createOrder', [this.detail])
      },
    },
  }
</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 {
    width: 100vw;
    padding: calc(var(--status-bar-height) + 120rpx) 0 198rpx 0;
    box-sizing: border-box;
	}

  .summary {
    width: 100%;
    padding: 40rpx 32rpx;
    box-sizing: border-box;
  }

  .card {
    border-radius: 24rpx;

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

    &.info {
      width: 100%;
      padding: 32rpx;
      box-sizing: border-box;
      background: #FFFFFF;

      .name {
        font-family: PingFang SC;
        font-weight: 400;
        font-size: 32rpx;
        line-height: 1.4;
        color: #181818;
      }

      .tags {
        margin-top: 16rpx;
        justify-content: flex-start;
        flex-wrap: wrap;
        gap: 16rpx;

        .tag {
          padding: 2rpx 14rpx;
          font-family: PingFang SC;
          font-weight: 400;
          font-size: 24rpx;
          line-height: 1.4;
          color: #7451DE;
          background: #EFEAFF;
          border: 2rpx solid #7451DE;
          border-radius: 8rpx;
        }
      }

      .price {
        margin-top: 32rpx;
        justify-content: flex-start;
        column-gap: 20rpx;

        &-val {
          font-family: PingFang SC;
          font-weight: 500;
          font-size: 24rpx;
          line-height: 1.4;
          color: #7451DE;

          .highlight {
            margin: 0 8rpx;
            font-size: 48rpx;
          }
        }

        &-bef {
          font-family: PingFang SC;
          text-decoration: line-through;
          font-weight: 400;
          font-size: 28rpx;
          line-height: 1;
          color: #8B8B8B;
        }
      }
    }

    &.bar {
      width: 100%;
      padding: 20rpx 32rpx;
      box-sizing: border-box;
      background: #FAFAFF;
      box-shadow: -4rpx -4rpx 20rpx 0 #FFFFFFC4,
                  4rpx 4rpx 20rpx 0 #AAAACC1F,
                  2rpx 2rpx 4rpx 0 #AAAACC40,
                  -2rpx -2rpx 4rpx 0 #FFFFFF;

      .row {
        padding: 8rpx 0;

        &-content {
          flex: 1;
          justify-content: flex-start;
          column-gap: 4rpx;
          font-family: PingFang SC;
          font-weight: 400;
          font-size: 28rpx;
          line-height: 1.4;

          .label {
            color: #8B8B8B;
          }

          .value {
            color: #393939;
          }
        }
      }

      .row + .row {
        margin-top: 24rpx;
      }
    }
  }

  .comment {
    padding: 40rpx 32rpx;

    .header {
      margin-bottom: 24rpx;
      font-family: PingFang SC;
      font-weight: 400;
      font-size: 26rpx;
      line-height: 1.4;
      color: #252545;

      .highlight {
        font-weight: 600;
        font-size: 48rpx;
      }

    }

    &-item {
      & + & {
        margin-top: 32rpx;
      }
    }
  }

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

    align-items: flex-start;
    column-gap: 32rpx;

		width: 100vw;
		height: 198rpx;
		padding: 24rpx 40rpx 0 40rpx;
		background: #FFFFFF;
		box-sizing: border-box;

    .btn {
      font-family: PingFang SC;

      &-simple {
        font-weight: 400;
        font-size: 22rpx;
        line-height: 1.1;
        color: #999999;

        .icon {
          width: 52rpx;
          height: auto;
        }
      }

      &-palin {
        flex: 1;
        padding: 14rpx 0;
        font-weight: 500;
        font-size: 36rpx;
        line-height: 1.4;
        color: #252545;
        border: 2rpx solid #252545;
        border-radius: 41rpx;
      }

      &-primary {
        flex: 1;
        padding: 16rpx 0;
        font-weight: 500;
        font-size: 36rpx;
        line-height: 1.4;
        color: #FFFFFF;
        background-image: linear-gradient(to right, #4B348F, #845CFA);
        border-radius: 41rpx;
      }
    }
	}

</style>