var e = getApp(),os = e.globalData.setting;
var utils = require('../../../../utils/util.js');
var regeneratorRuntime = require('../../../../utils/runtime.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    judge:0,
    quan_list:null,
    iurl:os.imghost,
    h_pic:null,
    GradeId:null,
    FormId:null,
    now:'',
    ismore: 0, //数据是否全部加载完成
    is_read: 0, //是否查询过我的礼包接口
    curpage: 1, //当前分页数
    pageSize: 10, //页大小
    total: 0, //总数量
  },
  clik_coupon:function(){
     this.setData({
         judge:1
     })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     console.log("onLoad");
     console.log(options);
     var h_pic=options.img;

     if(h_pic.indexOf("http")==-1){
         h_pic= os.imghost+h_pic;
     }

     var FormId=options.FormId;
     var GradeId=options.gradeId;
     var now=utils.gettimestamp();
     this.setData({h_pic:h_pic,GradeId:GradeId,FormId:FormId,now:now});
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:function () {
    this.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var th = this;
    if (th.data.total <= th.data.pageSize) return;
    if (th.data.ismore) return;
    th.getList();
    

  },
  getList: function () {

    if(this.data.ismore) return false;
    if(this.data.searching) return false;
    this.data.searching=1;

    wx.showLoading({
      title: '加载中.',
    })
    
    var th = this,q_list=null;

    //--获取列表--
    getApp().request.promiseGet("/api/weshop/users/grade/wechat/cash/page", {
      data:{ storeId:os.stoid,
        FormId:th.data.FormId,
        GradeId:th.data.GradeId,
        userId:getApp().globalData.user_id,
         page: th.data.curpage,
        pageSize: th.data.pageSize}
    }).then(res => {
      th.data.searching=0;
      if (utils.ajax_ok(res)){
        
        
                q_list = res.data.data.pageData;
              
                //取分类最后一级
                    for(var i in q_list){
                      var name=q_list[i].UseObjectName;
                      var arr=name.split("\\")
                      q_list[i].UseObjectName=arr[arr.length-1];
                      q_list[i].Remark =  q_list[i].Remark.replace(/\n/g, "\n")
                  } 
             th.data.curpage++;
            
              var quan_list=th.data.quan_list||[];
              
              quan_list=quan_list.concat(q_list);
              if (quan_list.length>=res.data.data.total) {
                th.setData({
                  is_read: 1,
                  ismore: 1,
                  is_get:1,
                })     
              }  
                  th.setData({
                    quan_list:quan_list,
                    total: res.data.data.total
             });
      }
      else {

        th.setData({
          is_read: 1,
          ismore: 1,
          is_get:1,
        })       
      }
      wx.hideLoading();

    })
    


  },

  //获取券
  get_quan:function (e) {

      console.log("get_quan_ing:"+this.data.get_quan_ing);

      if(this.data.get_quan_ing) return false;
      this.data.get_quan_ing=1;

      var th=this,index=e.currentTarget.dataset.ind;
      var item = this.data.quan_list[index];

      var txt_get="quan_list["+index+"].get_quan_ing";


      this.setData({
        [txt_get]:1
      })


      var can_get = item.ObtainTimes;
      var Obtain = item.Obtain;
      var Id = item.Id;

      if (!Obtain || Obtain == undefined) Obtain = 0;

      getApp().request.post("/api/weshop/users/grade/cash/insert",{
      data:{
          privilegeId:Id,
          storeId:os.stoid,
          userId:getApp().globalData.user_id,
      },
      success:function (e) {
        if(e.data.code==0){
           var txt="quan_list["+index+"].ObtainTimes"
           var obj={};obj[txt]=can_get-1;

           var text = "quan_list[" + index + "].Obtain"; Obtain++;
           obj[text] = Obtain;
           th.setData(obj);
           getApp().my_warnning("您已成功领取该券",1,th);

           setTimeout(()=>{
            th.data.get_quan_ing=0;
            th.setData({
               [txt_get]:0
            })
           },1000)

           
        
        }else{
            getApp().my_warnning(e.data.msg,0,th);

            setTimeout(()=>{
              th.data.get_quan_ing=0;
              th.setData({
                [txt_get]:0
            })
           })
        }
      }
    })
  },

  //点击核销券
  get_out:function (e) {
    var qt_txt = this.selectComponent("#pop_txt"); //组件的id
    var ob={title:"温馨提示",content:"当前优惠券已领取，点击二维码可立即使用"};
    qt_txt.open(ob);

  },

  clik_coupon:function (e) {
    var ind=e.currentTarget.dataset.ind;
    var is_open=this.data.quan_list[ind].is_open;
    if(is_open==1) is_open=0;
    else  is_open=1;

    var txt="quan_list["+ind+"].is_open"
    var obj={};obj[txt]=is_open;
    this.setData(obj);
  },

  //--显示核销券--
  show_quan:function (e) {
   var ind=e.currentTarget.dataset.ind;
   var item=this.data.quan_list[ind];
   var pId=this.data.quan_list[ind].Id;
   var th=this;
   var validay = item.Validay;
   var now = e.currentTarget.dataset.now;
  
   getApp().request.get("/api/weshop/users/grade/cash/code/get",{
       data:{
           storeId:os.stoid,
           privilegeId:pId,
           userId:getApp().globalData.user_id,
       },
       success:function (e) {
           console.log(e);
           console.log('/code/get');
          

           //--获取成功的时候--
           if(e.data.code==0 && e.data.data){

                var  name= e.data.data.UseObjectName;

               var no=e.data.data.CashRepNo;
               var qc_com = th.selectComponent("#qc_com"); //组件的id
               var obj={val:no,content:"请将二维码展示给核销员，抵券更快捷",now: now, validay: validay, name: name,is_quan:1};
               qc_com.open(obj)

           }else{
               var qt_txt = th.selectComponent("#pop_txt"); //组件的id
               var ob={title:"温馨提示",content:e.data.msg};
               qt_txt.open(ob);
           }

       }
   })
   

  }

})