<script lang="ts" setup>
import {EventEnum} from '@/constant/enums'

const {t} = useI18n();
const avatarUrl = ref('')
const show = ref(true)

onLoad((options) => {
  if (options.data) {
    avatarUrl.value = decodeURIComponent(options.data)
  }
})

function handleConfirm(event: { tempFilePath: string }) {
  console.log('确认', event)
  const {tempFilePath} = event
  uni.navigateBack()
  setTimeout(() => {
    uni.$emit(EventEnum.CROPPER_AVATAR, tempFilePath)
  }, 10)
}

function imgLoadError(res: any) {
  console.log('加载失败', res)
  uni.showToast({title: '图片加载失败'})
}

function imgLoaded() {
  console.log('加载成功')
}

function handleCancel() {
  uni.navigateBack()
}
</script>

<template>
  <view>
    <navbar :title="t('common.cuttingImage')"/>
    <wd-img-cropper
        v-model="show"
        :img-src="avatarUrl"
        @cancel="handleCancel"
        @confirm="handleConfirm"
        @imgloaded="imgLoaded"
        @imgloaderror="imgLoadError"
    ></wd-img-cropper>
  </view>
</template>

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