<%layout("/eova/layout/dialog.html",{'title' : 'Eova Find'}){%>

<body class="easyui-layout">
    <div id="layoutNorth" region="north" split="true" style="height:40px;">
    <#query1 id="queryFindForm" items="${itemList}" />
    </div>
    <div data-options="region:'center',fit:false,border:false">
	    <%include("/eova/widget/find/toolbar.html"){}%>
	    <#grid id="findGrid" toolbar="toolbar" objectJson="${objectJson}" fieldsJson="${fieldsJson}" url="${action!}" isPaging="${isPaging!true}" />
    </div>
<script>
var $findGrid = $('#findGrid');

$(function(){
	//绑定双击事件
	$findGrid.datagrid({
		onDblClickRow : function(rowIndex, rowData){
			// 模拟点击确认
			parent.$('#find_ok').trigger('click');
		},
		onLoadSuccess : function(data){
			// 初始值选中
			var val = "${parameter.val}";
			if(val && val != ''){
				var vals = val.split(',');
				$.each(data.rows, function(i, o) {
					$.each(vals, function(iv, val) {
						if(o.${pk} == val){
							$findGrid.datagrid('selectRow', i);							
						}
					});
				});
			}
		}
	});
});

var object = JSON.parse('${objectJson}');
var selectData = function($dialog, findBox) {

    var rows = $findGrid.datagrid('getSelections');
    try{
    	// 有选中行
        var oldValue = findBox.$valuebox.val();
        var newValue = '';
        if (rows.length != "0") {
	        var vals = [], txts = [];
	        for (var i = 0; i < rows.length; i++) { //组成一个字符串，ID主键用逗号隔开
	        	vals.push(rows[i][object.pk_name]);
	            txts.push(rows[i][object.cn]);
	        }
        	// 获取主键值(第一列)
            findBox.$valuebox.val(vals.join(','));
        	// 获取翻译值(第二列)
            findBox.$textbox.val(txts.join(','));
            newValue = findBox.$valuebox.val();
    	} else {
    		// 没选值，就清空
            findBox.$valuebox.val("");
            findBox.$textbox.val("");
    	}
        // 值变更回调
        if (oldValue != newValue) {
            findBox.options.onChange.call(findBox.$dom, oldValue, newValue, rows); // 回调带回所选行,可取其它列值.
        }
    }catch(err){
    	console.log("查找框validate异常:" + err);
    }finally{
    	// 关闭Dialog
    	$dialog.dialog('destroy');
    }
};

var clearData = function($dialog, findBox) {

    try{
        var oldValue = findBox.$valuebox.val();
        var newValue = '';

        findBox.$valuebox.val("");
        findBox.$textbox.val("");

        // 值变更回调
        if (oldValue != newValue) {
            console.log(oldValue +'|'+newValue);
            findBox.options.onChange.call(findBox.$dom, oldValue, newValue);
        }
    }catch(err){
        console.log("查找框异常:" + err);
    }finally{
        $dialog.dialog('destroy');
    }
};

</script>
</body>
<%}%>
