var t = getApp(), os = t.globalData.setting, ut = require("../../utils/util.js");
var regeneratorRuntime = require('../../utils/runtime.js');
Component({
  properties: {
    // 这里定义了innerText属性，属性值可以在组件使用时指定
    object: {
      type: Object, value: null,
    },
    goods_array: {
      type: Array, value: []
    },
  },
  data: {
    // 这里是一些组件内部数据
    someData: null,
    imghost: os.imghost,
  },

  ready: function () {
    var g_id = this.data.object.data;
    this.data.g_id = g_id;
    this.init(g_id);
  },

  pageLifetimes: {
    //游客登录后，只要更新卡的情况
    show: function () {
      //--初始化卡类--
      this.init(this.data.g_id);
    },
  },

  methods: {
    // 这里是一个自定义方法
    init: function (g_id) {
      var th = this;

      getApp().getConfig2(function (e) {
        var swithc_list = e.switch_list;
        var sw_arr = JSON.parse(swithc_list);

        if (sw_arr.is_retail_price) {
          th.setData({ is_retail_price: 1 });
        }
      })


      var app = getApp(), goodsidlist = "",new_idlist="";
      if (g_id && g_id.length > 0) {
        //--先把商品ID串起来--
        g_id.forEach(function (val, ind) {
          var item = {};
          goodsidlist += val.goodsid + ",";
          if (val.idlist) {           
            new_idlist += val.idlist + ",";
          }
        })
        goodsidlist = ut.sub_last(goodsidlist);
        
        if (new_idlist) {         
          new_idlist = ut.sub_last(new_idlist);
        }

        var user_id = getApp().globalData.user_id;
        var pre_data = { store_id: os.stoid, is_end: 0, timetype: 3, isuse: 1 };
        if (user_id) {
          pre_data.user_id = user_id;
        }
        let url=''
        if (new_idlist) {
           url="/api/weshop/marketing/marketingPresellList/page?isnew=1&idlist="+new_idlist
        }else{
           url="/api/weshop/marketing/marketingPresellList/page?goodsidlist=" + goodsidlist
         }
        //--调用接口,读取秒杀--
        app.request.promiseGet(url, {
          data: pre_data
        }).then(res => {

          if (res.data.code == 0 && res.data.data.pageData && res.data.data.pageData.length > 0) {
            //如果数组为空的时候
            var goodslist = res.data.data.pageData;
            //就算是添加的活动已经过期，就要用最新的进行中活动
            if (goodslist && goodslist.length > 0) {
              th.set_goods_list(g_id, goodslist);
            }

            th.data.timer = setInterval(function () {
              th.countDown2(th);
            }, 1000);
          }


        });
      } else {
        th.no_gid_set();
      }
    },

    //当是默认的情况
    no_gid_set() {
      var th = this;
      var user_id = getApp().globalData.user_id;
      var pre_data = { store_id: os.stoid, is_end: 0, timetype: 3, isuse: 1, page: 1, pageSize: 9 };
      if (user_id) {
        pre_data.user_id = user_id;
      }
      getApp().request.promiseGet("/api/weshop/marketing/marketingPresellList/page", { data: pre_data }
      ).then(res => {
        if (res.data.code == 0 && res.data.data.pageData && res.data.data.pageData.length > 0) {
          var goodsidlist = res.data.data.pageData;
          this.set_goods_list(null, goodsidlist);

          th.data.timer = setInterval(function () {
            th.countDown2(th);
          }, 1000);
        }
      });
    },

    //就算是添加的活动已经过期，就要用最新的活动
    set_goods_list(g_id, goodslist) {
      // 判断火热，预热
      var newTime = ut.gettimestamp(), all_array = [], th = this;
      th.setData({ newTime: newTime });
      if (g_id) {
        /*--商品队列按照添加的顺序排列--*/
        goodslist.forEach(function (vy, indy) {
          all_array.push(vy);
        })

      } else {
        all_array = goodslist;
      }

      if (all_array.length == 0) { all_array = goodslist; }

      var arr = new Array();
      //--三个三个一组---
      for (var i = 0; i < all_array.length; i += 3) {
        arr.push(all_array.slice(i, i + 3));
      }
      /*--熏染到前台--*/
      th.setData({ goods_array: arr });
    },

    //图片失败，默认图片
    bind_bnerr3: function (e) {
      var _errImg = e.target.dataset.errorimg;
      var _errObj = {};
      _errObj[_errImg] = "/public/images/default_goods_image_240.gif";
      this.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;
    },

    go_to_skill: function () {
      wx.navigateTo({
        url: "/packageC/pages/presell/list/list"
      });
    },

    //----秒杀倒计时函数-----
    countDown2: function (ob) {
      if (ob == undefined) return false;
      var ee = ob;
      // 获取当前时间，同时得到活动结束时间数组
      var newTime = ut.gettimestamp();
      this.setData({ newTime: newTime });

      var List = ee.data.goods_array;
      if (List.length == 0) return false;
      for (var j = 0; j < List.length; j++) {
        // 对结束时间进行处理渲染到页面
        var endTimeList = List[j];
        for (var i = 0; i < endTimeList.length; i++) {
          var o = endTimeList[i];
          var endTime = o.end_time;
          if (newTime < o.start_time) endTime = o.start_time;
          let obj = null;
          // 如果活动未结束，对时间进行处理
          if (endTime - newTime > 0) {
            let time = (endTime - newTime);
            // 获取天、时、分、秒
            let day = parseInt(time / (60 * 60 * 24));
            let hou = parseInt(time % (60 * 60 * 24) / 3600);
            let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
            let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
            obj = {
              day: this.timeFormat(day),
              hou: this.timeFormat(hou),
              min: this.timeFormat(min),
              sec: this.timeFormat(sec)
            }
          } else {
            //活动已结束，全部设置为'00'
            obj = {
              day: '00',
              hou: '00',
              min: '00',
              sec: '00'
            }
          }
          var txt = "goods_array[" + j + "][" + i + "].djs";
          ee.setData({
            [txt]: obj
          });
        }
      }
    },
    //---小于10的格式化函数----
    timeFormat: function (param) {
      return param < 10 ? '0' + param : param;
    },

  },




})