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

Page({
  /** 
   * 页面的初始数据
   */
  data: {
    qr_code_object: {},
    xp_list: null,
    iurl: os.imghost,
    page: 1,
    isLoading: 0,
    no_more: 0,
    is_use: 0, //领取状态
    is_appoint_storage:0 //是否要登记门店才可以领取 0是不用，1要登记
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (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 = ut.gettimestamp();
    this.setData({
      h_pic,
      GradeId,
      FormId,
      now,
    });
    this.requestData()
    this.getQrCode(FormId,GradeId)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let th=this
          //先获取是否有关闭使用优惠券 
    getApp().getConfig2(function (ee) {
      var json_d = JSON.parse(ee.switch_list);
      th.setData({
        is_appoint_storage:json_d.is_appoint_storage,//是否要登记门店才可以领取 0是不用，1要登记
      });
    }, 1);
      getApp().check_can_share();
  },
  async getQrCode(FormId,GradeId) {
    const res = await getApp().request.promiseGet("/api/weshop/users/grade/wares/code/get", {
      data: {
        privilegeId: FormId,
        storeId: os.stoid,
        GradeId:GradeId,
        userId: getApp().globalData.user_id,
      },
    });
    if (res.data.code == 0 && res.data.data) {
      if (res.data.data.VerifyDate || res.data.data.VerifyNo) {
        this.setData({
          is_use: 1
        })
      } else {
        this.setData({
          qr_code_object: res.data.data,
        })
      }
    }
  },
  //门店回调
  async callback(e){
    let item = e.detail;
    console.log('门店信息'); 
    console.log(item);
    if (item && item.pickup_no) {   
      let qr_code_object=this.data.qr_code_object
      let data={
        store_id:os.stoid,//商家编号
        user_id:getApp().globalData.user_id, //会员编号
        // storageId:item.pickup_no, //门店编号
        storageId:item.keyid,
        writeOffCode:qr_code_object.WriteOffCode,//核销码
      }
      let res = await getApp().request.promisePut("/api/weshop/users/grade/storage/update", {
        data: data
      });
      if (res && res.data && res.data.code==0) {
        let text='qr_code_object.StorageID'
        this.setData({
          [text]:item.keyid
        })
      }else{
        wx.showToast({
          title: '门店选择失败',
          icon: 'none',
          duration: 2000
        })
      }
    }else{
      wx.showToast({
        title: '门店选择失败',
        icon: 'none',
        duration: 2000
      })
    }
  },
  async requestData() {
    if (this.data.no_more) return false;
    if (this.data.isLoading) return false;
    this.data.isLoading = 1;
    let {
      GradeId,
      FormId,
      page,
      xp_list
    } = this.data;
    let req_data = {
      page,
      GradeId,
      FormId,
      storeId: os.stoid,
      userId: getApp().globalData.user_id,
      pageSize:100
    }
    const res = await getApp().request.promiseGet("/api/weshop/users/grade/wechat/wares/page", {
      data: req_data,
      isShowLoading:true,
    });

    if (ut.ajax_ok(res)) {
      if (res.data.data.page > 1) {
        xp_list = xp_list.concat(res.data.data.pageData);
      } else {
        xp_list = res.data.data.pageData;
      }
      this.setData({
        xp_list,
        isLoading: 0,
        no_more: 0,
      })
    } else {
      this.setData({
        xp_list: [],
        isLoading: 0,
        no_more: 0,
      })
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   onShareAppMessage: function () {   	  getApp().globalData.no_clear=1

  },


  /*-- 打开服务项目 --*/
  open_xp: function (e) {
    var th = this;
    var qc_com = this.selectComponent("#qc_com"); //组件的id
    qc_com.open(th.data.qr_code_object)
  },

  //--领取商品--
  async show_hxm(e) {
    setTimeout(() => {
      let qr_code_object = this.data.qr_code_object;
      if (qr_code_object !== {}) {
        let {
          WriteOffCode: val,
          BeginDate,
          EndDate
        } = qr_code_object;
        let qr_code = this.selectComponent("#qc_com");
        let obj = {
          val,
          content: "当前核销码仅限当面使用",
          now: BeginDate,
          validay: EndDate,
          is_quan: 0,
          is_fw:0,
        };
        qr_code.open(obj)
      } else {
        wx.showToast({
          title: "获取失败",
          icon: 'none',
          duration: 1000
        })
      }
    }, 500);
  },

  close: function () {
    this.getQrCode(this.data.FormId)
  },

  select_product(){
    this.setData({
      store:1
    })
  }
})