const util = require('../../../utils/util.js');
const app = getApp();
var updateTimer;
Page({
  data: {
    updateTime:{},
    datas: {},
    buttonText: "",
    buttonClass: "button-warn",
    checkIndex:null,
    oid:'',
    optionId:''
  },
  onLoad: function (options) {
    this.setData({oid:options.oid,optionId:options.optionId})
    this.getInfo()
  },
  checkClick(e){
    let index = e.currentTarget.dataset.index;
    if(this.data.datas.status != 2 || this.data.datas.alipays[index].actnum <= 0) return false
    if(index == this.checkIndex){
      this.setData({checkIndex:null})
    }else{
      this.setData({checkIndex:index})
    }
  },
  getInfo(){
    let that = this;
    util.ajax({
      func: "v2/articleSchedule/option/detail",
      data:{optionId:that.data.optionId,oid:that.data.oid}
    }, function (res) {
      if (res.code == 0 ) {
        let datas = res.data;
        datas.endSaleTimeLong = res.data.endSaleTimeLong - res.data.saleTimeLong;
        that.setData({ buttonText: util.decode(datas.status, 0, "待审核", 1, "审核通过", 2, "确定", 3, "已下架", 4, "已删除", 5, "未开始",6, "已结束",7, "已满额", "") });
        let buttonClass = datas.status == 2? 'button-warn': 'disabled';
        that.setData({datas,buttonClass});
        that.updateTime();
      }else{
        util.showTips(res.reason);
      }
    })
  },
  activeLogoBinderror(e){
    var datas = this.data.datas;
    datas.logo = '/images/noimg.png'
    this.setData({datas});
  },
  submit(){
    if(this.data.datas.status != 2) return false;
    if(this.data.checkIndex == null){
      util.showTips('请选择场次');
      return false;
    }
    let that = this,data = this.data.datas.alipays[this.data.checkIndex];
    wx.showModal({
      title: '提示',
      content: '你确定选择此排期吗？选择后\n自动调换过去。',
      success (res) {
        if (res.confirm) {
          util.ajax({
            func: "v2/articleSchedule/option/exchange",
            data:{optionId:that.data.optionId,oid:that.data.oid,nSid:data.sid,nPid:data.pid}
          }, function (res) {
            if (res.code == 0 ) {
              wx.navigateTo({
                url: '/pages/order/scheduling/schedulingOk?orderid=' + res.data.orderid
              })
            }else{
              util.showTips(res.reason);
            }
          })
        } else if (res.cancel) {

        }
      }
    })
  },
  updateTime() {
    var self = this,
    lag = (self.data.datas.saleTimeLong ? self.data.datas.saleTimeLong : 0) / 1000,
    lag2 = (self.data.datas.endSaleTimeLong ? self.data.datas.endSaleTimeLong: 0) / 1000;
    let second, minite, hour, day, buttonClass = self.data.buttonClass, buttonText = self.data.buttonText;
    if (lag > 0) {
      second = Math.floor(lag % 60);
      minite = Math.floor((lag / 60) % 60);
      hour = Math.floor(lag / 3600);
      day = Math.floor((lag / 3600) / 24);
      self.data.updateTime.title = "距离开始";
      buttonText = '未开始';
      self.data.datas.saleTimeLong = self.data.datas.saleTimeLong - 1000;
    } else if (lag2 > 0) {
      second = Math.floor(lag2 % 60);
      minite = Math.floor((lag2 / 60) % 60);
      hour = Math.floor(lag2 / 3600);
      day = Math.floor((lag2 / 3600) / 24);
      self.data.updateTime.title = "距离结束";
      self.data.datas.endSaleTimeLong = self.data.datas.endSaleTimeLong - 1000;
      if(self.data.datas.status == 2 || self.data.datas.status == 5){
        self.data.datas.status = 2;
        buttonClass = "button-warn";
        buttonText = "确定";
      }
    } else {//已结束lag和log2都小于=0
      buttonClass = "disabled";
      self.data.datas.status = 6;
      self.data.updateTime.title = "已结束";
      buttonText = "已结束";
    }
    if (lag > 0 || lag2 > 0){
      self.data.updateTime.hour = isTimeLeng(hour);
      self.data.updateTime.minite = isTimeLeng(minite);
      self.data.updateTime.second = isTimeLeng(second);
    }
    self.setData({ datas: self.data.datas, updateTime: self.data.updateTime, buttonClass: buttonClass, buttonText: buttonText });
    clearTimeout(updateTimer);
    updateTimer = setTimeout(function () {
      self.updateTime();
    }, 1000);
    function isTimeLeng(val) {
      var s = val.toString();
      return s.length == 1 ? "0" + val : val;
    }
  },
})