var filters = {
	toFix: function (val, count) {
		if (!val) {
			return parseFloat(0).toFixed(count);
		}
		val = parseFloat(val);
		return val.toFixed(count)
	},
	replace_time: function (val) {
		if (!val) return "不限";
		return val.replace("00:00:00", "");
	},
	gettimestamp: function (date) {
		console.log(date);
		var timestamp = getDate(date).getTime();
		console.log(timestamp);
		timestamp = parseInt(timestamp / 1000)
		return timestamp;
	},

	replace_time2: function (val) {
		if (val == null || val == undefined || val == '') return "";
		var a = val.split(" ");
		return a[0];
	},

	format_time: function (ts, isFull) {
		// 如果数值位数为1，则补0
		function appendZero(obj) {
			if (obj < 10) {
				return "0" + "" + obj;
			} else {
				return obj;
			}
		};

		if (ts == null || ts == undefined || ts == '') return "";
		var d = getDate(ts * 1000)
		var fm = [appendZero(d.getFullYear()), appendZero(d.getMonth() + 1), appendZero(d.getDate())].join('-');
		var md = appendZero(d.getMonth() + 1) + '月' + appendZero(d.getDate()) + '日';
		if (isFull == 1)
			fm = fm + ' ' + [appendZero(d.getHours()), appendZero(d.getMinutes()), appendZero(d.getSeconds())].join(':');
		if (isFull == 2) {
			fm = md + ' ' + [appendZero(d.getHours()), appendZero(d.getMinutes())].join(':');
		}
		return fm;
	},

	format_time2: function (ts, isFull) {
		// 如果数值位数为1，则补0
		function appendZero(obj) {
			if (obj < 10) {
				return "0" + "" + obj;
			} else {
				return obj;
			}
		};

		if (ts == null || ts == undefined || ts == '') return "";
		var d = getDate(ts)
		var fm = [appendZero(d.getFullYear()), appendZero(d.getMonth() + 1), appendZero(d.getDate())].join('-');
		var md = appendZero(d.getMonth() + 1) + '月' + appendZero(d.getDate()) + '日';
		if (isFull == 1)
			fm = fm + ' ' + [appendZero(d.getHours()), appendZero(d.getMinutes()), appendZero(d.getSeconds())].join(':');
		if (isFull == 2) {
			fm = md + ' ' + [appendZero(d.getHours()), appendZero(d.getMinutes())].join(':');
		}
		return fm;
	},

	pInt: function (t) {
		var d = parseInt(t);
		return d;
	},

	show_gui_ge: function (spec, color) {
		if ((spec == "" || spec == null || spec == "null")) {
			if ((color == "" || color == null || color == "null")) {
				return "规格1";
			} else {
				return color;
			}
		} else {
			if ((color == "" || color == null || color == "null")) {
				return spec;
			} else {
				return spec + "/" + color;
			}
		}
	},


	show_default: function (value, placeholder, multiple) {
		if (typeof value === 'number') {
			if (isNaN(value)) {
				return placeholder || '-';
			} else if (value === 0) {
				return placeholder || '0';
			} else {
				if (multiple) {
					return value / multiple;
				} else {
					return value;
				}
			};
		} else if (value === '' || value === undefined || value === null) {
			return placeholder || '-';
		} else {
			return value;
		}
	},


	// 根据状态值返回相应的状态文字
	// num:状态数值,
	// arr: 状态文字数组,
	// 文字顺序必须跟状态数值对应
	status: function (num, arr,order_status) {
		// var text = '';
		// if(num == 0) {
		// 	text = '未付款';
		// } else if(num == 1) {
		// 	text = '已付款';
		// } else if(num == 2) {
		// 	text = '等待分成(已收货)';
		// } else if(num == 3) {
		// 	text = '已完成';
		// };

		if(order_status==6) return '已退款';
		return arr[num];
	},


	in_arr: function (index, arr) {
		if (arr.indexOf(index) != -1) {
			return true;
		} else {
			return false;
		};
	},

	showByType: function (typeStr) {
		var index;
		var newTypeArr = [];
		var typeStrArr = typeStr.split(',');
		typeStrArr.forEach(function (item) {
			index = parseInt(item);
			newTypeArr.push(index);
		});
		// console.log('newTypeArr', newTypeArr);
		return newTypeArr;
	},

	// 价格除以100
	price: function (val) {
		return val / 100;
	},

	div100: function (val) {
		return val / 100;
	},

	// 计算百分比, 不带百分号
	percent: function (num1, num2) {
		// console.log('百分比',Math.round(num1/num2 * 10000) / 100);
		return Math.round(num1 / num2 * 10000) / 100;
	},

	// 计算差数
	difference: function (num1, num2) {
		if (!num1) num1 = 0;
		if (!num2) num2 = 0;
		if (num1 >= num2) {
			return num1 - num2;
		} else {
			return num2 - num1;
		};
	},

	// 计算乘积
	multiply: function (num1, num2) {
		return num1 * 10000 * num2 / 10000;
	},

	JSONStringify: function (val) {
		return JSON.stringify(val);
	},

	// 循环列表计算数量
	count: function (list) {
		var sum = 0;
		if (list && list.length != 0) {
			list.forEach(function (item) {
				sum += item.qty;
			});
			return sum;
		};
	},

	// 判断时间是否过期
	isExpired: function (date) {
		var mydate = getDate(date).getTime();
		var currentDate = getDate().getTime();

		if (currentDate >= mydate) {
			return true;
		} else {
			return false;
		};
	},

	// 显示开始和结束日期
	showStartAndEndDate: function (o, val) {

		var dateType = o.date_type;
		var currentDate = getDate().getTime();
		var startDate, endDate, days, begin_days;


		if (dateType === 0) {

			if (o.effective_days !== 0) {
				days = (o.effective_days - 1) * 24 * 60 * 60 * 1000;
				endDate = filters.format_time2(currentDate + days);
				currentDate = filters.format_time2(currentDate);
				return currentDate + '至' + endDate;
			} else {
				if (val) {
					days = (val - 1) * 24 * 60 * 60 * 1000;
					endDate = filters.format_time2(currentDate + days);
					currentDate = filters.format_time2(currentDate);
					return currentDate + '至' + endDate;
				} else {
					currentDate = filters.format_time2(currentDate);
					return currentDate + '至不限';
				}
			}

		};

		if (dateType === 1) {
			currentDate = filters.format_time2(currentDate);
			if (o.begin_date && o.effective_date) {
				return o.begin_date + '至' + o.effective_date;
			};
			if (o.begin_date && !o.effective_date) {
				return o.begin_date + '至不限';
			};
			if (!o.begin_date && o.effective_date) {
				return currentDate + '至' + o.effective_date;
			};
			if (!o.begin_date && !o.effective_date) {
				return currentDate + '至不限';
			};
		};


		if (dateType === 2) {
			// N天后开始
			begin_days = o.begin_days * 24 * 60 * 60 * 1000;
			// 有效天数
			days = (o.effective_days - 1) * 24 * 60 * 60 * 1000;
			startDate = filters.format_time2(currentDate + begin_days);
			endDate = filters.format_time2(getDate(startDate).getTime() + days);
			return startDate + '至' + endDate;
		};
  },
  //判断加购按钮是否显示
  has_add_goods:function(type){
    var hide_add_goods=[4,6,8,9] //不显示加购按钮的活动
    var off = true
    for (var index = 0; index < hide_add_goods.length; index++) {
      if (hide_add_goods[index] === type) {
        off = false      
      }
    }
    return off
  }
};

module.exports = {
  has_add_goods:filters.has_add_goods,
	toFix: filters.toFix,
	replace_time: filters.replace_time,
	replace_time2: filters.replace_time2,
	format_time: filters.format_time,
	pInt: filters.pInt,
	show_gui_ge: filters.show_gui_ge,
	show_default: filters.show_default,
	status: filters.status,
	in_arr: filters.in_arr,
	percent: filters.percent,
	showByType: filters.showByType,
	difference: filters.difference,
	multiply: filters.multiply,
	price: filters.price,
	div100: filters.div100,
	JSONStringify: filters.JSONStringify,
	count: filters.count,
	isExpired: filters.isExpired,
	gettimestamp: filters.gettimestamp,
	showStartAndEndDate: filters.showStartAndEndDate,
	is_virtual_gd:function (is_virtual){
         if(is_virtual==2) return true;
		 return false;
	}
}