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/notice/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: 'title',
				title: '标题',
				width: '30%',
				align: 'left'

			},

            {
                field: 'publishTime',
                title: '发布时间',
                width: '15%',
                align: 'left',
                formatter: function (publish_time) {
                    if (publish_time == '' || publish_time == null || publish_time == undefined)
                        return '';
                    return DateTimeFormatMs(publish_time);
                }

            },

			{
				field: 'status',
				title: '状态',
				width: '10%',
				align: 'left',
				formatter: function (value) {

					if (value === 0) {
						return '新建';
					} else {
						return '发布';
					}
				}

			},

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

			},
            {
                field: 'publishUserName',
                title: '发布人',
                width: '15%',
                align: 'left'

            },

			{
				field: 'publicflag',
				title: '是否公开',
				width: '10%',
				align: 'left',
				formatter: function (value, row, index) {
					if (value === "0") {
						return '不公开';
					} else {
						return '公开';
					}
				}

			},
			{
				field: 'pic',
				title: '路径',
				width: '11%',
				align: 'left',
				hidden: 'true',

			}
		]],
		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();
			}
		}, "-", {
			text: '发布',
			hide: true,
			iconCls: 'icon-ok',
			handler: function () {
				operate();//状态 2 发布
			}
		}
		]
	});
}



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('noticeDialog', g.ctx + 'pc/sys/notice/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['noticeDialog'].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/notice/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);
			}
		});
	});
}

function operate() {
	var cbox = $('#tb').datagrid('getSelected');
	if (cbox === null || cbox === undefined) {
		layer.msg("请选中一条记录");
		return;
	}

	if (cbox.status === 1) {
		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});
		$.ajax({
			url: g.ctx + 'pc/sys/notice/rest/publish',
			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);
			}
		});
	});
}
