var i = require("../../../../utils/util.js")
var e = getApp(),
  a = e.globalData.setting,
  os = a,
  t = e.request,
  d = e.globalData;
Page({
  data: {
    url: a.url, //接口网址
    iurl: a.imghost, //图片前缀网址
    itemShow: false, //项目栏目是否显示
    cardShow: false, //代金券栏目是否显示
    packShow: false, //福利栏目是否显示
    textShow: false, //规则是否显示
    integralShow: true, //积分领取是否显示
    growUpShow: true, //成长值领取是否显示
    itemButton: '',
    cardButton: '',
    getActId: '',
    getActType: '',
    giftBagId: '',
    receiveState: 0,
    textTitle: '',
    getUrl: '',
    noShow: false,
    cards: [], //礼包内容--礼券
    itemWare: [], //礼包内容--服务项目
    integralTitle: "",
    growUpTitle: "",
    is_sub: 0, //是否重复领取
    orderNumber: "", //订单号
    orderGoodsId: "", //商品id
    orderType:1,
    isdg:0,

    weappurltitle:"进入商城购物",
    weappurl:"/pages/index/index/index",

  },
  GetList: function() {
    var th = this;
    wx.showLoading({
      title: '加载中',
    })
    var url = "/api/weshop/marketing/giftbag/bound/get";

    var data={
      "actId": th.data.getActId, //活动Id
      "actType": 2, //活动类型 1:新人礼、2:评价有礼、3:节日营销、4:生日营销
      "storeId": a.stoid, //商家Id
      "userId": d.user_id, //用户ID
      "orderNumber": th.data.orderNumber,
      "orderType": th.data.orderType,
      "isdg": th.data.isdg
    };

    if(th.data.orderGoodsId){
      data["orderGoodsId"]=th.data.orderGoodsId;
    }

    if(th.data.giftBagId){
      data["gifbagid"]= th.data.giftBagId;
    }


    getApp().request.promiseGet(url, {
      isShowLoading: true,
      data:data
    }).then(res => {
      if (res.data.code == 0) {
        if (res.data.data.lbCoupons.length > 0 && res.data.data.lbCoupons != null) {
          //礼包内容--礼券
          th.setData({
            cardShow: true,
            cards: res.data.data.lbCoupons
          })
        }
        if (res.data.data.lbSM.length > 0 && res.data.data.lbSM != null) {
          //礼包内容--服务项目
          th.setData({
            itemShow: true,
            itemWare: res.data.data.lbSM
          })
        }
        //礼包积分
        if (res.data.data.lbIntegral <= 0) {
          if (res.data.data.lbGrowthValue <= 0) {
            th.setData({
              packShow: false
            })
          } else {
            th.setData({
              packShow: false,
              integralShow: false,
              growUpShow: true
            })
          }

        } else {
          if (res.data.data.lbGrowthValue > 0) {
            th.setData({
              packShow: true
            })
          } else {
            th.setData({
              packShow: true,
              integralShow: true,
              growUpShow: false
            })
          }
        }
        th.setData({
          integralTitle: '价值' + res.data.data.lbIntegral + '积分',
          growUpTitle: '价值' + res.data.data.lbGrowthValue + '成长值'
        })
        if (res.data.data.receiveState == 0) {
          th.setData({
            receiveState:0
          })
        } else {
          th.setData({
            receiveState: 1
          })
        }

        if (res.data.data.actIntro != '') {
          th.setData({
            textShow: true,
            textTitle: res.data.data.actIntro.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display：block"')
          })
        }
        if (res.data.data.weappurl != '' && res.data.data.weappurltitle != '') {
          th.setData({
            weappurl: res.data.data.weappurl,
            weappurltitle: res.data.data.weappurltitle
          })
        }


      } else {
        getApp().my_warnning(res.data.msg, 0, th);
        return false;
      }
    })
  },
  onLoad: function(options) {
    if(options.transfer){
      let obj=getApp().globalData.ts_trans_data;
      options= JSON.parse(JSON.stringify(obj));
      getApp().globalData.ts_trans_data=null;
 }

    var getisdg=0;
    if (options.isdg)
    {
      getisdg=options.isdg;
    }
    var th = this;
    th.setData({
      getActId: options.actId,
      orderNumber: options.orderNumber,
      orderGoodsId: options.orderGoodsId,
      giftBagId: options.giftBagId,
      isdg: getisdg
    })

    var orderType = options.orderType;
    if (orderType) {
      th.setData({ orderType: orderType})
    }
  

  },
  onShow: function() {
    //--先判断会员状态--
    var user_info = getApp().globalData.userInfo;
    if (user_info == null || user_info.mobile == undefined || user_info.mobile == "" || user_info.mobile == null) {
      getApp().goto('/packageE/pages/togoin/togoin');
      return false;
    }

    this.GetList();
 
  },
  GetReceive: function() {
    var th = this;
    if (th.data.receiveState) {
      return false;
    }
    var is_sub = th.data.is_sub; //判断是否重复提交
    if (is_sub == 0) {
      th.setData({
        is_sub: 1
      })

      var json = {
        "actId": th.data.getActId, //活动Id
        "actType": 2, //活动类型 1:新人礼、2:评价有礼、3:节日营销、4:生日营销
        "storeId": a.stoid, //商家Id
        "userId": d.user_id, //用户ID
        "orderNumber": th.data.orderNumber,
        "orderType": th.data.orderType,
        "buyFrom": 2
      };

      if(th.data.orderGoodsId){
          json.orderGoodsId=th.data.orderGoodsId;
      }

      if(th.data.giftBagId){
        json.giftBagId=th.data.giftBagId
      }


      if (th.data.isdg) {
        json.isdg=th.data.isdg
      }
      var data = JSON.stringify(json);
      var url = th.data.url + "/api/weshop/marketing/free/receive/gift/record/insert";
      getApp().request.json_post(url, json,
        function (res) {
          th.setData({
            is_sub: 0
          })
          if (res.data.code == 0) {
            th.setData({
              receiveState: 1
            })
            getApp().my_warnning("领取成功，同一个订单评价后只能兑换一份礼包！", 1, th);
          } else {
            getApp().my_warnning(res.data.msg, 0, th);
          }
        },
        function (res) {

        }

      )
    
    }
  },
  //界面跳转
  goto: function(e) {
    var url = e.currentTarget.dataset.url;
    getApp().goto(url);
  },
  //图片失败，默认图片
  bind_bnerr1: function(e) {
    var th = this;
    var _errImg = e.target.dataset.errorimg;
    var _Img = e.target.dataset.img;
    if (_Img != undefined) {
      var _errObj = {};
      _errObj[_errImg] = "/miniapp/images/giftbag/gift02.png";
      th.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;
    }
  },
});