<template>
  <view>
    <signatureCom @sumbit="sumbit" @fail="fail" cid="signature-cid" />
  </view>
</template>

<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import signatureCom from './components/signatureCom.vue'
import { uploadFile } from '@/utils/upload'
import { useTask } from '@/store/task'
import { useAuth } from '@/store/auth'

const taskStore = useTask()
const authStore = useAuth()

const type = ref()
const taskFiledKey = ref()

onLoad((e: any) => {
  // 任务过程中过来的
  if (e.type === 'task') {
    type.value = 'task'
    taskFiledKey.value = e?.taskFiledKey
  }
})

const sumbit = async (info: any) => {
  const res: any = await uploadFile(info.tempFilePath)
  if (res.code === 0) {
    if (type.value === 'task') {
      backTask(res.data)
    } else {
      backAuth(res.data)
    }
  }
}
// 返回task页面
const backTask = (data: any) => {
  const obj = {
    sendParamsKey: taskFiledKey.value,
    url: data.url
  }
  taskStore.setSign(obj)
  uni.navigateBack()
}

// 返回认证页面
const backAuth = (data: any) => {
  authStore.setAgreementUrl(data.url)
  uni.navigateBack()
}

const fail = (err: any) => {
  console.log('err', err)
}
</script>

<style lang="scss" scoped></style>
