// packageE/member/component/founder/collageItem_3/collageItem_3.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    name: {
      type: null,
    },
    datas: {
      type: null,
    },
    power: {
      type: null,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    nowtime: "",
    timeOver: "",
    lastTime: 0,
    ispower: "",
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {},
    moved() {},
    detached() {
      clearInterval(this.data.timeOver);
    },
  },
  observers: {
    datas: function (b) {
      let that = this;
      console.log(b);
      if (b.project_shell_type == 3) {
        console.log(`${b.lock_update_at}000`);
        let istime = Number(`${b.lock_update_at}000`);
        let time = that.formatDate(istime);
        this.data.timeOver = setInterval(() => {
          const nowDaytime = new Date().getTime();
          // let lastTime  = (istime - nowDaytime) /1000
          that._countDown(istime, nowDaytime);
        }, 1000);
        that.setData({
          nowtime: time,
        });
        console.log(time);
      }
    },
    power: function (a) {
      console.log(a);
      let that = this;
      that.setData({
        ispower: a,
      });
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 加入拼单车
    joinCart(e) {
      let projectId = e.currentTarget.dataset.item.id;
      this.triggerEvent("addProject", projectId);
    },
    // 编辑
    editProject(e) {
      console.log(e);
      let id = e.currentTarget.dataset.id;
      this.triggerEvent("delId", id);
    },
    // 删除
    delProject() {
      let id = e.currentTarget.dataset.id;
      console.log(id);
      this.triggerEvent("editProject", id);
    },
    // 查看详情
    examine(e) {
      console.log(e);
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url:
          "/packageE/member/collage/tacit_detproject/tacit_detproject?id=" + id,
      });
    },
    _countDown(istime, nowDaytime) {
      if (istime - nowDaytime < 0) {
        clearInterval(this.data.timeOver);
        this.setData({
          lastTime: 0,
        });
        return;
      }
      let time = (istime - nowDaytime) / 1000;
      // 获取天、时、分、秒
      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);
      let obj = {
        day: this._timeFormat(day),
        hou: this._timeFormat(hou),
        min: this._timeFormat(min),
        sec: this._timeFormat(sec),
      };
      this.setData({
        lastTime: `${obj.day}天${obj.hou}时${obj.min}分${obj.sec}秒`,
      });
    },
    //小于10的格式化函数
    _timeFormat(param) {
      //小于10的格式化函数
      return param < 10 ? "0" + param : param;
    },
    formatDate(date) {
      console.log(date);
      // var date = new Date(date);
      var YY = date.getFullYear() + "-";
      var MM =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      var hh =
        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
      var mm =
        (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
        ":";
      var ss =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      return YY + MM + DD + " " + hh + mm + ss;
    },
  },
});
