import Card from '../../../../palette/inviteFriends';
var app = getApp()
const wxService = require('../../../../utils/wxService')
const utils = require('../../../../utils/util')
import { Integer } from '../../../../utils/integerDigitalConvertion'
const { member } = wx.getStorageSync('_baseUserInfo')
const { avatarUrl } = wx.getStorageSync('_userInfo')

// src/pages/xml2can/xml2can.js
wxService.page({
  data: {
    //轮播图
    swiperCurrent: 0,
    currentTab: 0,
    indicatorDots: false,
    saveBtnDisabled : true,//保存海报默认disabled 
    autoplay: false,
    interval: 2000,
    duration: 1000,
    circular: true,
    beforeColor: "white",//指示点颜色 
    afterColor: "coral",//当前选中的指示点颜色 
    previousmargin: '142rpx',//前边距
    nextmargin: '142rpx',//后边距
    posterImgData:[],
    currentPosterImg: '',
    explain: [],
    codeImg:'', //二维码
    template :{}
  },

  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
    
  },

  onLoad: function(){
    this.getMiniCode()
    this.getShareImg()
    wx.hideShareMenu();
    
  },

  //轮播图的切换事件 
  swiperChange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current, //获取当前轮播图片的下标
      currentTab: e.detail.current,
      currentPosterImg: this.data.posterImgData[e.detail.current]
    },() => {
      this.generatePoster();
    })
  },

  // 点击更换海报
  switchPoster(e){
    this.setData({
      currentTab: e.currentTarget.dataset.current,
      swiperCurrent: e.currentTarget.dataset.current,
      currentPosterImg: e.currentTarget.dataset.img
    },() => {
      this.generatePoster();
    })
  },

  //生成海报
  generatePoster(){
    const { member } = wx.getStorageSync('_baseUserInfo')
    const { avatarUrl } = wx.getStorageSync('_userInfo')
    let _this = this;
    wx.getSystemInfo({
      success: function (system) {
        wx.getImageInfo({
          src: _this.data.currentPosterImg,
          success: function (res) {
            let posterImgWidth = res.width,
              posterImgHeight = res.height;
            _this.setData({
              template: new Card().palette(_this.data.currentPosterImg,
                _this.data.codeImg,
                member.memberWeixin.nickname,
                avatarUrl, { 
                  posterImgWidth: posterImgWidth, 
                  posterImgHeight: posterImgHeight ,
                  windowWidth: system.windowWidth,
                  windowHeight: system.windowHeight
                }, _this.data.explain),
            });
          }
        })
      }
    });
  },
  
  // 获取海报图片
  getShareImg() {
    wxService.get(`/member/share/inviteFriends/buyer/getValid`).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          let initiatorPage = JSON.parse(data.initiatorPage)
          let explain = JSON.parse(data.explain)
          this.setData({
            posterImgData: initiatorPage,
            explain: explain
          })
        }
      }
    })
  },

  //获取二维码
  getMiniCode() {
    const urls = utils.getCurrentPageUrlWithArgs()
    let _this = this;
    let data = {
      "autoColor": true,
      "page": 'pages/userCenter/userCenter',
      "scene": 'type=-1',
      "width": 100
    }
    wxService.post(`/marketing/quickMark/getAppQrCodePicture`, data).then(res => {
      const { result, data } = res.data
      if (result == 0) {
        this.setData({
          codeImg: app.globalData.imageUrl + data
          // codeImg: "https://hwimagecdn.ihotwind.cn/usr/201908/miniQrCode-f00c5051fef7479395efb193431762c4.jpg"
        },()=>{
          const { member } = wx.getStorageSync('_baseUserInfo')
          const { avatarUrl } = wx.getStorageSync('_userInfo')
          console.log('0000', avatarUrl)
          wx.getSystemInfo({
            success: function (system) {
              wx.getImageInfo({
                src: _this.data.posterImgData[0],
                success: function (res) {
                  let posterImgWidth = res.width,
                    posterImgHeight = res.height;
                  _this.setData({
                    template: new Card().palette(_this.data.posterImgData[0],
                      _this.data.codeImg,
                      member.memberWeixin.nickname,
                      avatarUrl, 
                      { 
                        posterImgWidth: posterImgWidth, 
                        posterImgHeight: posterImgHeight,
                        windowWidth: system.windowWidth,
                        windowHeight: system.windowHeight
                      }, _this.data.explain),
                  });
                }
              })
            }
          })
        })
      }
    })
  },

  // 保存海报
  savePoster(){
    let tentacleInfo = {
      content: "我的邀请页面",
      contentType: app.globalData.contants.SHARE_TYPE.INVITE_FRIENDS, //内容类型
      title: '我的邀请', //标题
      type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
    }

    wxService.getTentacleContent(tentacleInfo).then(res => {
      if (res && res.id) {
        wx.saveImageToPhotosAlbum({
          filePath: this.imagePath,
          success(res) { 
            console.log('3333')
            setTimeout(()=>{
              wx.showToast({
                title: '保存成功,请前往相册查看~',
                icon: 'none'
              },2000)
            })
          },
          fail(err){
            console.log(err)
            let txt = '保存失敗';
            if (err.errMsg == 'saveImageToPhotosAlbum:fail auth deny'){
              txt = '获取相册授权失败'
            }
            setTimeout(() => {
              wx.showToast({
                title: txt,
                icon: 'none'
              }, 2000)
            })
          }
        });
      }
    })
  },
  onImgOK(e) {
    this.setData({
      saveBtnDisabled : false,
    });
    wx.hideLoading()
    this.imagePath = e.detail.path;
  }
});