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

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

import Toast from '../../components/toast/toast';

VueLikePage([], {
  data: {
    cdn_url: CDN_URL,
    baseUrl: API_BASE_URL+'/',
    top:0
  },
  methods: {
    onLoad: function (options) {
      let {
        vr_link,
        id
      } = options
      
      this.setData({
        vr_link,
        id,
        show_img: this.data.baseUrl + vr_link + IMGSTR.normal
      })
      let left = app.globalData.deviceInfo.windowWidth - app.globalData.top.right
      isPhoneX().then(res => {
        this.setData({
          isIphoneX: res,
          top:app.globalData.top,
          left
        })
      })
    },
    loadcompele(){
      this.setData({
        loadCompele: true
      })
    },
    saveAlbum(e) {
      const { type } = e.currentTarget.dataset
      let img = this.data.baseUrl + this.data.vr_link + (!type?IMGSTR.normal:IMGSTR.qrcode)
      let toast = Toast.loading({
        message:'保存中…',
        selector: '#loading-toast'
      });
      
      wx.downloadFile({
        url: img,
        type: 'image',
        success: function (res) {
          var tempFilePath = res.tempFilePath;
          wx.saveImageToPhotosAlbum({
            filePath: tempFilePath,
            success(res) {
              Toast.success({
                message:type?'已保存,快去分享吧':'已保存到相册',
                selector: '#success-toast'
              });
            },
            fail: (err) => {
              Toast.fail({
                message:'保存失败，请稍后再试',
                selector: '#fail-toast'
              });
            },
            complete: () => {
              toast.hideLoading();
            }
          })
        },
        fail: function (res) {
          Toast.fail({
            message:'下载失败，请稍后再试',
            selector: '#fail-toast'
          });
          toast.hideLoading();
        }
      });

    },
    back(){
      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 2];
      prevPage.setData({
        form:"work"
      })
      wx.navigateBack({  
        delta: 1
      })
      // Router.redirectTo({
      //   url:'camera',
      //   query:{
      //     id:this.data.id
      //   }
      // })
    },
    backtoexample(){
      wx.navigateBack({
        delta: 3
      })
      // Router.redirectTo({
      //   url:'example',
      //   query:{
      //     id:this.data.id
      //   }
      // })
    }
  }
})