import {
  getGroupDetail,
  groupApply
} from '../../services/index.js'
import {
  imgServerUrl
} from '../../config/config.js'
import {
  showToast
} from '../../utils/tips.js'

var app = getApp()

Page({
  data: {
    hasShare:true,
    positionId: 0,
    positionCategory: 0, //0：全职 1：兼职 2：入职奖励 3：高薪急聘 4：拼团岗位 5：福利岗位 8:自发布全职 9：自发布兼职
    imgServerUrl: imgServerUrl,
    clearTimer: false,
    myFormat: ['天', ':', ':', ' '],
    leftTime: 0,
    groupId: '', //拼团id
    inviteUser_id: '', //拼团邀请人
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    successReward: 0, //拼团成功奖励金
    failReward: 0, //拼团失败奖励金
    isLogin: false,
  },

  onLoad: function(options) {
    if (options.scene) {
      const scene = decodeURIComponent(options.scene)
      var arrPara = scene.split("&");
      var arrValue = [];
      for (var i in arrPara) {
        var arr = [];
        arr = arrPara[i].split("=");
        arrValue[i] = arr[1];
      }
      this.setData({
        hpPositionGroupId: arrValue[0],
        positionCategory: arrValue[1],
        positionId: arrValue[2],
      })
    } else {
      this.setData({
        hpPositionGroupId: options.gd,
        positionCategory: options.pc,
        positionId: options.pd
      })
    }
  },
  onShow: function() {
    var userId = wx.getStorageSync("user_id");
    var userToken = wx.getStorageSync("user_token");
    var memberId = wx.getStorageSync("member_id");
    var userInfo = wx.getStorageSync("userInfo");
    var isLogin = false;
    if (userId && memberId) {
      isLogin = true;
      app.globalData.userId = userId;
      app.globalData.userToken = userToken;
      app.globalData.memberId = memberId;
      if (userInfo) {
        app.globalData.userInfo = userInfo;
      }
    }
    this.setData({
      isLogin: isLogin
    })
    this.fetchData();
  },

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


  //获取拼团详情
  fetchData() {
    let paramsObj = {
      positionCategory: this.data.positionCategory,
      positionId: this.data.positionId,
      groupId: this.data.hpPositionGroupId,
      member_id: app.globalData.memberId
    }
    Object.assign(paramsObj);
    getGroupDetail(paramsObj).then(data => {
      var leftTime = data.data.position.groupEndTime
      try {
        wx.setStorageSync("groupDetail", data.data);
      } catch (e) {

      }
      var femaleReward = data.data.position.femaleReward;
      var maleReward = data.data.position.maleReward;
      var hasReward = data.data.position.hasReward;
      var successReward;
      var failReward;
      if (hasReward == 1) {
        if (maleReward >= femaleReward) {
          successReward = maleReward;
          failReward = femaleReward;
        } else {
          successReward = femaleReward;
          failReward = maleReward;
        }
      } else {
        successReward = 0;
        failReward = 0;
      }
      this.setData({
        postitionDetail: data.data,
        leftTime: leftTime,
        failReward: failReward,
        successReward: successReward
      })
    })
  },

  // 拼团倒计时结束
  myLinsterner() {
    // this.setData({
    //   clearTimer: true
    // })
    // showToast('拼团已结束')
  },


  // 一键参团
  joinGroup(e) {
    var self = this;
    if (!self.data.isLogin) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }
    wx.showModal({
      title: '提示',
      content: '确定要加入该拼工吗?',
      success: function(res) {
        if (res.confirm) {
          let paramsObj = {
            groupId: self.data.hpPositionGroupId,
            inviteUser_id: self.data.inviteUser_id,
            positionCategory: self.data.positionCategory,
            positionId: self.data.positionId,
            user_id: app.globalData.userId,
            user_token: app.globalData.userToken,
            member_id: app.globalData.memberId
          }
          Object.assign(paramsObj);
          groupApply(paramsObj).then(() => {
            self.onShow()
          })
        } else if (res.cancel) {

        }
      }
    })
  },

  ptTip() {
    wx.navigateTo({
      url: '../pt-tip/index',
    })
  },

  //分享图片
  makeMyPt() {
    if (!this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }
    wx.navigateTo({
      url: '../pt-share/pt-share?hpPositionGroupId=' + this.data.hpPositionGroupId + '&positionCategory=' + this.data.positionCategory + '&positionId=' + this.data.positionId,
    })
  },


  //拨打手机号
  phoneCall() {
    if (this.data.postitionDetail.position.contactPhone) {
      wx.makePhoneCall({
        phoneNumber: this.data.postitionDetail.position.contactPhone,
        success: function(data) {
          console.log(data)
        },
        fail: function(data) {
          console.log(data)
        }
      })
    } else {
      showToast('暂无手机号')
    }
  },

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