/**
 * ${comments}js
 */

$(function () {
	initialPage();
	getGrid();
});

function initialPage() {
	$(window).resize(function() {
		$('#dataGrid').bootstrapTable('resetView', {height: $(window).height()-56});
	});
}

function getGrid() {
	$('#dataGrid').bootstrapTableEx({
		url: '../../${requestMapping}/list?_' + $.now(),
		height: $(window).height()-56,
		queryParams: function(params){
			params.name = vm.keyword;
			return params;
		},
		columns: [
			{checkbox: true},
<% for(column in columns!) { %>
    <% if(column.columnName != pk.columnName || pk.extra != 'auto_increment') { %>
            {field : "${column.fieldName}", title : "${column.columnComment}", width : "100px"},
    <% } %>
<% } %>
            {title : "操作", formatter : function(value, row, index) {
                    var _html = '';
                    if (hasPermission('${authKey}:edit')) {
                        _html += '<a href="javascript:;" onclick="vm.edit(\''+row.${pk.fieldName}+'\')" title="编辑"><i class="fa fa-pencil"></i></a>';
                    }
                    if (hasPermission('${authKey}:remove')) {
                        _html += '<a href="javascript:;" onclick="vm.remove(false,\''+row.${pk.fieldName}+'\')" title="删除"><i class="fa fa-trash-o"></i></a>';
                    }
                    return _html;
                }
            }
		]
	})
}

var vm = new Vue({
	el:'#dpLTE',
	data: {
		keyword: null
	},
	methods : {
		load: function() {
			$('#dataGrid').bootstrapTable('refresh');
		},
		save: function() {
			dialogOpen({
				title: '新增${comments}',
				url: '${viewPath}/add.html?_' + $.now(),
				width: '420px',
				height: '350px',
				yes : function(iframeId) {
					top.frames[iframeId].vm.acceptClick();
				},
			});
		},
		edit: function(${pk.fieldName}) {
            dialogOpen({
                title: '编辑${comments}',
                url: '${viewPath}/edit.html?_' + $.now(),
                width: '420px',
                height: '350px',
                success: function(iframeId){
                    top.frames[iframeId].vm.${objName}.${pk.fieldName} = ${pk.fieldName};
                    top.frames[iframeId].vm.setForm();
                },
                yes: function(iframeId){
                    top.frames[iframeId].vm.acceptClick();
                }
            });
        },
        remove: function(batch, ${pk.fieldName}) {
            var ids = [];
            if (batch) {
                var ck = $('#dataGrid').bootstrapTable('getSelections');
                if (!checkedArray(ck)) {
                    return false;
                }
                $.each(ck, function(idx, item){
                    ids[idx] = item.${pk.fieldName};
                });
            } else {
                ids.push(${pk.fieldName});
            }
            $.RemoveForm({
                url: '../../${requestMapping}/remove?_' + $.now(),
                param: ids,
                success: function(data) {
                    vm.load();
                }
            });
        }
	}
})