import { VueLikePage } from '../../../utils/page'
import UserApi from '../../../apis/user'
import Router from '../../../utils/routes'
import { API_BASE_URL } from '../../../config/config'
VueLikePage([], {
  data: {
    isLoading: true,
    detail: {}
  },
  methods: {
    onLoad (options) {
      this.handle(options.filePath)
    },
    bindinput (e) {
      const { value } = e.detail
      const { key } = e.currentTarget.dataset
      const detail = this.data.detail
      detail[key] = value
      this.setData({
        detail
      })
    },
    handle (filePath) {
      wx.uploadFile({
        filePath: filePath,
        name: 'file',
        url: `${API_BASE_URL}/app/viewer/recognizeBusinessCard`,
        formData: {
          viewerId: getApp().globalData.userinfo.viewerId,
          token:  getApp().globalData.token
        },
        success: (res) => {
          console.log(res)
          const data = JSON.parse(res.data)
          const aliyunRsp = data.data.aliyunRsp
          const detail = {
            name: aliyunRsp.name,
            companyAddress: aliyunRsp.addresses.join(),
            companyName: aliyunRsp.companies.join(),
            email: aliyunRsp.emails.join(),
            phoneNum: aliyunRsp.cellPhoneNumbers.join(),
            telNum: aliyunRsp.officePhoneNumbers.join(),
            companyPosition: aliyunRsp.titles.join(),
            picUrl: data.data.picUrl,
            fromScan: 1
          }
          this.setData({
            isLoading: false,
            detail: detail
          })
        },
        fail (err) {
          console.log(err, 'err')
        }
      })
    },
    createCard () {
      return UserApi.createVisitCard(this.data.detail).then(res => {
        Router.push('businessCard')
      })
    }
  }
})