import {
  imgServerUrl
} from '../../config/config.js'
import {
  showToast
} from '../../utils/tips.js'
import Poster from '../../components/wxa-plugin-canvas/poster/poster';
var app = getApp()

Page({
  data: {
    hasShare: true,
    imgServerUrl: imgServerUrl,
    qrCode: '',
    hasAuth: true,
    posterConfig: '',
    userInfo: '',
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },
  onLoad: function (options) {
    this.setData({
      hpPositionGroupId: options.hpPositionGroupId,
      positionCategory: options.positionCategory,
      positionId: options.positionId
    })
  },
  onShow: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo
      })
      this.start()
    }
    this.setData({
      hasAuth: true
    })
  },

  start() {
    Promise.all([this.fetchData()]).then(() => {
      let posterConfig = {
        width: 700,
        height: 950,
        debug: false,
        texts: [{
          x: 54,
          y: 260,
          baseLine: 'middle',
          text: this.data.userInfo.nickName,
          fontSize: 38,
          color: '#ffffff',
          lineHeight: 30,
          zIndex: 2,
        },
        {
          x: 54,
          y: 316,
          baseLine: 'middle',
          text: '“我在找好友一起上班！”',
          fontSize: 26,
          color: '#ffffff',
          zIndex: 2,
        },
        {
          x: 350,
          y: 446,
          baseLine: 'middle',
          textAlign: "center",
          width: 700,
          text: this.data.postitionDetail.position.positionName,
          fontSize: 38,
          color: '#ffffff',
          lineHeight: 38,
          zIndex: 2
        },
        {
          x: 350,
          y: 494,
          baseLine: 'middle',
          textAlign: "center",
          width: 700,
          text: this.data.postitionDetail.position.hpCompany.companyName,
          fontSize: 20,
          color: '#ffffff',
          zIndex: 2,
        },
        {
          x: 350,
          y: 542,
          baseLine: 'middle',
          textAlign: "center",
          width: 700,
          text: this.data.postitionDetail.position.totalSalary ? this.data.postitionDetail.position.totalSalary : this.data.postitionDetail.position.basicSalary,
          fontSize: 50,
          color: '#ffffff',
          zIndex: 2,
        },
        {
          x: 62,
          y: 828,
          baseLine: 'middle',
          text: '专属客服，全程服务',
          fontSize: 20,
          color: '#ffffff',
          zIndex: 2,
        },
        {
          x: 464,
          y: 786,
          baseLine: 'middle',
          text: '识别右侧二维码，',
          fontSize: 20,
          color: '#ffffff',
          zIndex: 2,
        },
        {
          x: 464,
          y: 812,
          baseLine: 'middle',
          text: '即可参与我发起的拼工！',
          fontSize: 20,
          color: '#ffffff',
          zIndex: 2,
        }
        ],
        images: [{
          width: 700,
          height: 950,
          x: 0,
          y: 0,
          url: imgServerUrl + '/images/happyjob/bg.png',
          zIndex: 1,
        },
        {
          width: 218,
          height: 218,
          x: 400,
          y: 160,
          url: app.globalData.userInfo.avatarUrl,
          borderRadius: 222,
          zIndex: 2,
          borderWidth: 12,
          borderColor: '#fff',
        },
        {
          width: 196,
          height: 40,
          x: 50,
          y: 772,
          url: '/images/happyjob/logo.png',
          zIndex: 2,
        },
        {
          width: 180,
          height: 180,
          x: 270,
          y: 700,
          url: this.data.qrcode, //二维码
          zIndex: 2,
          borderRadius: 6,
          borderWidth: 14,
          borderColor: '#fff',
        },
        ],
      }
      this.setData({
        posterConfig
      })

    })
  },

  //获取历史记录
  fetchData() {
    wx.showLoading({
      title: 'loading',
      mask: true
    });
    var that = this
    return new Promise(function (resolve, reject) {
      try {
        var value = wx.getStorageSync('groupDetail')
        if (value) {
          // Do something with return value
          wx.hideLoading()
          that.setData({
            postitionDetail: value,
            qrcode: value.qrcodeUrl,
          })
          resolve(true)
        }
      } catch (e) {
        // Do something when catch error
        wx.hideLoading()
        reject(data)
      }
    })
  },

  onPosterSuccess(e) {
    var detail = e.detail;
    var that = this
    wx.saveImageToPhotosAlbum({
      filePath: detail,
      success(res) {
        wx.hideLoading();
        showToast('已保存到相册,快去分享吧！')
      },
      fail() {
        wx.hideLoading();
        showToast('请授权保存到相册')
        that.setData({
          hasAuth: false
        })
      }
    })
    wx.hideLoading()
  },
  onPosterFail(err) {
    wx.hideLoading()
    console.error(err);
  },

  /**
   * 异步生成海报
   */
  onCreatePoster() {
    Poster.create();
  },

  qrcodeLoad() {
    wx.hideLoading()
  },

  onShareAppMessage: function () {
    return {
      title: '开心工作参团有奖',
      path: '/pages/pt-detail/index?gd=' + this.data.hpPositionGroupId + '&pc=' + this.data.positionCategory + '&pd=' + this.data.positionId,
      imageUrl: ''
    }
  },

  onError(err) {
    app.aldstat.sendEvent('报错', {
      'err': err
    });
  },
})