const util = require("../../../utils/util.js");
const app = getApp();
Page({
  data: {
    shareImage: '',
    orderid: "",
    groupid: "",
    painting: {},
    info: {
      posters: [],
    },
    indicatorDots: true,
    current: 0,
    channel: 0,
    channelTimes: 0,
    sid: '',
    pid: '',
    groupbook: -1,
    status: 0,
    userInfo: app.globalData.userInfo,
    showConent: true,
    shareStatus: 1,
    makeTask: false //是否为宝贝币中心任务进入
  },
  onLoad(options) {
    console.log('options', options)
    if (options.channel != 0 && !util.isEmpty(options.channel)) {
      this.setData({ channel: options.channel, channelTimes: options.channelTimes });
    }
    let prePath = util.getPrePageRoute();
    this.setData({ id: options.id, orderid: options.orderid || '', groupid: options.groupid || '', userInfo: app.globalData.userInfo, sid: options.sid || '', pid: options.pid || '', groupbook: options.groupbook || -1, showConent: (prePath == 'pages/product/activity/index' || prePath == 'pages/account/monetaryCenter/posterTask') ? true : false, makeTask: options.makeTask });
  },
  onReady() {
    this.getActivity();
  },
  // 复制文案
  copyContent(e) {
    let val = e.currentTarget.dataset.data;
    wx.setClipboardData({
      data: val,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
          icon: 'none'
        });
      }
    });
  },
  // 获取分享家是否停用
  stat() {
    let that = this;
    util.stat().then(res => {
      that.setData({ shareStatus: res.shareStatus })
    })
  },
  // 获取用户信息
  getActivity() {
    console.log(this.data.id);
    let that = this, data = { aid: that.data.id }, url = "article", params = {};//article ———— 二维码
    if (this.data.channel && this.data.channel != 0 && this.data.channelTimes == 1) {
      params.channelId = this.data.channel;
      params.channelCount = parseInt(this.data.channelTimes) + 1;
    }
    if (!util.isEmpty(that.data.orderid)) {
      url = "group";
      data.orderid = that.data.orderid;
      params.orderid = that.data.orderid;
    }
    if (!util.isEmpty(that.data.groupid)) {
      url = "group";
      data.groupid = that.data.groupid;
      params.groupid = that.data.groupid;
    }
    if (!util.isEmpty(that.data.id)) {
      params.aid = that.data.id;
    }
    if (!util.isEmpty(that.data.sid)) {
      params.sharesid = that.data.sid;
    }
    if (!util.isEmpty(that.data.pid)) {
      params.sharepid = that.data.pid;
    }
    params.code = this.data.userInfo.code;
    params.sharer = this.data.userInfo.nickname;
    data.params = JSON.stringify(params);
    console.log(data);
    console.log(url);
    util.ajax({
      func: "user/poster/" + url,   // user/poster/article  user/qrcode/group
      data: data                  // user/qrcode/article ——获取活动二维码
    }, function (res) {
      console.log(res);
      if (res.code == 0) {
        res.data.invitePrice = util.config.invitePrice;
        let tags = [];
        if (!util.isObjEmpty(res.data.tags)) {
          for (let i in res.data.tags) {
            tags.push(res.data.tags[i].tagValue);
          }
        }
        res.data.tags = tags.join("|");
        res.data.tags = res.data.tags.length > 37 ? res.data.tags.substring(0, 37) + ".." : res.data.tags;
        if (!util.isEmpty(res.data.avatar)) {
          wx.downloadFile({
            url: util.config.apiServer + "file.do?url=" + res.data.avatar,
            success(ret) {
              if (ret.statusCode === 200) {
                res.data.avatar = ret.tempFilePath;
              }
              that.setData({ info: res.data });
              that.cateQroce(data, url, res.data.qrcode, res.data.groupid);
            }
          });
        } else {
          res.data.avatar = "/images/default_logo.jpg";
          that.setData({ info: res.data });
          that.cateQroce(data, url, res.data.qrcode, res.data.groupid);
        }
        that.stat()
      } else
        util.showTips(res.reason);
    });
  },
  // 生成二维码
  cateQroce(data, url, qrcode, groupid) {
    console.log(url);
    let that = this;
    if (!util.isEmpty(that.data.orderid)) data.groupid = groupid;
    console.log(data);
    util.ajax({
      func: "user/qrcode/" + url,
      data: data
    }, function (res) {
      console.log(res);
      if (res.code == 0) {
        console.log(res);
        that.data.info.qrcode = res.data;
        that.setData({ info: that.data.info });
      } else
        util.showTips(res.reason);
    });
  },
  swiperChange(e) {
    let current = e.detail.current;
    this.setData({ current: e.detail.current });
  },
  // 记录分享次数
  shareRecord(aid) {
    if (!util.isEmpty(aid)) {
      util.ajax({
        func: 'article/share',
        data: { "aid": aid },
        load: false
      }), function (res) {
        console.log(res)
      }
    }
  },
  cetaInvite() {
    // this.shareRecord(this.data.id)
    wx.showLoading({
      title: '海报生成中'
    });
    let that = this, views = [];
    if (this.data.current < this.data.info.posters.length) {
      let poster = this.data.info.posters[this.data.current];
      console.log(poster.posterPic);
      views.push({
        type: 'image',
        url: poster.posterPic,
        top: 0,
        left: 0,
        width: poster.posterWidth,
        height: poster.posterHeight
      },
        // that.data.info.qrcode
        // https://img.bbztx.com/images/upload/thumbs/20221212/logo/1670811442549004585.png
        {
          type: 'image',
          url: that.data.info.qrcode,
          top: poster.posterHeight - poster.qrcodeY - poster.qrcodeHeight,
          left: poster.qrcodeX,
          width: poster.qrcodeWidth,
          height: poster.qrcodeHeight
        })

      this.setData({
        painting: {
          width: poster.posterWidth,
          height: poster.posterHeight,
          clear: true,
          views: views
        }
      });
    } else if (this.data.current === this.data.info.posters.length) {

      if (!util.isEmpty(that.data.orderid)) {
        views.push({
          type: 'image',
          url: "https://img.bbztx.com/images/upload/thumbs/20221212/logo/1670814536619084888.jpg",
          top: 0,
          left: 0,
          width: 750,
          height: 1332
        },
          // that.data.info.qrcode
          // https://img.bbztx.com/images/upload/thumbs/20221212/logo/1670811442549004585.png
          {
            type: 'image',
            url: that.data.info.qrcode,
            top: 1078,
            left: 488,
            width: 210,
            height: 210
          },
          {
            type: 'image',
            url: that.data.info.avatar,
            borderRadius: 65,
            top: 268,
            left: 310,
            width: 139,
            height: 139
          },
          {
            type: 'image',
            url: that.data.info.logo,
            top: 542,
            left: 18,
            width: 712,
            height: 370
          },
          {
            type: 'text',
            content: that.data.info.discountPrice,
            fontSize: 40,
            bolder: true,
            color: '#ee3a43',
            textAlign: 'left',
            top: 482,
            left: 460
          },
          {
            type: 'text',
            content: that.data.info.nickname + " 邀您拼团",
            fontSize: 32,
            color: '#3e3a39',
            textAlign: 'center',
            top: 420,
            left: 750 / 2
          },
          {
            type: 'text',
            content: that.data.info.tags,
            fontSize: 24,
            color: '#ee3a43',
            textAlign: 'left',
            MaxLineNumber: 1,
            breakWord: true,
            lineHeight: 30,
            top: 927,
            left: 50,
            width: 650
          },
          {
            type: 'text',
            content: "活动费",
            fontSize: 26,
            color: '#595757',
            textAlign: 'left',
            top: 990,
            left: 50
          },
          {
            type: 'text',
            content: "￥",
            fontSize: 18,
            color: '#ee3a43',
            textAlign: 'left',
            top: 995,
            left: 168
          },
          {
            type: 'text',
            content: that.data.info.price,
            fontSize: 26,
            bolder: true,
            color: '#ee3a43',
            textAlign: 'left',
            top: 990,
            left: 186
          });
      } else {
        views.push({
          type: 'image',
          url: 'https://img.bbztx.com/images/upload/thumbs/20221110/logo/1668071284848082519.jpg',
          top: 0,
          left: 0,
          width: 750,
          height: 1332
        },
          {
            type: 'image',
            url: that.data.info.logo,
            top: 235,
            left: 71,
            arcType: "arc",
            borderRadius: 12,
            width: 604,
            height: 311
          },
          {
            type: 'image',
            url: that.data.info.qrcode,
            top: 1036,
            left: 65,
            width: 210,
            height: 210
          },
          {
            type: 'image',
            url: that.data.info.avatar,
            borderRadius: 47,
            top: 105,
            left: 80,
            width: 100,
            height: 100
          },
          {
            type: 'text',
            content: that.data.info.tags,
            fontSize: 24,
            color: '#ee3a43',
            textAlign: 'left',
            top: 580,
            left: 70
          },
          {
            type: 'text',
            content: "         " + that.data.info.commentContent,
            MaxLineNumber: 4,
            lineHeight: 50,
            breakWord: true,
            fontSize: 28,
            width: 580,
            color: '#333333',
            textAlign: 'justify',
            top: 745,
            left: 70
          },
          {
            type: 'text',
            content: "活动费",
            fontSize: 28,
            color: '#333333',
            textAlign: 'left',
            top: 630,
            left: 70
          },
          {
            type: 'text',
            content: "￥",
            fontSize: 18,
            color: '#ee3a43',
            textAlign: 'left',
            top: 640,
            left: 168
          },
          {
            type: 'text',
            content: that.data.info.price,
            fontSize: 32,
            color: '#ee3a43',
            textAlign: 'left',
            top: 630,
            left: 186
          },
          {
            type: 'text',
            content: that.data.info.nickname,
            fontSize: 32,
            color: '#231815',
            textAlign: 'left',
            top: 120,
            left: 200
          },
          {
            type: 'text',
            content: that.data.userInfo.roleType == 'normal' || that.data.userInfo.roleTypeDisable == 1 ? "让宝贝探索属于自己的世界" : "邀你参加活动，送你" + util.config.invitePrice + "元新人现金券",
            fontSize: 22,
            color: '#231815',
            textAlign: 'left',
            top: 160,
            left: 200
          });
      }
      if (that.data.info.psAttendCount > 10) {
        views.push({
          type: 'text',
          content: "已售" + that.data.info.psAttendCount + "人",
          fontSize: 24,
          color: '#898989',
          textAlign: 'right',
          top: !util.isEmpty(that.data.orderid) ? 992 : 630,
          left: !util.isEmpty(that.data.orderid) ? 700 : 675
        })
      }
      this.setData({
        painting: {
          width: 750,
          height: 1332,
          clear: true,
          views: views
        }
      });
    }

  },
  swiperGetImage(event) {
    const { tempFilePath, errMsg } = event.detail;
    const current = event.currentTarget.dataset.current;
    if (errMsg === 'canvasdrawer:ok') {
      this.data.img.push(tempFilePath)
      this.data.info.posters[current].canvasImage = tempFilePath;
      this.setData({
        info: this.data.info,
        img: this.data.img,
      });
      wx.hideLoading();
    }
  },
  eventGetImage(event) {
    const { tempFilePath, errMsg } = event.detail;
    if (errMsg === 'canvasdrawer:ok') {
      wx.saveImageToPhotosAlbum({
        filePath: tempFilePath,
        success(res) {
          console.log('saveImageToPhotosAlbum-res', res)
          wx.showToast({
            title: '保存图片成功',
            icon: 'success',
            duration: 2000,
            success: () => {
              if (this.data.makeTask) {
                wx.navigateBack()
              }
            }
          })
        },
        fail(err) {
          console.log('saveImageToPhotosAlbum-err', err)
          if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
            wx.showModal({
              title: '提示',
              content: '需要您授权保存相册',
              showCancel: false,
              success: modalSuccess => {
                wx.openSetting({
                  success(settingdata) {
                    console.log("settingdata", settingdata)
                    if (settingdata.authSetting['scope.writePhotosAlbum']) {
                      wx.showModal({
                        title: '提示',
                        content: '获取权限成功,再次点击即可保存!',
                        showCancel: false,
                      })
                    } else {
                      wx.showModal({
                        title: '提示',
                        content: '获取权限失败，将无法保存到相册。',
                        showCancel: false,
                      })
                    }
                  }
                })
              }
            })
          }
        }
      });

      wx.hideLoading();
    }
  }
})