var e = getApp(),
  a = e.globalData.setting,
  os = a,
  t = e.request,
  d = e.globalData;
var ut = require("../../../utils/util.js");

Page({
  data: {
    url: a.url, //接口网址
    iurl: a.imghost, //图片前缀网址
    isBuy: 0,
    giftImage: '',
    giftTitle: '',
    giftDate: '',
    giftPrice: '',
    giftIntegral: '',
    giftPosPrice: '',
    giftQty: '',
    giftRemark: '',
    giftType: '',
    actTitle: '',
    getUrl: '',
    wareCard: [],
    lbId: "",
    code: "", //核销码
    c_state: "0", //判断是否已经使用, 0正常  1已使用  2已过期
    is_lb: 0, //是否有礼包
    receiveState: 0, //0未领取  1已领取
    is_sub: 0, //判断是否重复提交
    is_libao: 0, //0轮播查看活动   1领取礼包 2.查看礼包详情
    held_id: "", //活动id
    taskId: "", //任务id用于查找礼包详情
    orderSn: "", //订单号
    is_type: "", //类型 0.轮播过来的 1.领取礼包过来的  2.查看礼包过来的
    is_head: 0, //数据是否加载完成
    giftBagId: "", //礼包id
    id: "", //一键领取的时候使用
  },
  onLoad: function(options) {

    var th = this;
    if (options.is_libao == 0) {
      th.setData({
        held_id: options.help_id,
        taskId: options.taskId,
        is_type: 0
      })
      th.help();
    }
    if (options.is_libao == 1) {
      th.setData({
        held_id: options.help_id,
        taskId: options.taskId,
        is_type: 1,
        id: options.id
      })
      th.help();
    }
    if (options.orderSn) {
      th.setData({
        orderSn: options.orderSn,
        is_type: 2
      })
      th.GetMyGiftList();
    }
    var nav_b = th.selectComponent("#nav_b"); //组件的id
    nav_b.set_name("助力", "/pages/user/assistance/assistance");
  },
  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;
    }

  },
  //助力活动查看礼包详情调用
  help: function(e) {
    var th = this;
    var url = "/api/weshop/marketing/giftbag/help/get";
    getApp().request.promiseGet(url, {
      isShowLoading: true,
      data: {
        helpId: th.data.held_id,
        storeId: a.stoid,
        taskId: th.data.taskId
      }
    }).then(res => {
      if (res.data.code == 0) {
        th.setData({
          giftImage: th.data.iurl + res.data.data.lbUrl,
          giftTitle: res.data.data.giftTitle,
          is_head: 1
        })
        if (res.data.data.giftRemark == '') {
          th.setData({
            giftRemark: '暂无详情......'
          })
        } else {
          if (res.data.data.giftRemark != null && res.data.data.giftRemark != undefined && res.data.data.giftRemark != '') {
            th.setData({
              giftRemark: res.data.data.giftRemark.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display：block"')
            })
          }

        }

        th.setData({
          giftPosPrice: res.data.data.giftPosPrice,
          giftQty: res.data.data.giftQty,
          giftIntegral: res.data.data.payIntegral,
          giftPrice: res.data.data.payMoney,
          giftType: res.data.data.actType,
          actTitle: res.data.data.actTitle,
          wareCard: res.data.data.wareCard,
          lbId: res.data.data.lbId
        })
        //判断是否有礼包
        for (var i = 0; i < res.data.data.wareCard.length; i++) {
          if (res.data.data.wareCard[i].lbType == 1) {
            th.setData({
              is_lb: 1
            })
            break;
          }
        }
      } else {
        getApp().my_warnning(res.data.msg, 0, th);
        return false;
      }

    })
  },
  GetMyGiftList: function() {
    var _this = this;
    var th = this;
    getApp().request.get('/api/weshop/marketing/my/giftbag/detail/get', {
      isShowLoading: true,
      data: {
        "storeId": a.stoid, //商家ID
        "userId": d.user_id, //用户ID
        "orderSn": th.data.orderSn
      },
      success: function(res) {
        if (res.data.code == 0) {
          th.setData({
            giftImage: th.data.iurl + res.data.data.lbUrl,
            giftTitle: res.data.data.giftTitle,
            is_head: 1
          })
          if (res.data.data.giftRemark == '') {
            th.setData({
              giftRemark: '暂无详情......'
            })
          } else {
            if (res.data.data.giftRemark != null && res.data.data.giftRemark != undefined && res.data.data.giftRemark != '') {
              th.setData({
                giftRemark: res.data.data.giftRemark.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display：block"')
              })
            }
          }

          //---获取日期的时间戳---
          var t_endtime = res.data.data.endTime;
          t_endtime = t_endtime.replace(/-/g, '/');
          var t_date = new Date(t_endtime) / 1000;
          var t_now = ut.gettimestamp();

          if (res.data.data.goodsUseState) th.setData({
            c_state: 1
          })
          else if (t_date <= t_now)
            th.setData({
              c_state: 2
            })

          th.setData({
            giftDate: res.data.data.endTime,
            giftPosPrice: res.data.data.giftPosPrice,
            giftQty: res.data.data.giftQty,
            giftIntegral: res.data.data.payIntegral,
            giftPrice: res.data.data.payMoney,
            giftType: res.data.data.actType,
            actTitle: res.data.data.actTitle,
            wareCard: res.data.data.wareCard,
            lbId: res.data.data.lbId
          })
          //判断是否有礼包
          for (var i = 0; i < res.data.data.wareCard.length; i++) {
            if (res.data.data.wareCard[i].lbType == 1) {
              th.setData({
                is_lb: 1
              })
              break;
            }
          }
        } else {
          getApp().my_warnning(res.data.msg, 0, th);
          return false;
        }
      }
    });

  },
  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.id, //活动Id
        "actType": 5, //活动类型 1:新人礼、2:评价有礼、3:节日营销、4:生日营销
        //"giftBagId": 1, //礼包Id
		"giftBagId": th.giftBagId, //礼包Id
        "storeId": a.stoid, //商家Id
        "userId": d.user_id, //用户ID
        "buyFrom":2  //小程序领取
      };
      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) {

        }

      )

    }
  },
  GetBuyGiftList: function() {
    var _this2 = this;
    var th = this.data;
    getApp().request.get('/api/weshop/marketing/giftbag/detail/get', {
      isShowLoading: true,
      data: {
        "storeId": a.stoid, //商家ID
        "userId": d.user_id, //用户ID
        "giftBagId": th.giftBagId
      },
      success: function(res) {
        if (res.data.code == 0) {
          _this2.setData({
            giftImage: th.iurl + res.data.data.lbUrl,
            giftTitle: res.data.data.giftTitle,
            is_head: 1
          })
          if (res.data.data.giftRemark == '') {

            _this2.setData({
              giftRemark: '暂无详情......'
            })
          } else {
            _this2.setData({
              giftRemark: res.data.data.giftRemark.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display：block"')
            })
          }
          _this2.setData({
            giftPosPrice: res.data.data.giftPosPrice,
            giftQty: res.data.data.giftQty,
            giftIntegral: res.data.data.payIntegral,
            giftPrice: res.data.data.payMoney,
            giftType: res.data.data.actType,
            actTitle: res.data.data.actTitle,
            wareCard: res.data.data.wareCard
          })
        } else {
          getApp().my_warnning(res.data.msg, 0, _this2);
          return false;
        }
      }
    });

  },
  //显示核销码
  code_show: function(e) {
    var th = this;
    //--获取成功的时候--
    var no = th.data.code;
    var qc_com = th.selectComponent("#qc_com"); //组件的id
    var obj = {
      val: no,
      content: "请将二维码展示给核销员，收货更快捷"
    };
    qc_com.open(obj)
  },
  //获取核销码
  getcode: function() {
    var th = this;
    var orderSn = th.data.orderSn; //订单号
    var json = {
      "storeId": a.stoid,
      "orderSn": orderSn
    };
    var data = JSON.stringify(json);
    var url = th.data.url + "/api/weshop/marketing/gift/goods/verify/code/get"; //预约接口地址
    getApp().request.json_post(url, json,
      function(res) {
        if (res.data.code == 0) {
          th.setData({
            code: res.data.data
          })
          th.code_show();
        } else {
          getApp().my_warnning(res.data.msg, 0, th);
        }
      },
      function(res) {

      }, "put"

    )



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

  },

  //图片失败，默认图片
  bind_bnerr: function (e) {
    var _errImg = e.target.dataset.errorimg;
    var _errObj = {};
    _errObj[_errImg] = this.data.iurl + "miniapp/images/big_back.jpg";
    this.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;
    //this.setData({ giftImage: this.data.iurl + "/miniapp/images/giftbag/gift01.png"});

  },

  //关闭导航
  close: function() {
    var th = this;
    var nav_b = th.selectComponent("#nav_b"); //组件的id
    nav_b.close_box();
  }
});
