<template>
  
  <view class="page__view">

		<navbar title="身份认证" leftClick @leftClick="$utils.navigateBack" color="#191919" bgColor="#FFFFFF" />

    <view class="main">

      <view class="card">
        <view class="card-header">
          <view>身份认证</view>
          <view class="card-desc">信息仅用于身份认证，平台将保障你的信息安全</view>
        </view>
        
        <view class="form">
          <uv-form 
            ref="form"
            :model="form"
            :rules="rules"
            errorType="toast"
          >
            <view class="form-item">
              <uv-form-item prop="idFront" :customStyle="formItemStyle">
                <view class="form-item-content">
                  <view class="upload" @click="onUpload('idFront')">
                    <image v-if="form.idFront" class="upload-img" :src="form.idFront" mode="scaleToFill" />
                    <view v-else class="flex upload-default">
                      <image class="upload-default-img" src="@/pages_order/static/order/id-front-default.png" mode="aspectFill" />
                    </view>
                  </view>
                </view>
                <view class="form-item-label">身份证国徽面（点击上传）</view>
              </uv-form-item>
            </view>
            <view class="form-item">
              <uv-form-item prop="idBack" :customStyle="formItemStyle">
                <view class="form-item-content">
                  <view class="upload" @click="onUpload('idBack')">
                    <image v-if="form.idBack" class="upload-img" :src="form.idBack" mode="scaleToFill" />
                    <view v-else class="flex upload-default">
                      <image class="upload-default-img" src="@/pages_order/static/order/id-back-default.png" mode="aspectFill" />
                    </view>
                  </view>
                </view>
                <view class="form-item-label">身份证人像面（点击上传）</view>
              </uv-form-item>
            </view>
          </uv-form>
        </view>
			</view>
    </view>
  
    <view class="bottom">
      <button class="btn" @click="onConfirm">确认</button>
    </view>

  </view>
</template>

<script>
  export default {
    data() {
      return {
        form: {
          idFront: null,
          idBack: null,
        },
        rules: {
          'idFront': {
            type: 'string',
            required: true,
            message: '请上传身份证国徽面',
          },
          'idBack': {
            type: 'string',
            required: true,
            message: '请上传身份证人像面',
          },
        },
        formItemStyle: { padding: 0 },
      }
    },
    methods: {
      onUpload(key) {
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					success: res => {
						let avatarUrl = res.tempFilePaths[0] // 将选择的图片赋值给我们定义的cover
		
						this.$Oss.ossUpload(avatarUrl)
						.then(url => {
							this.form[key] = url
						})
					}
				});
      },
      async onConfirm() {

        try {
          const res = await this.$refs.form.validate()

          console.log('onSave res', res)

          // todo: save

          // todo: check
          // this.$utils.redirectTo(`/pages_order/order/userInfo/facialVerify`)
    			this.$utils.redirectTo('/pages_order/order/userInfo/facialVerifyCustom')

        } catch (err) {
          console.log('onSave err', err)
        }

      },
    },
  }
</script>

<style lang="scss" scoped>

	.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) + 144rpx) 32rpx 224rpx 32rpx;
	}

	.card {
		padding: 32rpx;
		background: #FAFAFF;
		border: 2rpx solid #FFFFFF;
		border-radius: 32rpx;

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

		&-header {
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 36rpx;
			line-height: 1.4;
			color: #252545;
			margin-bottom: 48rpx;
		}

    &-desc {
      margin-top: 4rpx;
      font-family: PingFang SC;
      font-weight: 400;
      font-size: 26rpx;
      line-height: 1.4;
      color: #4E4E69;
    }
	}

	.form {
		padding: 8rpx 0 0 0;

		&-item {

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

      &-label {
        margin-top: 32rpx;
        text-align: center;
        font-family: PingFang SC;
        font-weight: 400;
        font-size: 30rpx;
        line-height: 1.4;
        color: #4E4E69;
      }
		}
	}

  .upload {
    position: relative;
    width: 100%;
    height: 386rpx;
    overflow: hidden;
    border-radius: 32rpx;
    box-shadow: -5rpx -5rpx 10rpx 0 #FFFFFF,
                10rpx 10rpx 20rpx 0 #AAAACC80,
                4rpx 4rpx 10rpx 0 #AAAACC40,
                -2rpx -2rpx 5rpx 0 #FFFFFF;

    &-img {
      width: 100%;
      height: 100%;
    }

    &-default {
      width: 100%;
      height: 100%;
      background-image: linear-gradient(#FAFAFF, #F3F3F3);

      &-img {
        width: 240rpx;
        height: 240rpx;
      }
    }
  }

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

		width: 100vw;
		// height: 200rpx;
		padding: 24rpx 40rpx;
		padding-bottom: calc(env(safe-area-inset-bottom) + 24rpx);
		background: #FFFFFF;
		box-sizing: border-box;

		.btn {
			width: 100%;
			padding: 16rpx 0;
			box-sizing: border-box;
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 36rpx;
			line-height: 1;
			color: #FFFFFF;
			background-image: linear-gradient(to right, #4B348F, #845CFA);
			border-radius: 41rpx;
		}
	}

</style>