var CommonModel = {
    getFomattedDate: {}, //格式化日期函数
    toDouble: {}, //处理时间位数
    isTimeInvalid: {}, //时间限制

    avoidIOSMistakeTouch: {}, //避免ios滑动误触
}
CommonModel.getFomattedDate = function(date, format) {
    var date = new Date(date);
    if (!format) {
        format = "yyyy-MM-dd hh:mm:ss";
    }

    var o = {
        "M+": date.getMonth() + 1, //month
        "d+": date.getDate(), //day
        "h+": date.getHours(), //hour
        "m+": date.getMinutes(), //minute
        "s+": date.getSeconds(), //second
        "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
        "S": date.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}

CommonModel.toDouble = function(str) {
    str = str.toString();
    str = str.length == 1 ? ('0' + str) : str;
    return str;
}


CommonModel.isTimeInvalid = function(startTime, endTime) {
    startTime = new Date(startTime).getTime();
    if (!endTime) { //判断传入的时间是否超过当前时间
        var today = new Date().getTime();
        if (startTime > today) {
            return true;
        }

    } else { //判断传入的开始时间是否大于传入的结束时间
        endTime = new Date(endTime).getTime();
        if (startTime > endTime) {
            return true;
        }
    }
}

//获取格式化日期和格式
//返回如 今天14:21  昨天01:12  2014-03-12
//依赖moment.js
CommonModel.formatDateTime = function(time) {

    if (typeof(time) == 'number') {
        time = new Date(time);
    } else {
        time = new Date(time.replace(/-/g, '/'));
    }

    var _then = moment([time.getFullYear(), time.getMonth(), time.getDate()]);
    var now = new Date();
    var _now = moment([now.getFullYear(), now.getMonth(), now.getDate()]);
    var days = _now.diff(_then, "days");
    var content = "";
    if (days == 0) {
        content = "今天 " + moment(time).format("HH:mm");
    } else if (days == 1) {
        content = "昨天 " + moment(time).format("HH:mm");
    } else {
        content = moment(time).format("YYYY-MM-DD");
    }
    return content;
}

//避免ios误触
function avoidIOSMistakeTouch() {
    var isMove = false;
    document.documentElement.addEventListener("touchstart", function() {
        isMove = false;
    })

    document.documentElement.addEventListener("touchmove", function() {
        isMove = true;
    })

    document.documentElement.addEventListener("touchend", function(e) {
        if (isMove) {
            e.stopPropagation()
            return false;
        }
    }, true);
}
avoidIOSMistakeTouch();
CommonModel.fitInStatusBar = function() {
  // 适配状态栏
  var statusBarHeightDom = document.getElementById('statusBarHeight');

  if (api.systemType == "android") {
      var statusBar = api.require('statusBar');

      var statusBarHeight = statusBar.getStatusBarHeight();
      if (statusBarHeightDom) {
        // statusBarHeightDom.style.height = statusBarHeight + 'px';
        statusBarHeightDom.style.height = api.safeArea.top + 'px';
      }

      var heightHeaderDom = document.getElementById('heightHeader');
      var allHeight = $api.cssVal($api.dom('header'), 'height');
      if(heightHeaderDom) {
        heightHeaderDom.style.height = allHeight;
      }
  } else {
      statusBarHeightDom.style.height = '1.67rem';
  }
}


/**
 * 弹出提示框
 */
function toast(msg) {
    api.toast({
        msg: msg,
        duration: 3000,
        locaiton: 'top'
    });
}

// 位置分类

var POSITIONCLASSIFY  = [
	'',
  '高压侧',
  '低压侧',
  '变压器',
  '直流屏',
  '信号屏',
  '其他'
];
