<?php

include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");
include_once (PATH_TO_ROOT."webuserspool/UsersPoolinit.php");
include_once(PATH_TO_ROOT."_class/class.Extjs_tree.php");
include_once(PATH_TO_ROOT."_class/class.Extjs_grid.php");

$UserSession->CreateTabCheck();

	$tabId = $_POST['uniqueTabId'];

	$grid = new MakeGrid($tabId);
	$grid->setMainTable(WUP_MAILSTATISTIC,DB_WARPIT_USERSPOOL);
	$gridName = $grid->getName();
	$grid->selectSQL = "SELECT a.id,mail_set,mail_address,date_queue,date_schedule,mail_status,idLab,numSend FROM ".WUP_MAILSTATISTIC." a INNER JOIN ".WUP_SENDMAILSET." c ON a.mail_set = c.id INNER JOIN ".WUP_EMAIL." b ON	c.id_template = b.id";
	
	
	$grid->addColumn('id');
	$grid->addColumn('numSend');
	$grid->addColumn('mail_set','Set')->setProperty('groupRenderer',"function(val,nic,rec){return rec.data.idLab + ' ' + rec.data.date_queue + ' ('+rec.data.numSend +')' ; }");
	$grid->addColumn('mail_address','Email');
	$grid->addColumn('idLab','Template');
	$grid->addColumn('date_queue','Queue time')->width(150)->renderer("function renderDate(value){
    return Date.parseDate(value,'Y-m-d h:i:s').format('d.m.Y H:i:s');
}");
	$grid->addColumn('date_schedule','Schedule time')->width(150)->renderer("function renderDate(value){
	if(value == '0000-00-00 00:00:00') return 'No schedule';
    return Date.parseDate(value,'Y-m-d h:i:s').format('d.m.Y H:i:s');
}");;
	$grid->addColumn('mail_status','Status')->width(40)-> renderer("function(val){
			var ret =  '<div class=\"';
			if(val == 1) ret+= 'RMicon_forward_mail';
			else if(val == 3) ret+= 'RMicon_success_email';
			else if(val == 5) ret+= 'RMicon_failed_email';
			else if(val == 2) ret+= 'RMicon_wait_email';
			else if(val == 4) ret+= 'RMicon_canceled_email';
			
				return ret + ' ux-cell-action\">&#160;</div>'
	}");
	
	
	$grid->setTitle('Send emails')
		->setGrouping('mail_set',"{hideGroupedColumn:true,showGroupName:false}")
		->setPaging()
		->setCheckBoxSelectionModel()
		->setSort('mail_set','desc')
		->setAutoExpand('mail_address')
		->setRemoteSort()
		->setFiltering(true)
		//->setSearchField()
		->addConfig('iconCls',"'RMicon_send_email'")
		->addTbarItem("{iconCls:'RMicon_canceled_email',text:'Cancel',
			handler:function(){
				var sm = $gridName.getSelectionModel();
				if(sm.hasSelection())
				{
				
				Ext.Msg.show({ title:'Remove record',buttons: Ext.MessageBox.YESNO,msg:'Are you sure?',
				fn: function(btn){if(btn == 'yes'){
						var selId = [];
	            		Ext.each( sm.getSelections() , function(rec){
	            			selId.push(rec.id);
	            		} );
				
					Ext.Ajax.request({url:'webuserspool/_ajax/ajax.showgroup.php',params:{action:'cancelMail',ids:selId.join()},
							success: function(resp){ $gridName.getStore().reload();  }
					});
					}
					}});
				}
			}
		}")
		;
	

	echo $grid->createGrid();
	
	
	
	echo "$gridName.on('show',function(gr){ 
			gr.getStore().reload();
			$gridName.autoRefreshId = setInterval(\"Ext.getCmp('$gridName').getStore().reload()\",30000);
		});
		$gridName.on('hide',function(gr){ 
			clearInterval($gridName.autoRefreshId);
		});
		$gridName.on('removed',function(gr){ 
			clearInterval($gridName.autoRefreshId);
		});
		
		 ";
	
	echo "mainTabPanel.add($gridName).show();"
?>
	