<template>
  <view>
    <uv-popup ref="popup" mode="bottom" bgColor="none"  @change="onPopupChange">
      <view class="popup__view" v-if="isShow">
        <view class="flex header">
          <view class="title">评价</view>
          <button class="btn" @click="close">关闭</button>
        </view>
        <view class="form">
          <uv-form 
            ref="form"
            :model="form"
            :rules="rules"
            errorType="toast"
          >
            <view class="section">
              <productCard :data="productCardData" style="background: linear-gradient(120deg, #DAF3FF, #FBFEFF 30%, #FBFEFF); border: 2rpx solid #FFFFFF;"></productCard>
            </view>
            <view class="section">
              <view class="form-item">
                <uv-form-item prop="processScore" :customStyle="formItemStyle">
                  <view class="flex row">
                    <view class="form-item-label">行程</view>
                    <view class="form-item-content">
                      <formRate v-model="form.processScore"></formRate>
                    </view>
                  </view>
                </uv-form-item>
              </view>
              <view class="form-item">
                <uv-form-item prop="spotScore" :customStyle="formItemStyle">
                  <view class="flex row">
                    <view class="form-item-label">景点</view>
                    <view class="form-item-content">
                      <formRate v-model="form.spotScore"></formRate>
                    </view>
                  </view>
                </uv-form-item>
              </view>
              <view class="form-item">
                <uv-form-item prop="teacherScore" :customStyle="formItemStyle">
                  <view class="flex row">
                    <view class="form-item-label">导师</view>
                    <view class="form-item-content">
                      <formRate v-model="form.teacherScore"></formRate>
                    </view>
                  </view>
                </uv-form-item>
              </view>
            </view>
            <view class="form-item">
              <uv-form-item prop="contentIds" :customStyle="formItemStyle">
                <view class="form-item-content">
                  <view class="tags">
                    <view 
                      v-for="(item, oIdx) in configList.commentOptionList" :key="oIdx"
                      :class="['tag',  form.contentIds.includes(item.id) ? 'is-active' : '']" 
                      @click="onSelectContent(item.id)"
                    >
                      {{ item.title }}
                    </view>
                  </view>
                </view>
              </uv-form-item>
            </view>
          </uv-form>
        </view>
        <view class="footer">
          <button class="flex btn" @click="onPublish">发布</button>
        </view>
      </view>
    </uv-popup>
  </view>
</template>

<script>
  import productCard from '@/pages_order/order/components/productCard.vue'
  import formRate from '@/pages_order/components/formRate.vue'

  export default {
    components: {
      productCard,
      formRate,
    },
    data() {
      return {
        isShow: false,
        id: null,
        // todo: fetch
        detail: {},
        form: {
          processScore: null,
          spotScore: null,
          teacherScore: null,
          contentIds: [],
        },
        rules: {
          'processScore': {
            type: 'number',
            required: true,
            message: '请为行程打分',
          },
          'spotScore': {
            type: 'number',
            required: true,
            message: '请为景点打分',
          },
          'teacherScore': {
            type: 'number',
            required: true,
            message: '请为导师打分',
          },
          'contentIds': {
            type: 'array',
            required: true,
            message: '请选择评语',
          },
        },
        // todo: fetch
        options: [],
      }
    },
    computed: {
      productCardData() {
        const { 
          activityId,
          activityTitle,  
          activityBrief,
          activityTag,
          startDate,
          endDate,
        } = this.detail

        return {
          time: 'time',
          product: {
            id: activityId,
            title: activityTitle,
            brief: activityBrief,
            tagDetails: activityTag,
            dateList: [
              {
                id: 'time',
                startDate,
                endDate,
              }
            ]
          }
        }
      },
    },
    methods: {
      async getData() {
        // todo: fetch order product
      },
      async open(id, detail) {
        console.log('open', id, detail)
        this.id = id

        this.detail = detail
        // todo: fetch order product
        // await this.getData()

        this.form = {
          processScore: null,
          spotScore: null,
          teacherScore: null,
          contentIds: [],
        }

        this.$refs.popup.open()
      },
      close() {
        this.$refs.popup.close()
      },
      onPopupChange(e) {
        this.isShow = e.show
      },
      onSelectContent(id) {
        let arr = this.form.contentIds
        this.form.contentIds = arr.includes(id) ? arr.filter(item => item !== id) : arr.concat(id)
      },
      async onPublish() {
        try {
          await this.$refs.form.validate()

          const {
            processScore,
            spotScore,
            teacherScore,
            contentIds,
          } = this.form

          const commentOptionList = this.configList.commentOptionList

          const content = contentIds.map(id => {
            return commentOptionList.find(item => item.id === id)?.title
          }).join('、')

          const params = {
            orderId: this.id,
            processScore,
            spotScore,
            teacherScore,
            content,
          }

          await this.$fetch('addComment', params)

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

          this.$emit('submitted')

          this.close()

        } catch (err) {
          console.log('onSave err', err)
        }
      },
    },
  }
</script>

<style lang="scss" scoped>

  .popup__view {
    
    width: 100vw;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    background: #FFFFFF;
    border-top-left-radius: 32rpx;
    border-top-right-radius: 32rpx;
  }

  .header {
    position: relative;
    width: 100%;
    padding: 24rpx 0;
    box-sizing: border-box;
    border-bottom: 2rpx solid #EEEEEE;

    .title {
      font-family: PingFang SC;
      font-weight: 500;
      font-size: 34rpx;
      line-height: 1.4;
      color: #181818;
    }

    .btn {
      font-family: PingFang SC;
      font-weight: 500;
      font-size: 32rpx;
      line-height: 1.4;
      color: #8B8B8B;
      position: absolute;
      top: 26rpx;
      left: 40rpx;
    }

  }

  .section {
    & + & {
      margin-top: 24rpx;
    }
  }
  
  .form {
    max-height: 75vh;
    padding: 32rpx 40rpx;
    box-sizing: border-box;
    overflow-y: auto;

    &-item {

      &-label {
        margin-bottom: 14rpx;
        display: flex;
        align-items: center;
        font-family: PingFang SC;
        font-weight: 400;
        font-size: 26rpx;
        line-height: 1.4;
        color: #181818;

        .icon {
          margin-right: 8rpx;
          width: 16rpx;
          height: auto;
        }
      }

      &-content {

      }
    }
  }

  .row {
    justify-content: space-between;
    padding: 4rpx 0;

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

    .form-label {
      margin: 0;
    }
  }

  .tags {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 24rpx;
  }

  .tag {
    min-width: 0;
    padding: 16rpx;
    font-size: 26rpx;
    line-height: 1.4;
    color: #252545;
    background: #F5F8FF;
    border-radius: 24rpx;

    &.is-active {
      color: #FFFFFF;
      background: #00A9FF;
    }
  }

  .footer {
    width: 100%;
    padding: 32rpx 40rpx;
    box-sizing: border-box;
    border-top: 2rpx solid #F1F1F1;

    .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>