<?xml version="1.0" encoding="ISO-8859-1"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:cti_control="CTI.technique.controls.*" xmlns="*" 
	creationComplete="creationComplete()" resize="resize()"
	styleName="CTIView"
	paddingTop="0" paddingBottom="0" paddingRight="0" paddingLeft="0"
	creationPolicy="all">

   <mx:Script>
        <![CDATA[   
		
			import mx.controls.Alert;
			import mx.controls.Menu;
			import mx.collections.ArrayCollection;
			import flash.net.URLVariables;
			import mx.events.CloseEvent
			import flash.events.*;
			import mx.events.*;
			import mx.collections.Sort;
			import mx.collections.SortField;

			import CTI.technique.CTI_Utilitaires;
			import CTI.technique.CTI_ArrayCollection;

			public var buildComplete:Boolean = false;
			
			[Bindable]
			public var title:String = "Synchronisation des données";
			
			[Bindable]
			public var hasHelp:Boolean = false;
			
			[Bindable]
			public var exe_list_provider:XMLList = new XMLList();

			[Bindable]
			public var jobsHistory_list:CTI_ArrayCollection = new CTI_ArrayCollection();
			[Bindable]
			public var jobsHistory_length:Number;

			public function creationComplete():void {
				if (application.currentHelpFile != "") {
					hasHelp = true;
				}
				
				if (application.name == "CTI") {
				}
				if (application.name == "CTI_1") {
					GUI_titleBar.height = 0
				}
				
				tabNavigator_GUI.selectedIndex = 0

				getData()
			}
			
			public function eventDispatcher(eventCode:String):Boolean {
				return true
			}
			
			public function getData():void {
			
				var urlVariables:URLVariables 

				CTI_Utilitaires.startRequestHTTP("modules/rh/php/getSynchronize.php",urlVariables,exec,true,this.parentApplication)

				function exec(resultatService:XML):void {
				
					var initDft:Boolean = false;
					
					exe_list_provider = resultatService.EXE
					
					jobsHistory_length = resultatService.JOB.length()
					jobsHistory_list.loadXMLList(resultatService.JOB)
					
					var _sort:Sort = new Sort()
					_sort.fields = [new SortField("start", true, true, false)];
					jobsHistory_list.sort = _sort;
					jobsHistory_list.refresh()

					setTimeout(setBuildComplete, 1000);
				}
			}
			
			private function setBuildComplete():void {
				buildComplete = true;
			}

			private function resize():void {}

			private function execSynchronize(exe:XML):void {
			
				CTI_Utilitaires.question_yesNo("Synchronisation", "Confirmez vous la " + exe.@confirm + " ? ", responseFunction)
			
				function responseFunction(event:CloseEvent):void {
				
					if (event.detail != Alert.YES) {
						return;
					}
				
					var urlVariables:URLVariables = new URLVariables() 
					urlVariables.exe_path = exe.@exe_path;
					
					CTI_Utilitaires.startRequestHTTP("modules/rh/php/execSynchronize.php",urlVariables,exec,true,this.parentApplication)

					function exec(resultatService:XML):void {
						if (resultatService.@returnCode.toString() == "KO") {
							CTI_Utilitaires.alert_error("Synchronisation", resultatService.@returnTexte.toString());
						}
						else {
							CTI_Utilitaires.alert_info("Synchronisation", "Synchronisation démarrée en batch sur le serveur");
							getData();
						}
					}
				}
			}

			private function displayLog():void {
				var job:Object;
				
				if (historique_UI.selectedIndex < 0) {
					return
				}
				job = historique_UI.selectedItem;

				if (job.logFile.toString() == "") {
					return;
				}
			
				var urlVariables:URLVariables = new URLVariables();
		        urlVariables.longfile = job.logFile.toString();
				CTI_Utilitaires.openURL("modules/outils/php/getLogFile.php",urlVariables,"iCTI_logFile")
			}
	
        ]]>
    </mx:Script>
	<mx:VBox width="100%" height="100%">
		<cti_control:CTIViewTitle id="GUI_titleBar" label="{title}" width="100%" height="25" />
		<mx:Canvas width="100%" height="28" >	
			<mx:Canvas width="100%" height="100%" visible="{tabNavigator_GUI.selectedIndex != 1}" >
				<mx:HBox width="100%">
					<mx:Spacer />
                    <mx:Button height="28" width="28" toolTip="Réafficher" icon="@Embed('../../base/swf/CTI/images/24/view-refresh.png')" labelPlacement="right" click="getData()" />
					<mx:Spacer />
					<mx:Button height="28" width="28" toolTip="Aide" icon="@Embed('../../base/swf/CTI/images/24/help.png')" labelPlacement="right" click="application.displayHelp()" visible="{hasHelp}" />
					<mx:Button height="28" width="28" toolTip="A propos" icon="@Embed('../../base/swf/CTI/images/24/information.png')" labelPlacement="right" click="application.showApropos_view()" />
				</mx:HBox>
			</mx:Canvas>
			<mx:Canvas width="100%" height="100%"  visible="{tabNavigator_GUI.selectedIndex == 1}" >
				<mx:HBox width="100%">
					<mx:Button height="28" width="28" toolTip="Réafficher" icon="@Embed('../../base/swf/CTI/images/24/view-refresh.png')" labelPlacement="right" click="getData()" />
					<mx:Button height="28" label="Afficher le fichier log" toolTip="Afficher le fichier log" icon="@Embed('../../base/swf/CTI/images/24/document-open.png')" labelPlacement="right" click="displayLog()" />
					<mx:Spacer />
					<mx:Button height="28" width="28" toolTip="Aide" icon="@Embed('../../base/swf/CTI/images/24/help.png')" labelPlacement="right" click="application.displayHelp()" visible="{hasHelp}" />
					<mx:Button height="28" width="28" toolTip="A propos" icon="@Embed('../../base/swf/CTI/images/24/information.png')" labelPlacement="right" click="application.showApropos_view()" />
				</mx:HBox>
			</mx:Canvas>
		</mx:Canvas>
		<mx:TabNavigator id="tabNavigator_GUI" width="100%" height="100%" >
			<mx:VBox width="100%" height="100%" label="Synchronisation des données">
				<mx:Repeater id="exe_repeater" dataProvider="{exe_list_provider}">
					<mx:Button label="{exe_repeater.currentItem.@button}" icon="@Embed('../../base/swf/CTI/images/24/run.png')" labelPlacement="right" click="execSynchronize(event.currentTarget.getRepeaterItem())"/>
                    <mx:Label width="100%" text="{exe_repeater.currentItem.@last_status}" />
    			</mx:Repeater>
			</mx:VBox>
			<mx:VBox width="100%" height="100%" label="Historique des travaux">
				<mx:DataGrid id="historique_UI" 
					rowCount="{jobsHistory_length + 1}" height="100%" width="100%" rowHeight="24"
					wordWrap="false" dataProvider="{jobsHistory_list}">
					<mx:columns>
						<mx:DataGridColumn dataField="name" headerText="Nom" width="200"/>
						<mx:DataGridColumn dataField="title" headerText="Titre" width="250"  editable="false"/>
						<mx:DataGridColumn dataField="start" headerText="Démarré" width="120"  editable="false"/>
						<mx:DataGridColumn dataField="end" headerText="Terminé" width="120"  editable="false"/>
						<mx:DataGridColumn dataField="endStatus" headerText="Statut" width="60"  editable="false"/>
						<mx:DataGridColumn dataField="duration" headerText="Durée" width="60"  editable="false"/>
					</mx:columns>
				</mx:DataGrid>
			</mx:VBox>
		</mx:TabNavigator>
	</mx:VBox>
</mx:Application>