<template>
  <view>
    <uv-popup ref="popup" mode="bottom" bgColor="none" >
      <view class="popup__view">
        <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="form-item">
              <uv-form-item prop="image" :customStyle="formItemStyle">
                <view class="form-item-label">
                  <image class="icon" src="@/pages_order/static/icon-require.png" mode="widthFix"></image>
                  上传图片
                </view>
                <view class="form-item-content">
                  <button class="flex btn">
                    <view v-if="form.image" class="avatar">
											<image class="img" :src="form.image" mode="aspectFill"></image>
											<view class="flex mask">
	        							<image class="icon" src="@/pages_order/static/icon-change.png" mode="widthFix" />
											</view>
										</view>
										<view v-else class="flex avatar is-empty">
        							<image class="icon" src="@/static/image/icon-plus.png" mode="widthFix" />
										</view>
                  </button>
                </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>
  export default {
    data() {
      return {
        form: {
          image: null,
        },
        rules: {
          'image': {
            type: 'string',
            required: true,
            message: '请上传图片',
          },
        },
        formItemStyle: { padding: 0 },
      }
    },
    methods: {
      onUpload() {
        uni.chooseImage({
          count: 1,
          sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
          success: res => {
            let image = res.tempFilePaths[0] // 将选择的图片赋值给我们定义的cover
    
            this.$Oss.ossUpload(image)
            .then(url => {
              this.form.image = url
            })
          }
        });
      },
      async onPublish() {
        try {
          await this.$refs.form.validate()

          const {
          } = this.form

          const params = {
          }

          // todo: fetch
          // await this.$fetch('updateAddress', 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;
    }

  }
  
  .form {
    max-height: 75vh;
    padding: 32rpx 40rpx;
    box-sizing: border-box;
    overflow-y: auto;

    &-item {
      padding: 8rpx 0 6rpx 0;
      
      & + & {
        padding-top: 24rpx;
        border-top: 2rpx solid #EEEEEE;
      }

      &-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 {
    
        .text {
          padding: 2rpx 0;
          font-family: PingFang SC;
          font-weight: 400;
          font-size: 32rpx;
          line-height: 1.4;

          &.placeholder {
            color: #C6C6C6;
          }
        }

      }
    }
  }

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

  .btn-avatar {
		display: inline-block;
		width: auto;
		border: none;
	}

	.avatar {
		position: relative;
		width: 200rpx;
		height: 200rpx;
		border-radius: 24rpx;
		overflow: hidden;

		.img {
			width: 100%;
			height: 100%;
		}
		
		.mask {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			background: #00000080;
			border-radius: 24rpx;

			.icon {
				width: 64rpx;
				height: 64rpx;
			}
		}

		&.is-empty {
			background: #F3F2F7;
			
			.icon {
				width: 61rpx;
				height: auto;
			}

		}
	}
</style>