<template>
  <view class="page__view">
    <navbar title="意见反馈" leftClick @leftClick="$utils.navigateBack" bgColor="transparent" />

    <view class="bg">
      <view class="title">Hello</view>
      <view class="desc">有什么好的建议可以告诉我们哦～</view>
    </view>

    <view class="main">
      <view class="form">
        <uv-form 
          ref="form"
          :model="form"
          errorType="toast"
        >
          <view class="form-item">
            <uv-form-item prop="textDetails" :customStyle="formItemStyle">
              <view class="card">
                <view class="editor-header">
                  <view class="editor-header-content">
                    <view>反馈内容</view>
                    <view class="editor-icon">
                      <uv-icon name="star-fill" color="#FFFFFF" size="26rpx"></uv-icon>
                    </view>
                  </view>
                </view>
                <view class="editor__view">
                  <editor id="editor" class="editor"
                    placeholder="请填写10个字以上的内容，以便我们为您提供更好的服务"
                    @ready="onEditorReady"
                    @input="onEditroInput"
                  ></editor>
                  <view class="editor-tools">
                    <button @click="insertImage" plain class="flex btn">
                      <!-- todo: 缺切图 -->
                      <uv-icon name="camera-fill" color="#014FA2" size="56rpx"></uv-icon>
                    </button>
                  </view>
                </view>
              </view>
            </uv-form-item>
          </view>
          <view class="form-item">
            <uv-form-item prop="phone" :customStyle="formItemStyle">
              <view class="flex card phone">
                <view class="label">联系电话</view>
                <input 
                  v-model="form.phone" 
                  placeholder-style="color: #999999; font-size: 28rpx;"
                  placeholder="请输入手机号" 
                />
              </view>
            </uv-form-item>
          </view>
        </uv-form>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        form: {
          textDetails: null,
          phone: null,
				},
        formItemStyle: { padding: 0 },
      }
    },
		onReady() {
			this.$refs.form.setRules(this.getRules());
		},
    methods: {
      onEditorReady() {
				uni.createSelectorQuery().select('#editor').context((res) => {
					this.editorCtx = res.context
				}).exec()
			},
      onEditroInput(e) {
        const { text } = e.detail

        this.descLen = text?.length || 0
      },
			insertImage() {
				uni.chooseImage({
					count: 1,
					success: (res) => {
						// this.editorCtx.insertImage({
						// 	src: res.tempFilePaths[0],
						// 	alt: '图像',
						// })

            // todo: check
            this.$Oss.ossUpload(res.tempFilePaths[0]).then(url => {
              this.editorCtx.insertImage({
                src: url,
                alt: '图像',
              })
            })
					}
				})
			},
      setEditorContents(html) {
        if (!this.editorCtx) {
          setTimeout(() => {
            this.setEditorContents(html)
          }, 200)

          return
        }

        this.editorCtx.setContents({ html })
      },
      getEditorContents() {
        return new Promise((resolve, reject) => {
          this.editorCtx.getContents({
            success: (e) => {
              const { html, text } = e
              resolve({ html, text })
            },
            fail: () => {
              reject()
            }
          })
        })
      },
      getRules() {
        const textDetailsValidator = async (rule, value, callback) => {
          const textDetails = (await this.getEditorContents())?.html

          if (textDetails) {
            callback()
            return
          }

          callback(new Error('请填写10个字以上的内容，以便我们为您提供更好的服务'))
        }

        return {
					'phone': {
						type: 'string',
						required: true,
						message: '请输入手机号',
					},
					'textDetails': {
            asyncValidator: textDetailsValidator,
					},
				}
      },
      async onSubmit(headImage) {
        try {

          await this.$refs.form.validate()

          const textDetails = (await this.getEditorContents())?.html

          const {
            phone,
          } = this.form

          const params = {
            phone,
            textDetails,
          }

          // todo
          // await this.$fetch('saveOrUpdateArticleShare', params)
          
          uni.showToast({
            title: '提交成功',
            icon: 'none'
          })

          setTimeout(uni.navigateBack, 1000, -1)
						
        } catch (err) {

        }
      },
    },
  }
</script>

<style scoped lang="scss">

  .page__view {
    background: #F5F5F5;
    
    /deep/ .nav-bar__view {
      position: fixed;
      top: 0;
      left: 0;
    }
  }

  .bg {
    width: 100%;
    height: 501rpx;
    padding-top: 203rpx;
    box-sizing: border-box;
    color: #FFFFFF;
		background: linear-gradient(164deg, #014FA2 30%, #4C8FD6);

    .title {
      padding: 0 73rpx;
      font-size: 64rpx;
      font-weight: 600;
    }

    .desc {
      margin-top: 18rpx;
      padding: 0 68rpx;
      box-sizing: border-box;
      font-size: 28rpx;
    }
  }

  .main {
    width: 100%;
    padding: 0 28rpx;
    box-sizing: border-box;
    background: #F5F5F5;
  }

  .form {
    // transform: translateY(-98rpx);
    transform: translateY(-46rpx);

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

  .editor__view {
    padding: 43rpx 30rpx;
    background-color: #FFFFFF;
    border-radius: 12rpx;
  }

  .editor-header {
    position: absolute;
    top: 0;
    left: 12rpx;
    transform: translateY(-100%);
    // margin-left: 12rpx;

    &-content {
      position: relative;
      padding: 9rpx 20rpx 3rpx 20rpx;
      font-size: 28rpx;
      font-weight: 600;
      color: #FFFFFF;
      border-top-left-radius: 30rpx;
      background: linear-gradient(to right, #014FA2, #6A9ACE);
    }
  }

  .editor-icon {
    position: absolute;
    top: 0;
    right: 0;
    transform: translate(30rpx, -4rpx);
  }

  .editor {
    height: 462rpx;

    &-tools {
      padding: 0 7rpx;
      .btn {
        width: 133rpx;
        height: 133rpx;
        border: 2rpx dashed #014FA2;
      }
    }

  }

  .card {
    position: relative;
  }

  .phone {
    justify-content: flex-start;
    padding: 44rpx 27rpx;
    column-gap: 31rpx;
    background: #FFFFFF;
    border-radius: 15rpx;

    .label {
      font-size: 28rpx;
      color: #000000;
    }
  }

</style>