// 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:false,
    current:'1',
    currentImg:'',
    isSelect: false
  },
  methods:{
    onLoad:function (options) {
      let {id} = options
      this.setData({
        current:id
      })
      isPhoneX().then(res => {
        this.setData({
            isIphoneX: res
        })
      })
    },
    onShow:function () {
      // this.reTake()
    },
    loadcompele(){
      this.setData({
        loadCompele: true
      })
    },
    back(){
      if (this.data.currentImg) {
        this.reTake()
      }
      else{
        wx.navigateBack()
      }
    },
    changPosition(){
      this.setData({
        isBackCamera:!this.data.isBackCamera
      })
    },
    reTake(){
      this.setData({
        currentImg:'',
        isSelect:false
      })
    },
    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,
                id: this.data.current
              }
            })
          } 

          else{
            wx.showModal({
              title: '无法识别人脸，请正对摄像头拍照',
              showCancel:false
            });
          }
        },
        fail:()=>{
          wx.showModal({
            title: '无法识别人脸，请正对摄像头拍照',
            showCancel:false
          });
        },
        complete(){
          wx.hideLoading();
        }
      })
      
    },
    selectImage(){
      wx.chooseImage({
        sourceType:["album"],
        count: 1,
        sizeType: ['original', 'compressed'],
        success:res =>{
          console.log(res)
          this.setData({
            currentImg: res.tempFilePaths[0],
            isSelect:true
          })

          console.log(this.data.currentImg,this.data.isSelect)

        },
        fail:error =>{
          console.log(error);
        }
      })
    },
    takePhoto() {
      const ctx = wx.createCameraContext()
      ctx.takePhoto({
        quality: 'high',
        success: (res) => {
          this.setData({
            currentImg: res.tempImagePath,
            isSelect:false
          })
        }
      })
    }
  }
})

