/**
 * 初始化 kendo ui Table list tree 的封装
 * @author LiaoYun 2019-09-17  945505778@qq.com
 */
(function () {
	var KDListTreeTable = function (kdtableId, url, columns) {
		this.kdtableId = kdtableId;
		this.url = Feng.ctxPath + url;
		this.method = "post";
		this.columns = columns;
		this.height = 595;						                                    //默认表格高度665
		this.fields = {};                                                           //data type of the field {number|string|boolean|date} default is string
		this.code = '';
		this.parentCode = '';
		this.resizable = true;                                                      //表格宽度允许拖动
		this.reorderable = true;                                                    //是否允许拖动列来改变列的显示顺序
		this.selectable = true;                                                     //是否允许选择
		if (columns!=null && columns.length>0) {
			for(var i=0; i<columns.length; i++){
				var temp = columns[i];
				if (!temp) {
					break;
				}
				if (temp.type!=null && temp.type!='' && typeof(temp)!="undefined") {
					this.fields[temp.field] = {'type': temp.type};
				} else {
					this.fields[temp.field] = {'type': 'string'};
				}
			}
		}
	};
	
	KDListTreeTable.prototype = {
		/**
         * 初始化kendo list tree table
         */	
		init: function(){
			var tableId = this.kdtableId;
			var me = this;
			this.btInstance = $("#" + tableId).kendoTreeList({
				columns: this.columns,
				dataSource: {
					transport: {
						read: {
							url: this.url,
							type: this.method,
							dataType: "json",
							contentType: "application/x-www-form-urlencoded"
						},
						parameterMap: function(options, operation){
							console.log(options);
							if (operation == "read") {
								var parameter = {};
								$.each(options, function(name, value){
									parameter[name] = value;
								});
								return parameter;
							}
						}
					},
					schema: {
						model: {
							id: this.code,
			                parentId: this.parentCode,
			                fields: this.fields
						}
					}
				},
				resizable: this.resizable,
				height: this.height,
				reorderable: this.reorderable,
				selectable: this.selectable,
				refresh: true
			});
			return this;
		},
		setTreeCode: function(code, parentCode) {
			this.code = code;
			this.parentCode = parentCode;
		},
		getSelections: function() {
			var grid = $("#"+this.kdtableId+",.k-grid-content").data("kendoTreeList");
			var selected = [];
			var rows = grid.select();
//			console.log(rows);
			for(var i=0; i<rows.length; i++){
				var temp = rows.eq(i);
				var className = temp.parent().parent().parent().attr("class");
//				console.log("class=" + temp.parent().parent().parent().attr("class"));
				if (className === 'k-grid-content-locked') {
					continue;
				}
				selected.push(grid.dataItem(rows[i]))
			}
			console.log(selected);
			return selected;
		},
		refresh: function(val) {
			var grid = $("#"+this.kdtableId).data("kendoTreeList");
			if (val && val.query) {
				var filter = [];
				var flag = false;
				$.each(val.query, function(name, value){
					if (value!=null && value!='' && typeof(value)!="undefined") {
						flag = true;
					} else {
						delete val.query[name];
					}
				});
				if (flag) {
					val.query.page = 1;
					val.query.pageSize = this.pageSize;
					val.query.take = this.pageSize;
					val.query.skip = 0;
					grid.dataSource.query(val.query);
				} else {
					grid.dataSource.read();
				}
			} else {
				grid.dataSource.read();
			}
		}
	};
	
	window.KDListTreeTable = KDListTreeTable;
}());