// index.js
// 获取应用实例
const app = getApp()
import {
  VueLikePage
} from '../../utils/page'

import { CDN_URL,API_BASE_URL,TYPE } from '../../config/index'
import {isPhoneX} from './../../utils/tools'

import Router from '../../utils/routes'


VueLikePage([],{
  data:{
    cdn_url:CDN_URL,
    isBackCamera:true,
    current:'1',
    currentImg:''
  },
  methods:{
    onLoad:function (options) {
      let {id} = options
      this.setData({
        current:id
      })
      isPhoneX().then(res => {
        this.setData({
            isIphoneX: res
        })
      })
    },
    back(){
      wx.navigateBack()
    },
    changPosition(){
      this.setData({
        isBackCamera:!this.data.isBackCamera
      })
    },
    reTake(){
      this.setData({
        currentImg:''
      })
    },
    submit(){
      wx.showLoading({
        title: '处理中…',
        mask:true
      });
        
      wx.uploadFile({
        url: `${API_BASE_URL}/api/uploadFaceSwap/${TYPE[this.data.current]}`, //仅为示例，非真实的接口地址
        filePath: this.data.currentImg,
        name: 'file',
        success: (res)=>{
          let data = JSON.parse(res.data)
          if (data.code==0) {
            this.setData({
              work:data.msg
            })

            Router.push({
              url: 'work',
              query: {
                vr_link:data.msg
              }
            })
          } 
        },
        fail:()=>{
          wx.showModal({
            title: '生成失败，请稍后重试'
          });
        },
        complete(){
          wx.hideLoading();
        }
      })
      
    },
    selectImage(){
      wx.chooseImage({
        sourceType:["album"],
        count: 1,
        sizeType: ['original', 'compressed'],
        success:res =>{
          this.setData({
            currentImg: res.tempFilePaths[0]
          })
        },
        fail:error =>{
          console.log(error);
        }
      })
    },
    takePhoto() {
      const ctx = wx.createCameraContext()
      ctx.takePhoto({
        quality: 'high',
        success: (res) => {
          this.setData({
            currentImg: res.tempImagePath
          })
        }
      })
    }
  }
})

