import win.ui;
import win.inputBox;
import win.ui.menu;
import win.util.tray;
import win.ui.atom;
import win.timer;
import console;
import win.timer;
import fsys;
import web.multipartFormData;
import inet.http;
import fsys.config;
/*DSG{{*/
mainForm = win.form(text="数据采集";right=799;bottom=399;border="dialog frame";max=false)
mainForm.add(
add={cls="button";text="新建";left=27;top=130;right=98;bottom=154;font=LOGFONT(h=-14;name='微软雅黑');z=6};
backupPath={cls="edit";left=107;top=58;right=337;bottom=84;edge=1;font=LOGFONT(h=-14;name='微软雅黑');z=11};
backupPathLabel={cls="static";text="备份文件夹";left=31;top=60;right=118;bottom=81;font=LOGFONT(h=-14;name='微软雅黑');notify=1;transparent=1;z=10};
groupbox={cls="groupbox";text="采集内容";left=18;top=110;right=346;bottom=390;edge=1;z=5};
groupbox2={cls="groupbox";text="采集日志";left=353;top=7;right=792;bottom=390;ah=1;aw=1;db=1;dr=1;edge=1;z=1};
groupbox3={cls="groupbox";text="基础配置";left=18;top=7;right=346;bottom=102;edge=1;z=2};
listbox={cls="listbox";left=360;top=24;right=785;bottom=387;edge=1;hscroll=1;items={};vscroll=1;z=12};
listview={cls="listview";left=27;top=161;right=337;bottom=384;edge=1;fullRow=1;items={};msel=false;z=7};
serverUrl={cls="edit";left=107;top=25;right=337;bottom=51;edge=1;font=LOGFONT(h=-14;name='微软雅黑');z=4};
serverUrlLabel={cls="static";text="服务器地址";left=31;top=28;right=118;bottom=49;font=LOGFONT(h=-14;name='微软雅黑');notify=1;transparent=1;z=3};
start={cls="button";text="开始采集";left=266;top=130;right=337;bottom=154;font=LOGFONT(h=-14;name='微软雅黑');z=8};
statusLabel={cls="static";left=143;top=130;right=221;bottom=154;align="center";color=32768;font=LOGFONT(h=-16;name='微软雅黑';weight=700);transparent=1;z=9}
)
/*}}*/

/*
inputbox = win.inputBox(mainForm.hwnd)
inputbox.text = "输入框标题"
inputbox.info.text = "请在下面输入您的名字"
inputbox.input.text = "在这里输入您的名字"
name = inputbox.doModal();
	
if(name)
	win.msgbox("您的名字是："+name )
else 
	win.msgbox("你拒绝输入名字")
*/
mainForm.listview.adjust = function(cx,cy){
    mainForm.listview.fillParent(/*列序号*/);
} 
 
mainForm.listview.insertColumn("设备编号",100,,0x0/*_LVCFMT_LEFT*/) 
mainForm.listview.insertColumn("文件夹地址",100,,0x0/*_LVCFMT_LEFT*/) //第二列开始可以使用_LVCFMT_CENTER居中

config = fsys.config("/config/")
mainForm.bindConfig( config.winform,{
	serverUrl = "text";
	backupPath = "text";
	listview = "items";	
} );
if(mainForm.serverUrl.text==""){
	mainForm.serverUrl.text='http://127.0.0.1:8000'
}
if(mainForm.backupPath.text==""){
	mainForm.backupPath.text="D:\Daq\Backup"
}
/*
if(table.count(mainForm.listview.items)==0){
	mainForm.listview.items = {{"JN102";"D:\tmp"}};
}
*/

var isWorking = false;
var atom,hwnd/*冲突窗口的句柄,该函数会自动激活此窗口*/ = mainForm.atom("33D501DF-BFC2-4283-8BBE-AF17AADB1C27");
if(!atom){
	/*为窗口设置原子值可以避免一个程序重复运行多个实例*/
	win.quitMessage();	return;
}


mainForm.onMinimize = function(lParam){
	var tray = win.util.tray(mainForm) //创建托盘图标 
	tray.tip = "数据采集";
	tray.pop("数据采集已最小化运行" )
	mainForm.show(false); //隐藏窗口
	return true;//阻击默认消息传递,取消最小化过程
}
mainForm.wndproc = {
	[0xACCF/*_WM_TRAYMESSAGE*/ ] = function(hwnd,message,wParam,lParam){
		if( lParam = 0x205/*_WM_RBUTTONUP*/ ){ 
	    	var pt = ::POINT();
			::User32.GetCursorPos(pt);
	    	//弹出托盘菜单以前,一定要前置主窗口中,不然不点击菜单不会消失
	    	win.setForeground(mainForm.hwnd)
	    	mainForm.popmenu.popup(pt.x,pt.y,true )
	    }elseif(lParam = 0x203/*_WM_LBUTTONDBLCLK*/){
	    	mainForm.show()
	    }
	    
	   
	}
}

mainForm.popmenu = win.ui.popmenu(mainForm);//创建弹出菜单
mainForm.popmenu.add('&打开',function(id){
	//在下面输入菜单响应代码
	mainForm.show()
});
mainForm.popmenu.add();//分隔线
mainForm.popmenu.add('&退出',function(id){ mainForm.close() })

mainForm.start.oncommand = function(id,event){
	toggleWorking();
	
}
var timer = win.timer( mainForm );
timer.setInterval(5000)
function toggleWorking(){
	if(isWorking == false){
		mainForm.serverUrl.disabled = true
		mainForm.backupPath.disabled = true
		mainForm.add.disabled = true
		mainForm.statusLabel.text="采集中..."
		mainForm.start.text="停止采集"
		isWorking = true
		timer.enable()
	}else{
		mainForm.serverUrl.disabled = false
		mainForm.backupPath.disabled = false
		mainForm.add.disabled = false
		mainForm.statusLabel.text=""
		mainForm.start.text="开始采集"
		isWorking=false
		timer.disable()
	}
}
if(mainForm.serverUrl && table.count(mainForm.listview.items)>0){
toggleWorking();
}

timer.onTimer = function(hwnd,msg,id,tick){
	for itemIndex in mainForm.listview.each(){
		var number, dir = mainForm.listview.items[itemIndex][1], mainForm.listview.items[itemIndex][2]
		//批量处理文件
		fsys.enum( dir, //指定要遍历的目录
			"*.*", //指定查询文件名,支持windows掩码
			function(dir,filename,fullpath,findData){ //指定触发器
				if(filename){ 
					mainForm.listbox.add(tostring(time.now(),"%Y-%m-%d %H:%M:%S","chs") + ":新文件-"+filename, -1)
					mainForm.listbox.add("正在上传并解析...")
					var webData = web.multipartFormData();
					webData.add("equip_num", number)
					webData.add("file", "@"+fullpath)
					webData.contentHeader()
					var http = inet.http(); 
					/*
http.beginRequest( mainForm.serverUrl.text + "/api/em/daq/", "POST" );
					//小数据一次性上传
					http.beginSendData(webData.size());
					http.writeData(webData.readAll())
					http.endSendData();
*/
					var html,err,errCode = http.post( mainForm.serverUrl.text + "/api/em/daq/",webData.readAll(),webData.contentHeader());
					if(html){
						var res = web.json.parse(html)
						if(res['code']==200){
							import fsys
							var theDir = fsys.createDir(mainForm.backupPath.text+"\\"+number, false)
							fsys.move(fullpath, theDir) //移动到备份文件库
							mainForm.listbox.add("采集成功!")
						}else{
							mainForm.listbox.add("失败:"+res['msg'])
						}
					}else{
						mainForm.listbox.add("失败:请求错误")
					}
					


				}
			}, false
		  ); 
	}
}

mainForm.listview.onnotify = function(id,code,ptr){ 
    
    select(code) {
    	case 0xFFFFFFFB/*_NM_RCLICK*/  {
    		var x,y = win.getCursorPos();
    		var popmenu = win.ui.popmenu(mainForm);//创建弹出菜单
			popmenu.add('删除',function(id){
				//在下面输入菜单响应代码
				mainForm.listview.delItem( mainForm.listview.selIndex )
			});
    		popmenu.popup(x,y,true);//弹出菜单
    	}
    }
    
}

mainForm.add.oncommand = function(id,event){
	
var frmChild = mainForm.loadForm("\\dlg\\add.aardio");
frmChild.doModal();

}





mainForm.show();
return win.loopMessage();