<template>
  <view class="page">
		<navbar title="文章分享" leftClick @leftClick="$utils.navigateBack" />

    <view class="content">
      <uv-form 
        ref="form"
        :model="form"
        :rules="rules"
        labelPosition="left" 
        labelWidth="300rpx"
        :labelStyle="{
          color: '#1B1B1B',
          fontSize: '32rpx',
          fontWeight: 'bold',
        }"
      >
        <view class="form-item">
          <uv-form-item label="用户ID" prop="id">
            <view class="form-item-content">
              <template v-if="form.id">
                <text>{{ form.id }}</text>
                <view style="margin-left: 20rpx;">
                  <button class="btn-simple" plain @click="$utils.copyText(form.id)">
                    <uv-icon name="file-text" color="#05D9A2" size="28rpx"></uv-icon>
                  </button>
                </view>
              </template>
            </view>
          </uv-form-item>
        </view>
        <view class="form-item">
          <uv-form-item label="选择封面图" prop="imageUrl">
            <view class="form-item-content">
              <formUpload v-model="form.imageUrl">
                <template v-slot="{ value }">
                  <view class="flex" style="min-width: 116rpx; height: 45rpx;">
                    <image 
                      :src="value" 
                      mode="aspectFill" 
                      style="width: 68rpx; height: 68rpx;"
                      radius="14rpx"
                    />
                    <uv-icon style="margin-left: 20rpx" name="arrow-right" color="#000000" size="28rpx"></uv-icon>
                  </view>
                </template>
              </formUpload>
            </view>
          </uv-form-item>
        </view>
        <view class="form-item">
          <uv-form-item label="标题" labelWidth="84rpx" prop="title">
            <view class="form-item-content">
              <formInput v-model="form.title" placeholder="请输入你的文章标题" width="584rpx"></formInput>
            </view>
          </uv-form-item>
        </view>
        <view class="form-item">
          <uv-form-item label="设置转发次数（次）" prop="times">
            <view class="form-item-content">
              <formNumberBox v-model="form.times" ></formNumberBox>
            </view>
          </uv-form-item>
        </view>
        <view class="form-item">
          <uv-form-item label="选择二维码" prop="qrCode">
            <view class="form-item-content">
              <formUpload v-model="form.qrCode">
                <template v-slot="{ value }">
                  <view class="flex" style="min-width: 93rpx; height: 45rpx;">
                    <image 
                      :src="value" 
                      mode="aspectFill" 
                      style="width: 45rpx; height: 45rpx;"
                      radius="14rpx"
                    />
                    <uv-icon style="margin-left: 20rpx" name="arrow-right" color="#000000" size="28rpx"></uv-icon>
                  </view>
                </template>
              </formUpload>
            </view>
          </uv-form-item>
        </view>
        <view class="form-item">
          <uv-form-item label="文章内容" prop="description" labelPosition="top">
            <view class="editor__view" style="margin-top: 32rpx;">
              <editor id="editor" class="editor"
                placeholder="请输入你的文章内容"
                @ready="onEditorReady"
                @input="onEditroInput"
              ></editor>
              <view class="flex editor-tools">
                <view style="flex: 1;">
                  <button @click="insertImage" plain class="btn-simple" style="float: left; font-size: 0;">
                    <image src="../static/record/icon-add.png" style="width: 126rpx; height: 126rpx;" ></image>
                  </button>
                </view>
                <view style="flex: 1; text-align: right;">
                  <text class="editor-count">{{ `${descLen}/${descLengthLimit}` }}</text>
                </view>
              </view>
            </view>
          </uv-form-item>
        </view>
      </uv-form>
    </view>

    <!-- 审核通过 -->
    <button v-if="auditStatus === 1" class="button-submit" @click="onPublish">
      发布
    </button>
    <!-- 不是 审核中 或 已发布 -> 即 创建分享 或 审核不通过 -->
    <button v-else-if="![0,2].includes(auditStatus)" class="button-submit" @click="onSubmit">
      提交审核
    </button>
  </view>
</template>

<script>
  import formInput from '../components/formInput.vue'
  import formNumberBox from '../components/formNumberBox.vue'
  import formUpload from '../components/formUpload.vue'
  import formTextarea from '../components/formTextarea.vue'

	export default {
    components: {
      formInput,
      formNumberBox,
      formUpload,
      formTextarea,
    },
		data() {
      return {
        id: null,
        auditStatus: null,
        form: {
          id: null,
          imageUrl: null,
          title: null,
          times: 10,
          qrCode: null,
          description: null,
        },
        rules: {
          // todo
        },
        editorCtx: null,
        descLen: 0,
        descLengthLimit: 1000,
      }
    },
		onLoad(option) {
      const { id } = option

      this.id = id
      // todo: init data by id
		},
    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: '图像',
              })
            })
					}
				})
			},
      getEditroContents() {
        return new Promise((resolve, reject) => {
          this.editorCtx.getContents({
            success: (e) => {
              const { html, text } = e
              resolve({ html, text })
            },
            fail: () => {
              reject()
            }
          })
        })
      },
      async onSubmit() {
        try {

          const description = (await this.getEditroContents())?.html
          // todo: decode?

          console.log(description)

          return

          const params = { ...this.form, description }
        
          // todo
          
          this.$api('submitPersonalSharing', params)
        } catch (err) {

        }
      },
      onPublish() {
        // todo
      },
    }
  }

</script>

<style scoped lang="scss">
@import '../styles/pageForm.scss';

.editor__view {
  background-color: #F3F3F3;
  border-radius: 12rpx;
}

.editor {
  height: 253rpx;
  padding: 20rpx;

  &-tools {
    align-items: flex-end;
    padding: 0 20rpx 16rpx 14rpx;
  }

  &-count {
    color: #999999;
    font-size: 28rpx;
  }
}

</style>