const navigationBarHeight = (getApp().statusBarHeight + 50) + 'px'
const Http = require("../../../../utils/HttpBasics");
const imgurl = require("../../../../utils/imgurl");
const config = require("../../../../config/config");
const util = require("../../../../utils/util");
let app = getApp();
Page({

  /**
   * 页面的初始数据 
   */
  data: {
    navigationBarHeight,
    noMerchant: imgurl.noMerchant.url,
    noCoupon: imgurl.noCoupon.url,
    poterbg: imgurl.poterbg.url,
    teljpgUrl: imgurl.teljpg.url,
    fenxiang: imgurl.fenxiang1.url,
    actUrl: imgurl.act.url,
    wmhome: imgurl.wmhome.url,
    page: 1,
    imglist: null,
    shopVoList: [],
    couponList: [], //活动劵列表
    qrCodeL: '', //小程序码
    currentTab: 0,
    isshare: false,
    showpost: false,
    imgHeight: 0,
    id: null,
    windowWidth: tt.getSystemInfoSync().windowWidth,
    windowHeight: tt.getSystemInfoSync().screenHeight,
    totalHeight: 0,
    canvasScale: 1.0, // 画布放大的倍数,因为如果保存的是一倍的分享图片的话，分享图会有点虚。所以保存的时候，canvasScale设置为2.0，wxss 里面的left: 500%;打开注释。就可保存两倍的分享图
  },
  //关闭海报
  closePoste: function() {
    this.setData({
      showpost: false
    })
  },
  goback: function () {
    tt.reLaunch({
      url: '/index/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    if (options && options.id) {
      this.setData({
        id: options.id
      });
      that.getList(options.id);
      that.getCouponList(options.id);
      this.setData({
        currentTab: 0
      })
    }
  },
  /**
   * 拨打电话
   */
  phone: function(e) {
    let that = this;
    tt.makePhoneCall({
      phoneNumber: e.target.dataset.merchantlinkphone
    });
  },
  /**
   * 显示分享弹框
   */
  showshare: function() {
    this.setData({
      isshare: true,
    })
  },
  /**
   * 隐藏分享弹框
   */
  hidemodal: function() {
    this.setData({
      isshare: false,
    })
  },
  //滑动切换
  swiperTabView: function(e) {
    this.setData({
      currentTab: e.detail.current
    });
  },
  /**
   * 显示分享海报
   */
  /* showPoster: function() {
    //跳转到海报生成页
    tt.navigateTo({
      url: `/pages/canvas/index?merchantId=${this.data.id}`
    })
  }, */
  showPoster: function() {
    //跳转到海报生成页
    let that = this;
      Http.get({
        url: config.api.checkUserStatus,
        data: {
          token: app.globalData.token
        }
      }).then(res => {
        tt.navigateTo({
          url: `/pages/canvas/index?merchantId=${that.data.id}`
        })
      }).catch(err => {
        console.log(err)
        if (err.code == 11004) {
          // 用户昵称未授权
          tt.redirectTo({
            url: `/pages/getuserinfo/index?couponChannelId=${that.data.id}&fromflag=poster`
          })
          
        }
      })
    
  },
  //点击切换
  clickTab: function(e) {
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      this.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  /**
   * 获取商户详情
   */
  getList: function(id) {
    let that = this;
    let data;
    data = {
      pageNum: that.data.page,
      pageSize: 15,
      id: id
    }
    Http.get({
        url: config.api.merchantList,
        data: data
      }).then(res => {
        if (res.data.list.length==0){
          tt.showModal({
            title: '提示',
            content: '此商户已经停用',
            confirmText: "返回",
            showCancel:false,
            success: function (res) {
              if (res.confirm) {
                tt.navigateBack({
                  url: '/index/searchbar',
                })
              }
            }
          })
        }
        let imgList = [];
        imgList.push(res.data.list[0].merchantImgUrl)
        that.setData({
          data: res.data.list[0],
          shopVoList: res.data.list[0].shopVoList,
          imglist: res.data.list[0].coverPicture == '[]' ? imgList : JSON.parse(res.data.list[0].coverPicture),
        })
      })
      .catch(err => {
        tt.showToast({
          title: err.errMsg,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      })
  },
  /**
   * 获取商户活动信息  券类型(1.满减券,2.代金券,3.团购券,4.礼品券,5.停车券,6.多商户券,8.砍价券,9.团购券,50.积分券,51.积分停车券 100.消费卡)
   * 投放频道：（1.列表, 2.限时抢购, 3. banner图 4. 游戏 5.卡频道 6.砍价频道 7.拼团频道 8专题）
   */
  getCouponList: function(id) {
    let that = this;
    let data;
    data = {
      status: 0,
      merchantId: id,
      pageNum: that.data.page,
      pageSize: 15,
    }
    Http.post({
        url: config.api.listByMerchant,
        data: data
      }).then(res => {
        that.setData({
          couponList: res.data.page.list,
        })
        
        // if (res.data && res.data.qrCode){
        //   that.setData({
        //     qrCode: res.data.qrCode,
        //   })
        // }
      })
      .catch(err => {
        tt.showToast({
          title: err.errMsg,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      })
  },
  /**
   * 获取多商铺列表 
   */
  onShareAppMessage: function(res) {
    app.globalData.previewFlag = true
    let that = this;
    let shareObj = {
      title: that.data.data.merchantName,
      path: `/pages/index/index?id=${that.data.id}&frommd=md&type=md`,
      success: function(res) {
        if (res.errMsg == 'shareAppMessage:ok') {}
      },
      fail: function(error) {
        if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
      }
    };
    console.log(`/pages/index/index?id=${that.data.id}&frommd=md`);
    // 来自页面内的按钮的转发
    if (res.from === 'button') {
      console.log(res)
      var eData = res.target.dataset.id;
      console.log(eData)
      shareObj.path = `/pages/index/index?id=${eData}&frommd=md&type=md`;
    }
    // 返回shareObj
    return shareObj;
  }
})