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,
    autoplay: false,
    interval: 2000,
    duration: 1000,
    circular: true,
    beforeColor: "white",//指示点颜色 
    afterColor: "coral",//当前选中的指示点颜色 
    previousmargin: '142rpx',//前边距
    nextmargin: '142rpx',//后边距
    posterImgData:[],
    currentPosterImg: '',
    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(){
    this.setData({
      template: new Card().palette(this.data.currentPosterImg, this.data.codeImg, member.memberWeixin.nickname, avatarUrl),
    });
  },
  
  // 获取海报图片
  getShareImg() {
    wxService.get(`/member/share/inviteFriends/buyer/getValid`).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          let initiatorPage = JSON.parse(data.initiatorPage)
          this.setData({
            posterImgData: initiatorPage
          })
        }
      }
    })
  },

  //获取二维码
  getMiniCode() {
    const urls = utils.getCurrentPageUrlWithArgs()

    let data = {
      "autoColor": true,
      "page": 'pages/userCenter/userCenter',
      "scene": '123',
      "width": 100
    }
    wxService.post(`/marketing/quickMark/getAppQrCodePicture`, data).then(res => {
      const { result, data } = res.data
      console.log(app.globalData.imageUrl + data)
      if (result == 0) {
        this.setData({
          codeImg: app.globalData.imageUrl + data
          // codeImg: "https://hwimagecdn.ihotwind.cn/usr/201908/miniQrCode-f00c5051fef7479395efb193431762c4.jpg"
        },()=>{
          this.setData({
            template: new Card().palette(this.data.posterImgData[0], this.data.codeImg, member.memberWeixin.nickname, avatarUrl),
          });
        })
      }
    })
  },

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

    wxService.getTentacleContent(tentacleInfo).then(res => {
      console.log(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.imagePath = e.detail.path;
  }
});