var pageii = null;
var el = {
	tb: '#tb'
};
var $table;
$(function () {
	loadGrid();
});


function loadGrid() {
	$('#tb').datagrid({
		iconCls: 'icon-list',
		title: '',
		height: gridheight - 20,
		width: gridwidth,
		url: g.ctx+'pc/sys/msgTemplate/rest/list',
		singleSelect: true,
		pagination: true,
		idField: 'id',
		autoHeight: true,
		loadMsg: "数据加载中......",
		rownumbers: true,
		fitColumns: false,
		remoteSort: true,
		page: 1,
		pageSize: 10,
		queryParams: {
			page: 1,
			rows: 10,
			name: "",
			desc: ""
		},
		onLoadSuccess: function () {
		},
		columns: [[
			{
				field: 'ck',
				checkbox: true
			},
			{
				field: 'msgCode',
				title: '邮件模板编码',
				width: '20%',
				align: 'left'

			},
			{
				field: 'msgName',
				title: '邮件模板标题',
				width: '20%',
				align: 'left'

			},
			{
				field: 'remark',
				title: '备注',
				width: '40%',
				align: 'left'

			},
			{
				field: 'createTime',
				title: '创建时间',
				width: '20%',
				align: 'left',
				formatter: function (create_time) {
					if (create_time == '' || create_time == null || create_time == undefined)
						return '';
					return DateTimeFormatMs(create_time);
				}

			},
		]],
		toolbar: [{
			text: '新增',
			iconCls: 'icon-add',
			hidden: true,
			handler: function () {
				detail(0);
			}
		}, "-", {
			text: '编辑',
			iconCls: 'icon-edit',
			handler: function () {
				detail(1);
			}
		}, "-", {
			text: '删除',
			hide: true,
			iconCls: 'icon-remove',
			handler: function () {
				delNotice();
			}
		}
		]
	});
}



function detail(flag) {
	var id= '';
	if(flag===1){
		var cbox = $('#tb').datagrid('getSelected');
		if (cbox === null || cbox === undefined) {
			layer.msg("请选中一条记录");
			return;
		}
		id = cbox.id;
	}
	var noticeDialog = openDialog('msgtemplateDialog', g.ctx + 'pc/sys/msgTemplate/detail?id=' + id, {
		title: flag===0?'新增':'编辑',
		width: '90%',
		height: '90%',
		cache: false,
		modal: true,
		border: false,
		draggable: false,
		shadow: false,
		onClose: function () {
			noticeDialog.dialog('destroy');
		},
		buttons: [
			{
				text: itablei8n.save,
				iconCls: 'icon-save',
				handler: function () {
					topWin.frames['msgtemplateDialog'].saveOrUpdate(function () {
						topWin.layer.msg('数据保存成功', {icon: 1});
						noticeDialog.dialog('close');
						$(el.tb).datagrid('reload');
					});
				}
			},
			{
				text: itablei8n.close,
				iconCls: 'icon-cancel',
				handler: function () {
					noticeDialog.dialog('close');
				}
			}
		]
	});
}
function delNotice() {
	var cbox = $('#tb').datagrid('getSelected');
	if (cbox === null || cbox === undefined) {
		layer.msg("请选中一条记录");
		return;
	}

	var noticeId = cbox.id;

	parent.layer.confirm('是否删除？', function (index) {
		var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
		var url = g.ctx+'pc/sys/msgTemplate/rest/remove';
		$.ajax({
			url: url,
			method: "post",
			data: {id: noticeId},
			success: function (data) {
				parent.layer.close(index);
				$(el.tb).datagrid('reload');
			},
			complete: function(data) {
				top.layer.close(loading);
			},
			error: function (data) {
				parent.layer.close(index);
				parent.layer.alert(JSON.parse(data.responseText).message);
			}
		});
	});
}


