<?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;
			public var provider:String = '';
			
			[Bindable]
			private var providerConnected:Boolean = false;
			
			[Bindable]
			private var extract_data:Boolean = false;

			[Bindable]
			public var title:String = "Synchronisation des données de comptabilité";
			
			[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()
				
				extract_data_GUI.selected = false;
			}
			
			
			
			public function eventDispatcher(eventCode:String):Boolean {
				return true
			}
			
			public function getData():void {
			
				var urlVariables:URLVariables 

				CTI_Utilitaires.startRequestHTTP("modules/compta/php/COMPTA_getSynchronize.php",urlVariables,exec,true,this.parentApplication)
							
				function exec(resultatService:XML):void {
				
					var initDft:Boolean = false;
					
					exe_list_provider = resultatService.EXE
					provider = resultatService.PROVIDER.@name.toString();
					
					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);
										
					checkConnexion();
				}
				
			}
			
			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;
					urlVariables.extract_data = extract_data;
					
					CTI_Utilitaires.startRequestHTTP("modules/compta/php/COMPTA_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")
			
			}
			

			public function checkConnexion():void {
                
                var urlVariables:URLVariables = new URLVariables();
				
				urlVariables.provider = provider;
                
                CTI_Utilitaires.startRequestHTTP("modules/outils/php/httpService_database_checkConnexion.php", urlVariables, exec, true, this.parentApplication);
                
                function exec(resultatService:XML):void {
                    var xmlReturn:XML;
                    xmlReturn = resultatService;
                    var returnCode:String = xmlReturn.@returnCode.toString();
                    var returnTexte:String = xmlReturn.@returnTexte.toString();
                    
                    if (returnCode == "OK") {
                        providerConnected = true;
						//CTI_Utilitaires.alert_info("Test connexion " + provider, "Les données pourront être extraites avant synchronisation");
                    }
                    else {
                        providerConnected = false;
						//CTI_Utilitaires.alert_error("Test connexion " + provider, "Les données ne pourront pas être extraites avant synchronisation");
                    }
                }
            }
			
			public function change_extract():void {
				if (extract_data == true) {
					extract_data = false;
				}
				else {
					extract_data = true;
				}
            }
	
        ]]>
    </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:Button height="28" width="28" toolTip="Réafficher" icon="@Embed('../../base/swf/CTI/images/refresh24.png')" labelPlacement="right" click="getData()" />
					<mx:Spacer />
					<mx:Button height="28" width="28" toolTip="Aide" icon="@Embed('../../base/swf/CTI/images/help24.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/refresh24.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/viewlog24.gif')" labelPlacement="right" click="displayLog()" />
					<mx:Spacer />
					<mx:Button height="28" width="28" toolTip="Aide" icon="@Embed('../../base/swf/CTI/images/help24.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%" backgroundColor="0xffffff" >


			<mx:VBox width="100%" height="100%" label="Synchronisation des données">
				

				<mx:Repeater id="exe_repeater" dataProvider="{exe_list_provider}">
					<mx:Spacer height="5" />
					<mx:Panel width="100%" height="100%" title="{exe_repeater.currentItem.@title}" >
						<mx:Spacer height="3" />
						<mx:Label width="100%" text="{exe_repeater.currentItem.@last_status}" />	
						<mx:Spacer height="3" />
						<mx:CheckBox id="extract_data_GUI" label="Extraction des données prestataires avant synchronisation" selected="{extract_data}" click="change_extract()" visible="{providerConnected}"/>
						<mx:Button height="28" width="100%" label="{exe_repeater.currentItem.@button}" icon="@Embed('../../base/swf/CTI/images/run24.png')" 
							labelPlacement="right" click="execSynchronize(event.currentTarget.getRepeaterItem())"/>
						<mx:Spacer height="3" />
						<mx:TextArea width="100%" height="100%" htmlText="{exe_repeater.currentItem.@comment}" editable="false" selectable="false" borderStyle="none"/>
					</mx:Panel>
				</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>