noticeManage.js 3.77 KB
var $ = require('jquery'),
	common = require('../common/common');

	new common.dropDown({el: "#notice-position"});

	var g = new common.grid({
		el: "#notice-list",
		parms: function () {
			return {
				title: common.util.__input('notice-title'),
				position: common.util.__input('notice-position')
			};
		},
		columns: [
			{display: "ID", name: "id"},
			{display: "公告", name: "title"},
			{display: "位置", name: "position", render: function(item) {
				if(item.position == 1) {
					return "首页";
				} else if(item.position == 2) {
					return "个人中心";
				}
			}},
			{display: "url地址", name: "url"},
			{display: "开始时间",name: "startTime"},
			{display: "结束时间",name: "endTime"},
			{
				display: "状态",name: "", render: function (item) {
					if(item.status == 0) {
						return '<a class="btn btn-danger statbtn" data-index="' + item.__index + '">公告已关闭</a>';
					} else {
						return '<a class="btn btn-warning statbtn" data-index="' + item.__index + '">公告已开启</a>';	
					}
				}
			},
			{
				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('/operations/notice/getList');

	var Bll = {
		toast:function(url, item, hint) {
			var e = new common.edit("#baseform");

			common.dialog.confirm(hint,
				common.util.__template($("#template").html(), item),
				function() {
					e.submit(url, function (option) {
						option.data.startTime=new Date(option.data.startTime).getTime() / 1000;
						option.data.endTime=new Date(option.data.endTime).getTime() / 1000;
						if($("#intent").val()) {
							option.data.url = '{"action":"go.' + $("#intent").val() 
							+ '","url":"' + $("#url").val() + '"}';
						}
						option.success=function() {
							console.log(item.showChannels);
							g.reload();
						};
						option.error=function(){};
					});
				});
			e.init();

			new common.dropDown({el: "#position"});
			// new common.dropDown({el: "#intent"});

			if($("#position").val() == "1") {
				$("#showchannel").show();
			} else {
				$("#showchannel").hide();
			}

			$("#position").on('change', function() {
				if($("#position").val() == "1") {
					$("#showchannel").show();
				} else {
					$("#showchannel").hide();
				}
			});

			if(item.url && item.url.indexOf('"action"') != -1) {
				var urlIndex = '","url":"';
				$("#intent").val(item.url.substring('{"action":"go.'.length, item.url.indexOf(urlIndex)));
				$("#url").val(item.url.substring(item.url.indexOf(urlIndex) + urlIndex.length, item.url.length - 2));
			}

		}
	};

	$('#add-notice').on('click', function() {
		var item = {};
		Bll.toast('/operations/notice/addNotice', item, "创建公告");
	});

	$(document).on('click', '.add2', function() {
		var item = g.rows[$(this).data("index")];
		console.log(item);
		Bll.toast('/operations/notice/updateNotice', item, "公告编辑");
	});

	$(document).on('click', '.statbtn', function() {
		var statbtn = $(this);
		var item = g.rows[$(this).data("index")];
		if(item.status == 1) {
			item.status = 0;
		} else {
			item.status = 1;
		}
		common.util.__ajax({
			url: '/operations/notice/setStatus',
			data: {
				id: item.id,
				status: item.status
			}
		}, function() {
			g.reload();
		});
	});


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

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