subjectManage.js 6.63 KB
var $ = require('jquery'),
	common = require('../common/common');


	new common.dropDown({el: "#subject-type"});
	new common.dropDown({el: "#subject-paltform"});
	new common.dropDown({el: "#subject-status"});

	var g = new common.grid({
		el: "#subject-manage",
		parms: function () {
			return {
				sort: common.util.__input('subject-type'),
				plateform: common.util.__input('subject-paltform'),
				status: common.util.__input('subject-status')
			};
		},
		columns: [
			{display: "ID", name: "id"},
			{display: "分类", name: "sort", render: function (item) {
				if(item.sort == "1") return "奥莱";
				else if(item.sort == "2") return "SALE";
			}},
			{display: "排序", name: "orderNum"},
			{display: "标题", name: "title"},
			{display: "封面图", name: "coverUrl", render: function (item) {
				return '<img src="'+item.coverUrl+'" height="100px" />';
			}},
			{display: "促销形式", name: "promotionName"},
			{display: "平台", name: "plateform", render: function (item) {

				if(item.plateform != null && item.plateform.trim() != "") {
					var paltform = item.plateform;
					var temp = paltform.replace("1", "WEB").replace("2", "APP").replace("3", "WAP").replace("4", "IPAD");
					return temp;
				}
			}},
			{display: "状态", name: "status", render: function (item) {
				if(item.status == 1) return "未定时";
				else if(item.status == 2) return "未发布";
				else if(item.status == 3) return "已发布";
				else if(item.status == 4) return "已结束";
			}},
			{display: "创建时间", name: "createTime", render: function (item) {
				if(!item.createTime) return "";
				return Bll.getLocalTime(item.createTime);
			}},
			{display: "活动时间", name: "", render: function (item) {
				// return new Date(item.startTime).toStri34ng() + "~" + new Date(item.endTime).tostring();
				if(!item.startTime || !item.endTime) return "";
				return Bll.getLocalTime(item.startTime) + "~" + Bll.getLocalTime(item.endTime);
			}},
			{
				display: "操作", name: "", render: function (item) {
					var arr = [];
					arr.push('<a class="btn btn-info add2" data-index="' + item.__index + '">编辑</a>');
					arr.push('<a class="btn btn-info delbtn" data-index="' + item.__index + '">删除</a>');
					return arr.join("");
				}
			}

		]
	});

	g.init('/runManage/subjectManage/subjectList');

	var Bll = { 
		getLocalTime:function(nS) {
		    var date = new Date(parseInt(nS) * 1000);
		    var mm = date.getMonth() + 1;
		    var dd = date.getDate();
		    return date.getFullYear() + "-" + (mm < 10 ? "0" + mm : mm) + "-" + (dd < 10 ? "0" + dd : dd);
		},
		toast:function(url, item, hint) {
			var e = new common.edit("#baseform", {bucket:"activity"});
			
			e.on("validate",function() {
				var startTime=$("#startTime").val();
				var endTime=$("#endTime").val();
				if((startTime==""&&endTime=="") || (startTime!=""&&endTime!="")){}else{
					return "开始时间和结束时间必须同时存在";
				}
			});
			e.on("validate", function() {
				var temp = $("#productPoolId").val();
				if(!temp || temp == -1) {
					return "请填写商品池";
				}
			});

			e.on("validate", function() {
				if(($("#promotion1").is(":checked") && !$("#promotionName1").val())
				|| ($("#promotion2").is(":checked") && (!$("#promotionName2").val() || !$("#promotionName3").val()))
				|| ($("#promotion4").is(":checked") && !$("#promotionName4").val())
				|| ($("#promotion5").is(":checked") && !$("#promotionName5").val())) {
					return "请将折扣形式填写完整";
				}
			});

			var dialog=common.dialog.confirm(hint,
				common.util.__template($("#template").html(), item),
				function() {
					e.submit(url,function(option){
						//option.data;
						if(typeof option.data.startTime == "string") {
							option.data.startTime=new Date(option.data.startTime).getTime() / 1000;
							option.data.endTime=new Date(option.data.endTime).getTime() / 1000;
						}

						var val=$('input:radio[name="promotionName"]:checked').val();
						if(val == 1) {
							option.data.promotionName = $("#promotionName1").val() + "折起";
						}else if(val == 2) {
							option.data.promotionName = $("#promotionName2").val() + "~" + $("#promotionName3").val() + "折";
						}else if(val == 4) {
							option.data.promotionName = $("#promotionName4").val() + "元起";
						}else if(val == 5) {
							option.data.promotionName = $("#promotionName5").val() + "%Off";
						}
						
						option.success=function(){
							dialog.close();
							g.reload();
						};
						option.error=function(){
							dialog.close();
						}
					});
					return false;
				});
			//
			if(item.promotionName) {
				if(item.promotionName.indexOf("折起") != -1) {
					$('#promotionName1').val(item.promotionName.replace("折起", ""));
					$("#promotion1").attr("checked","checked");
				} else if(item.promotionName.indexOf("折") != -1) {
					$('#promotionName2').val(item.promotionName.split("~")[0]);
					$('#promotionName3').val(item.promotionName.split("~")[1]);
					$("#promotion2").attr("checked","checked");
				} else if(item.promotionName.indexOf("元起") != -1) {
					$('#promotionName4').val(item.promotionName.replace("元起", ""));
					$("#promotion4").attr("checked","checked");
				} else if(item.promotionName.indexOf("%") != -1) {
					var promotionName5 = $("#promotionName5");
					var temp = item.promotionName.replace("%", "");
					temp = temp.replace(/off/i, "");
					promotionName5.val(temp);
					$("#promotion5").attr("checked","checked");
				}
			}
			console.log($("#promotion1").val());

			e.init();
			new common.dropDown({el: "#productPoolId","ajax":"productPool"});

			if(hint == "专题编辑") {
				common.util.__ajax({
		 			url:'/runManage/subjectManage/queryBaseProductPoolListById',
		 			data:{id:item.productPoolId}
		 		},function(res) {
		 			$("#select2-productPoolId-container").html(res.data[0].text);
		 		},true);
			}
		}
};

	$('#add-subject').on('click', function() {
		var item = {};
		Bll.toast('/runManage/subjectManage/addSubject', item, "专题添加");
	});

	$(document).on('click', '.add2', function() {
		var item = g.rows[$(this).data("index")];
		if(typeof item.startTime == "number") {
			item.startTime = Bll.getLocalTime(item.startTime);
			item.endTime = Bll.getLocalTime(item.endTime);
		}
		Bll.toast('/runManage/subjectManage/modify', item, "专题编辑");
	});

	$(document).on('click', '.delbtn', function() {
		 var item=g.rows[$(this).data("index")];
		 common.dialog.confirm("警告", 
		 	"确认删除?",
		 	function() {
		 		common.util.__ajax({
		 			url:'/runManage/subjectManage/delSubject',
		 			data:{id:item.id}
		 		},function() {
		 			g.reload();
		 		});
		 	});
	});

	$(document).on("click","#filter-btn",function () {
		g.reload(1);
	});