var sceneReadyBehavior = require('../../behavior-scene/scene-ready');
// 自定义组件
import '../../../xr-custom/components/AutoRotate';
import '../../../xr-custom/components/Particle/SystemProperty/BasicParticle';
import '../../../xr-custom/components/Particle/index';
import '../../../xr-custom/elements/xr-auto-rotate-touchable-gltf';
import '../../../xr-custom/elements/xr-custom-particle';
// 自定义Geometry
import '../../../xr-custom/assets/geometry-star';
import '../../../xr-custom/assets/geometry-specialCylinder';
// 自定义材质
import '../../../xr-custom/elements/xr-shining-star';
// 自定义effect
import '../../../xr-custom/assets/effect-shining';
import '../../../xr-custom/assets/effect-last-record-final';
//var capate = require("G:/GUnityProject/WXXR/miniprogram/components/xr-classic-show/index.js");
Page({
  behaviors:[sceneReadyBehavior],
  data:{   
    captureState: 0,
    recordState: 0,
    captureQuality: 0.8,
    captureType: 'jpg',
    recordFPS: 30,
    recordScale: 1,
    recordWidth: 0,
    recordHeight: 0,
    recordBPS: 30000,
    sceneWidth: 0,
    sceneHeight: 0,

    zhaohuanstate:true,
    paizhaostate:false,
    luxiangstate:false,
    isStartluxiang:false,
    isCloseluxiang:false,


    uiShowModelView:false,
    UiShowModel_Bottom:false,
    uiMainView:true,
    uiShowNum:3,
    uiShowIndex:0,

    current:0,
    products:[],
    modelImg:[
      'https://wx-model-1317557471.cos.ap-shanghai.myqcloud.com/WXImage/%E9%80%89%E6%8B%A9%E7%A5%9E%E5%85%BD-%E5%A4%AB%E8%AF%B8%E9%A2%84%E8%A7%88%E5%9B%BE.png',
      'https://wx-model-1317557471.cos.ap-shanghai.myqcloud.com/WXImage/%E9%80%89%E6%8B%A9%E7%A5%9E%E5%85%BD-%E9%B8%BE%E9%B8%9F%E9%A2%84%E8%A7%88%E5%9B%BE.png',
      'https://wx-model-1317557471.cos.ap-shanghai.myqcloud.com/WXImage/%E8%A7%A3%E9%94%81%E7%A5%9E%E5%85%BD-%E5%BA%94%E9%BE%99%E9%A2%84%E8%A7%88%E5%9B%BE2.png',
      'https://wx-model-1317557471.cos.ap-shanghai.myqcloud.com/WXImage/%E9%80%89%E6%8B%A9%E7%A5%9E%E5%85%BD-%E7%8B%BB%E7%8C%8A%E9%A2%84%E8%A7%88%E5%9B%BE.png',
    ],
    log:"123",
  },
  onLoad(){
    wx.setInnerAudioOption({
      obeyMuteSwitch:false
     })
     this.bgm = wx.createInnerAudioContext({});
     this.bgm.src = 'https://wx-model-1317557471.cos.ap-shanghai.myqcloud.com/WXBGM.MP3';
     this.bgm.play();
     this.bgm.loop=true;
      
  },
  onShow(){
    this.setData({log:'OnShow'});
    var that = this;
    this.bgm.play();
    wx.login({
      success: (res) => {
        if (res.code) {
          // 获取到用户的临时登录凭证 code，可以发送给服务器
          console.log('用户登录成功，code：', res.code);
      
          wx.request({
            url: 'https://api-fat1.ghz-tech.com/miniprogram-shanhaijing/v1/product/listInfo',
            method:'POST',
            header:{
              'Content-Type':'application/json'
            },
            data:{
              topic:1,
              js_code:res.code,
              app_id:'wx1a11cc00f433dee9'
            },
            success:(res)=>{ 
                console.log('POST 请求成功', res.data);
                that.setData({log:'POST 请求成功'});
                var products2 = res.data.data.products;
                var modelImgs = this.data.modelImg;
                console.log(' 商品列表 ',products2[0].desc);
                
                for(var i =0;i<products2.length;i++){
                  products2[i]["modelImg"] = modelImgs[i];
                  var formattedNumber = products2[i].price/100;
                  formattedNumber = parseFloat(formattedNumber).toFixed(2);
                  products2[i].price = formattedNumber;
                  var modelShowIndex =  products2[i]["id"];
                  if(products2[i].activate){
                    that.setData({uiShowIndex:modelShowIndex});
                  }
                }
                that.setData({products:products2});      
                console.log(' 商品列表 ',this.data.products[0]);
                that.setData({UiShowModel_Bottom:true});
      
            },  
            fail:function(err){
              console.error('POST 请求失败', err);
              var errorlog = err.toString();
              that.setData({log:errorlog});
            }
          });
        } else {
          console.log('登录失败！' + res.errMsg);
        }
      },
    });

   
  },
  onUnload(){ 
    this.bgm.stop(); 
  },

  handleReady: function({detail}) {
    this.setData({
      sceneWidth: detail.width,
      sceneHeight: detail.height,
      recordWidth: detail.width,
      recordHeight: detail.height,
    });

  },
  handleProgress: function({detail}) {
    console.log('assets progress', detail);

    this.setData({progressInfo: `${~~(detail.progress * 100)} %\n`});
  },
  handleLoaded: function({detail}) {
    console.log('assets loaded', detail);

    this.setData({loaded: true});
  },
  handlestr: function({detail}) {
    //console.log('assets handlestr', detail);

    this.setData({
      loadedSaoMiao: true,
    });
  },
  
  clickZhaoHuan(e){
    this.setData({zhaohuanstate:true});
    this.setData({paizhaostate:false});
    this.setData({luxiangstate:false});
  
  },
  clickPaiZhao(e){
    this.setData({zhaohuanstate:false});
    this.setData({paizhaostate:true});
    this.setData({luxiangstate:false});
    

  
  },
  clickLuXiang(e){
     this.setData({luxiangstate:true});
      this.setData({zhaohuanstate:false});
      this.setData({paizhaostate:false});
  },
  ClickzhaohuanSS(e){
    this.setData({zhaohuanstate:false});
    this.setData({uiMainView:false});
    this.setData({uiShowModelView:true});
    this.setData({uiShowNum:1});
  },
  Clickpaizhaoss(e){
   
   // this.capture();
   /* this.scene.share.captureToFriends({
      fileType: 'png',
      quality: 0.8
    });
    //const base64 = scene.share.captureToDataURL();
    if(supported){
       // 判断当前客户端是否支持分享系统
   // const supported = scene.share.supported;
   await this.scene.share.captureToFriends({
    fileType: this.data.captureType,
    quality: this.data.captureQuality
  });
    }*/
    //capate.capate();
    //this.setData({paizhaostate:false});
    
    if (this.data.recordState) {
      wx.showToast({
        title: '录屏中不允许！',
      });
      return;
    }

    if (this.data.captureState) {
      wx.showToast({
        title: '等待上次完成！',
      });
      return;
    }

    this.setData({captureState: 1});
    // hack，其实应该等待异步方法完成
    setTimeout(() => {
      this.setData({captureState: 0});
    }, 1000);
    
  },
  Clickstartluxiang(e){
  
   this.setData({isStartluxiang:true});
   this.setData({recordState: 1});
  },
  Clickcloseluxiang(e){

    this.setData({isStartluxiang:false});
    this.setData({recordState: 0});
    wx.showToast({
      title: '视频已保存至相册',
    });
  },
  changeCaptureState(e) {
    if (this.data.recordState) {
      wx.showToast({
        title: '录屏中不允许！',
      });
      return;
    }

    if (this.data.captureState) {
      wx.showToast({
        title: '等待上次完成！',
      });
      return;
    }

    this.setData({captureState: 1});
    // hack，其实应该等待异步方法完成
    setTimeout(() => {
      this.setData({captureState: 0});
    }, 1000);
  },
  changeRecordState(e) {
    this.setData({recordState: this.data.recordState ? 0 : 1});
  },
  clickCloseUIShow(e){
    this.setData({uiMainView:true});
    this.setData({zhaohuanstate:true});
    this.setData({uiShowModelView:false});
  },
  clickJiSuoModel(e){

  },
  changeMoel:function(event){
    var current2 = event.detail.current;
    this.setData({
      current:current2
    })
  },
  clickShowLeft(e){
   
    var leftoldIndex = this.data.uiShowNum;
    if(leftoldIndex ==0){
      
    }
   
    switch(this.data.uiShowIndex){
      case 0:
        this.setData({uiShowIndex:2});
        break;
      case 2:
        this.setData({uiShowIndex:1});
        break;
      case 1:
        this.setData({uiShowIndex:0});
        break;
    }
    let current = this.data.current;
    current = current > 0 ? current - 1 : 0;
    this.setData({
      current: current,
    });
  },
  clickShowRight(e){
    var  oldIndex = this.data.uiShowIndex;
    oldIndex = oldIndex+1;    
    var nIndex = oldIndex%3;
    this.setData({uiShowIndex:nIndex});  

    let current = this.data.current;
    current = current < 2 ? current + 1 : 2; // 假设总共有3页
    this.setData({
      current: current,
    });
   
  },clickJiSuoModel(e){
    var that = this;
    wx.login({
      success: (res) => {
        if (res.code) {
          // 获取到用户的临时登录凭证 code，可以发送给服务器
          console.log('用户登录成功，code：', res.code);
          var modelId = this.data.products[this.data.current].id;
          console.log('当前ModelId ', modelId);
            wx.request({
            url: 'https://api-fat1.ghz-tech.com/miniprogram-shanhaijing/v1/order/prepay',
            method:'POST',
            header:{
              'Content-Type':'application/json'
            },
            data:{
              shop_cart:[{
                id:modelId,
                count:1,     
              }],
              js_code:res.code,
              app_id:'wx1a11cc00f433dee9'
            },
            success:function(res){
              console.log('POST 请求成功', res.data);
              var paySignStr = res.data.data.pay_Sign;
              console.log('即将调取微信支付',res.data.data.pay_Sign);
             
            wx.requestPayment({
                nonceStr: res.data.data.nonce_str,
                package: res.data.data.package,
                paySign: res.data.data.pay_sign,
                timeStamp: res.data.data.time_stamp,
                signType:res.data.data.sign_type,
                success(res){
                  console.log( '微信支付请求成功',res );
                  // 付款成功回调

                    var products2 = that.data.products;
                  products2[that.data.current].activate = true;
                 that.setData({products:products2});
                  console.log('AAAA ',that.data.products);
                  var uiShowIndex2 = products2[that.data.current].id;
                  that.setData({uiShowIndex:uiShowIndex2});
                },
                fail(err){
                  console.log('微信支付请求失败',err);
                }
              })
            },
            fail:function(err){
              console.error('POST 请求失败', err);
            }
          });
        } else {
          console.log('登录失败！' + res.errMsg);
        }
      },
    });
  }

});

