<?php include(mvc::$cfg['PATH_TPL'] . 'public/header.php'); ?>
<?php include(mvc::$cfg['PATH_TPL'] . 'wages/common.php'); ?>
<div class="list_pager">
	<div id="list_buttons"></div>
	<table id="grid_wages_grant"></table>
	<div id="jqGridPager"></div>
</div>
<style>
	.seqBgOne {
		background-color: #eef;
	}

	.seqFilter {
		background-color: #eee;
		text-decoration: line-through;
		color: #676a6c;
	}

	.seqCreate {
		background-color: #efe;
		color: #060;
	}

	.seqError {
		background-color: #fee;
		color: #900;
	}
</style>
<script type="text/javascript" defer>
	var grid = null;
	var form_title = '工资发放';
	var sop = {
		sopt: ['cn', 'eq', 'ne', 'le', 'lt', 'gt', 'ge', 'nc', 'nu', 'nn']
	};
	var send_settings = null;
	$(document).ready(function() {
		createGrid();
		createTools();
	});
	/**方法************************/
	var createGrid = function() {
		var coloptions = {
			sorting: false,
			filtering: false,
			columns: false,
			freeze: false,
			grouping: true
		};
		var height = $(document).height() - 160;
		grid = new qgGrid('grid_wages_grant', {
			url: HOST + 'wages/listGrant',
			autowidth: true,
			rowNum: 100,
			colMenu: true,
			height: height + 'px',
			colModel: [{
					label: 'ID',
					name: 'vim_id',
					key: true,
					hidden: true,
					width: 120,
					sorttype: 'integer',
					searchoptions: {
						sopt: ['cn', 'eq', 'ne', 'le', 'lt', 'gt', 'ge', 'nc', 'nu', 'nn']
					},
					colmenu: false
				},
				{
					label: '类型',
					name: 'vim_type',
					key: false,
					hidden: true,
					width: 120,
					searchoptions: {
						sopt: ['cn', 'eq', 'ne', 'le', 'lt', 'gt', 'ge', 'nc', 'nu', 'nn']
					},
					coloptions: coloptions
				},
				{
					label: '生成',
					name: 'vim_is_create',
					width: 35,
					search: false,
					sortable: false,
					searchoptions: sop,
					formatter: viewState,
					align: "center",
					colmenu: false
				},
				{
					label: '日期',
					name: 'vim_date',
					key: false,
					width: 120,
					searchoptions: {
						sopt: ['cn', 'eq', 'ne', 'le', 'lt', 'gt', 'ge', 'nc', 'nu', 'nn']
					},
					colmenu: false
				},
				{
					label: '组',
					name: 'vim_group',
					key: false,
					width: 90,
					searchoptions: {
						sopt: ['cn', 'eq', 'ne', 'le', 'lt', 'gt', 'ge', 'nc', 'nu', 'nn']
					},
					colmenu: false
				},
				{
					label: '借方科目',
					name: 'vim_j_text',
					key: false,
					width: 180,
					searchoptions: {
						sopt: ['cn', 'eq', 'ne', 'le', 'lt', 'gt', 'ge', 'nc', 'nu', 'nn']
					},
					colmenu: false
				},
				{
					label: '贷方科目',
					name: 'vim_d_text',
					key: false,
					width: 180,
					searchoptions: {
						sopt: ['cn', 'eq', 'ne', 'le', 'lt', 'gt', 'ge', 'nc', 'nu', 'nn']
					},
					colmenu: false
				},
				{
					label: '部门',
					name: 'vim_depa',
					key: false,
					hidden: false,
					width: 120,
					searchoptions: {
						sopt: ['cn', 'eq', 'ne', 'le', 'lt', 'gt', 'ge', 'nc', 'nu', 'nn']
					},
					coloptions: coloptions
				},
				{
					label: '职员',
					name: 'vim_staff',
					key: false,
					width: 120,
					searchoptions: {
						sopt: ['cn', 'eq', 'ne', 'le', 'lt', 'gt', 'ge', 'nc', 'nu', 'nn']
					},
					coloptions: coloptions,
					//formatter: viewLink,
					unformat: function(text) {
						return text;
					}
				},
				{
					label: '客户',
					name: 'vim_client',
					key: false,
					hidden: true,
					width: 120,
					searchoptions: {
						sopt: ['cn', 'eq', 'ne', 'le', 'lt', 'gt', 'ge', 'nc', 'nu', 'nn']
					},
					coloptions: coloptions,
					formatter: viewLink,
					unformat: function(text) {
						return text;
					}
				},
				{
					label: '金额',
					name: 'vim_money',
					key: false,
					width: 120,
					sorttype: 'integer',
					searchoptions: {
						sopt: ['cn', 'eq', 'ne', 'le', 'lt', 'gt', 'ge', 'nc', 'nu', 'nn']
					},
					cellattr: moneyAttr,
					formatter: viewMoney,
					unformat: unMoney,
					colmenu: false,
					summaryType: "sum",
					summaryTpl: "<span style='color:#090'><i class='fa fa-rmb'></i> {0}</span>",
					align: 'right'
				},
				{
					label: '备注',
					name: 'vim_summary',
					key: false,
					width: 220,
					searchoptions: {
						sopt: ['cn', 'eq', 'ne', 'le', 'lt', 'gt', 'ge', 'nc', 'nu', 'nn']
					},
					colmenu: false
				},
			],
			footerrow: true,
			groupingView: {
				groupColumnShow: [true],
				groupText: ["<b>{0}</b>"],
				groupSummary: [true],
				groupOrder: ["asc"],
				groupCollapse: false
			},
			loadBeforeSend: function(xhr, settings) {
				send_settings = settings;
				return settings;
			},
			gridComplete: function() {
				grid.grid.footerData('set', {
					"vim_staff": '合计',
					vim_money: grid.grid.getCol('vim_money', false, 'sum').toFixed(2),
				});
				//行变色
				var ids = grid.grid.getDataIDs();
				var old = '';
				var ischangebg = '';
				var regi = 0;
				for (var i = 0; i < ids.length; i++) {
					var rowData = grid.grid.getRowData(ids[i]);
					if (old == '') old = rowData.cycleseq;
					if (rowData.cycleseq == old) {

					} else {
						regi++;
						old = rowData.cycleseq;
					}
					if (regi % 2 != 0)
						$('#' + ids[i]).find("td").removeClass('seqFilter').addClass("seqBgOne");
					if (rowData.vim_is_create == 1 || rowData.vim_is_create.indexOf('check') != -1) {
						$('#' + ids[i]).find("td").removeClass('seqBgOne').addClass("seqCreate");
					}
				}

			},
			onSelectRow: function(key, status, event) {
				;
			},
			onEnter: function(rowid) {
				if (rowid) {
					var viewUrl = urls.view + '?key=' + rowid;
					var row = grid.selectRowData();
					qgui.form.view(row.w_staff_text, viewUrl)
				}
			}
		});
	};

	var createTools = function() {
		qgui.tableButton({
			dom: '#list_buttons',
			grid: grid,
			title: form_title,
			buttons: [{
					btn: 'refresh'
				},
				{
					btn: 'search'
				},
				/*{btn:'add',url:urls.add},*/
				/*{btn:'edit',url:function(){
					var key = grid.selectRowKey();
					if(key!=null){
						return urls.edit+'&key='+key;
					}else{
						qgui.box.timer('请先选择一条记录');
						return '';
					}
				}},*/
				{
					btn: 'delete',
					url: function() {
						qgui.box.input('请输入一个工资月份, 例 202003', function(value) {
							value = value.trim();
							if (value.length != 6) {
								qgui.box.msg('格式不正确');
							} else {
								qg.ajax.post(HOST + '/wages/deleteGrant', {
									cycle: value
								}, function(data) {
									qgui.box.msg('删除完成');
									gridReload();
								});
							}
						});
					}
				},
				/*{btn:'import'},*/
				{
					btn: 'export'
				},
				{
					btn: 'tools'
				},
				{
					btn: 'ui_showhideCol'
				},
				{
					btn: 'ui_save'
				},
				{
					btn: 'ui_reset'
				},
			],
			customButtons: [{
					btn: 'btn1',
					caption: '导入-工资发放',
					icon: 'fa fa fa-upload',
					type: 'btn-white',
					click: function(e) {
						qgui.page.open('导入-工资发放', HOST + 'wages/importGrant.html');
					},
				},
				{
					btn: 'btn2',
					caption: '生成当前结果的凭证',
					icon: 'fa fa-credit-card',
					type: 'btn-white',
					click: function(e) {
						qg.ajax.post(HOST + '/wages/createVoucherFromImportGrant', {
							send: send_settings.data
						}, function(data) {
							qgui.box.msg('生成凭证完成');
							gridReload();
						});
					}
				}
			],
		});
	};

	var gridReload = function() { //供弹窗调用
		grid.reload();
	};
	//列格式化
	//在colModel列中加上 formatter:viewLink,unformat:function(text){return text;}
	var viewLink = function(cellValue, options, rowObject, action) {
		var key = rowObject.w_id;
		var viewUrl = urls.view + '?key=' + key;
		var title = escape(cellValue);
		//return "<a href='javascript:;' onclick=qgui.form.view('"+title+"','"+viewUrl+"')>"+cellValue+"</a>";
		title = rowObject.w_cycle + '-' + cellValue;
		return "<a href='javascript:;' onclick=qgui.page.open('" + title + "','" + viewUrl + "')>" + cellValue + "</a>";
	}
	var viewMoney = function(cellValue, options, rowObject, action) {
		if (cellValue == '0' || cellValue == '0.00' || !cellValue)
			cellValue = '';
		else
			cellValue = qg.num.money.format(cellValue);
		return cellValue;
	};
	var unMoney = function(cellValue, options, rowObject) {
		return qg.num.money.unformat(cellValue);
	};

	function moneyAttr(rowId, val, rawObject, cm, rdata) {
		if (val < 0) {
			return "style='color:red'";
		}
	}
	var viewState = function(cellValue, options, rowObject, action) {
		if (rowObject.filter == true || rowObject.filter == 'true') {
			return '';
		} else if (cellValue == '1') {
			return '<i class="fa fa-check" style="color:#080"></i>';
		} else {
			return '<i class="fa fa-circle-o" style="color:red"></i>'
		}
	};
</script>
<?php include(mvc::$cfg['PATH_TPL'] . 'public/footer.php'); ?>