//teUtils 表格文件处理
namespace teUtils;
import io;
import fsys.dlg;
import fsys.codepage;

var currentFilePath = null;

setCurFilePath = function(path){
	currentFilePath = path;
}

getCurFilePath = function(){
	return currentFilePath;
}

generateColFields = function(limit){
    var combinations = {};
    letters = {};
    count = 0;
    for(i=65;90;1){
        if(count >= limit){
            break;
        }
        ..table.push(combinations, ..string.pack(i))
        ..table.push(letters, ..string.pack(i))
        count += 1;
    }
    
    for(_,letter1 in letters){outer_loop:
        for(_,letter2 in letters){
            if(count >= limit){
                break outer_loop;
            }
            ..table.push(combinations, letter1 + letter2)
            count += 1;
        }
    }
    return combinations;
}

initFlexCellShow = function(fc){
	var col_sum = 100;
	var row_sum = 100;
	var t = {};
	t.fields = {};
	t.fields = ..table.append(t.fields, generateColFields(col_sum));
	for(i=1; row_sum; 1){
    	var row = {};
    	for (j=1; #t.fields; 1) {
        	row[t.fields[j]] = "";
    	}
    	..table.push(t, row);
	}
	fc.AutoRedraw = false;
	fc.setTable(t, t.fields, t.fields);
	fc.column(0).AutoFit();
	if(fc.Column(0).Width < 40){
		fc.Column(0).Width = 40;
	}
	fc.AutoRedraw = true;
	fc.Refresh();
}

insertFlexCellNewRow = function(flexcell, start_row, add_row_count, colCount){
	var t = {};
	t.fields = {};
	t.fields = ..table.append(t.fields, generateColFields(colCount));
	for(i=1; add_row_count; 1){
    	var row = {};
    	for (j=1; #t.fields; 1) {
        	row[t.fields[j]] = "";
    	}
    	..table.push(t, row);
	}
	flexcell.insertRow(start_row/*插入行号*/, t/*插入行的数据表*/);
}

insertFlexCellNewCol = function(flexcell, start_col, add_col_count, colCount){
	var new_fields = generateColFields(colCount + add_col_count);
	flexcell.insertCol(start_col, add_col_count, new_fields);
}

getTxtTableData = function(reference_row = 2){
	var fileName = ..fsys.dlg.open("txt file(*.txt)|*.txt|db file(*.db)|*.db|sqlite file(*.sqlite)|*.sqlite|","打开","./");
	
	if(!fileName){
		return;
	}
	
	setCurFilePath(fileName);
	
	var str = ..fsys.codepage.load(fileName,"UTF-16LE")
	
	var row_count = 0;
	var col_count = 0;
	for i in ..string.lines(str,'\r\n','\t'){
		row_count += 1;
		if(row_count == reference_row){
			for(n=1;#i;1){
				if(..string.trim(i[n]) == ""){
					break;
				}
				col_count += 1;
			}
		}
	}
	
	var t = {};
	t.fields = {};
	t.fields = ..table.append(t.fields, generateColFields(col_count));
	var temp_cur_line = 0;
	for i in ..string.lines(str,'\r\n','\t', col_count){
    	var col_values = {};
    	var isEmptyLine = false;
		
    	temp_cur_line += 1;
    	
    	for (j=1; col_count; 1) {
    	    if(#i == 0){
    	    	isEmptyLine = true;
    	    	break;
    	    }
    	    
    	    if(i[j] == null){
    	    	i[j] = "";
    	    }
    	    
    	    if(j == col_count){//去除每一行末尾多余的\t
    	    	i[j] = ..string.replace(i[j], '\t',"");
    	    }
    	    
			col_values[t.fields[j]] = i[j];
    	}
		if (!isEmptyLine) {
			..table.push(t, col_values);
		}
	}
	return t;
}

var writeTxtFile = function(file_path, vlist_data){
	var file = ..io.open(file_path,"w");
	setCurFilePath(file_path);
	for(i=1;#vlist_data;1){
		var arrLine = {};
		for(j=1;#vlist_data.fields;1){
			..table.push(arrLine, vlist_data[i][vlist_data.fields[j]]);
		}
		
		str = ..string.join(arrLine, '\t', 1, #vlist_data.fields);
		str = str ++ '\t';// 每行最后一列之后添加一个\t
		
		if(i != #vlist_data){
			file.write(str,'\n');
		}
		else {
			file.write(str);
		}
	}
	file.close();
	
	var str2 = ..fsys.codepage.load(file_path,"UTF-8")
	..fsys.codepage.save(file_path,str2,"UTF-16LE")
}

saveTxtTableData = function(vlist_data){
	var curFilePath = getCurFilePath();
	if(!curFilePath){
		return;
	}
	writeTxtFile(curFilePath, vlist_data);
}

saveAsTxtTableData = function(vlist_data){
	var newFilePath = ..fsys.dlg.saveOp("txt file(*.txt)|*.txt|db file(*.db)|*.db|sqlite file(*.sqlite)|*.sqlite|","保存","./");
	if(!newFilePath){
		return;
	}
	setCurFilePath(newFilePath);
	writeTxtFile(newFilePath, vlist_data);
}