<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
    <view>
      <page-head :title="title"></page-head>
      <view class="uni-padding-wrap uni-common-mt">
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="addTodo">添加Todo</button>
        </view>
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="randomFail">随机触发失败重试</button>
        </view>
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="fail">云对象失败调用</button>
        </view>
        <view class="uni-btn-v uni-common-mt">
          <button type="primary" @tap="success">云对象成功调用</button>
        </view>
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script>
  export default {
    data() {
      return {
        title: '请求云对象',
        todoTitle: '学习编程',
        todoContent: '熟悉uts语法',
        returnTodoTitle: '',
        returnTodoContent: '',
        failErrCode: '',
        successErrCode: -1,
        isUniTest: false
      }
    },
    onUnload() {
      if(this.isUniTest){
        uni.hideToast()
      }
    },
    methods: {
      notify(content : string, title : string) {
        if (!this.isUniTest) {
          uni.showModal({
            title,
            content,
            showCancel: false
          })
        } else {
          uni.showToast({
            title: content
          })
          console.log(title, content)
        }
      },
      addTodo() {
        const todo = uniCloud.importObject('todo', {
          customUI: this.isUniTest
        })
        const title = this.todoTitle
        const content = this.todoContent
        todo.add(title, content).then((res : UTSJSONObject) : void => {
          this.returnTodoTitle = res['title'] as string
          this.returnTodoContent = res['content'] as string
          this.notify(res['showMessage'] as string, '提示')
        }).catch((err : any | null) : void => {
          const error = err as UniCloudError
          console.error(error)
        })
      },
      randomFail() {
        const todoObj = uniCloud.importObject('todo', {
          errorOptions: {
            retry: true
          }
        })
        todoObj.randomFail().then((res : UTSJSONObject) : void => {
          this.notify(res['showMessage'] as string, '提示')
        }).catch((err : any | null) : void => {
          const error = err as UniCloudError
          console.error(error)
        })
      },
      fail() {
        const todo = uniCloud.importObject('todo', {
          customUI: this.isUniTest
        })
        todo.fail().then((res : UTSJSONObject) : void => {
          this.notify('todo.fail应调用失败，此处错误的触发了成功回调', '错误')
          console.log('todo.fail: ', res);
        }).catch((err : any | null) : void => {
          const error = err as UniCloudError
          this.failErrCode = error.errCode as string
          console.error(error)
          if (this.isUniTest) {
            uni.showToast({
              title: error.errMsg
            })
          }
        })
      },
      success() {
        const todo = uniCloud.importObject('todo', {
          customUI: this.isUniTest
        })
        todo.success().then((res : UTSJSONObject) : void => {
          this.successErrCode = res['errCode'] as number
          this.notify(res['showMessage'] as string, '提示')
        }).catch((err : any | null) : void => {
          const error = err as UniCloudError
          console.error(error)
        })
      }
    }
  }
</script>

<style>

</style>
