// pages/writeReading/components/photo/index.js
import { uploadOcrFile } from "../../../../service/public"
import request from "../../../../utils/request"
import { alertError, alertInfo, alertSuccess, loadingFunc } from "../../../../utils/index"
const { OK } = request;
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    chooseImage() {
      const that = this;
      wx.chooseMedia({
        count: 1,
        mediaType: ['image'],
        sourceType: ['album', 'camera'],
        success: async function(res) {
          wx.cropImage({
            src: res.tempFiles[0].tempFilePath, // 图片路径
            cropScale: '16:9', // 裁剪比例
            success: function (res) {
              if (!/(\.jpg|\.png|\.jpeg)$/.test(res.tempFilePath.toLowerCase())) {
                wx.showToast({
                  title: '请上传jpg、png或jpeg格式的图片',
                  icon: 'none',
                });
                return;
              }
              var tempFilePaths = res.tempFilePath;
              loadingFunc(async () => {
                const { code, message, data, url } = await uploadOcrFile(tempFilePaths)
                if (code !== OK) {
                  alertError(message)
                  return
                }
                setTimeout(() => {
                  that.triggerEvent("finish", { number: data?.integer + "." + data?.decimal || 0, success: data?.integer != null && data?.integer !== "", url })
                }, 100)
              })
   
            }
    
          })
  
          // loadingFunc(async () => {
          //   const { code, message, data } = await uploadOcrFile(res.tempFiles[0].tempFilePath)
          //   if (code !== OK) {
          //     alertError(message)
          //     return
          //   }
          //   setTimeout(() => {
          //     that.triggerEvent("finish", { number: data?.integer, success: data?.integer != null && data?.integer !== "" })
          //   }, 100)
          // })
          
        }
      })
    }
  }
})