<?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 flash.external.*;
			import flash.utils.ByteArray;
			
			import CTI.datagrid.CTI_GridChart;
			import CTI.datagrid.CTI_DataGridColumn;
			
			import CTI.technique.CTI_Utilitaires;
			import CTI.technique.CTI_Base64;
			
			
			public var buildComplete:Boolean = false;
			
			[Bindable]
			public var title:String = "Consolidation";
			
			[Bindable]
			public var hasHelp:Boolean = false;
			
			[Bindable]
			public var imports_list:ArrayCollection = new ArrayCollection();
			
			[Bindable]
			public var chiffrier_list:ArrayCollection = new ArrayCollection();
			
			[Bindable]
			public var jobsHistory_list:XMLList;
			[Bindable]
			public var jobsHistory_length:Number;
			
			[Bindable]
			private var download_menu_GUI:Menu = new Menu();
			[Bindable]
			private var download_menu_items:ArrayCollection = new ArrayCollection();
			private var download_menu_current_data_type:String;
			
			
			
			[Bindable]
			private var importEnabled:Boolean = false;
			
			[Bindable]
			private var importStarted:Boolean = false;
			[Bindable]
			private var oneImportStarted:Boolean = false;

			[Bindable]
			private var validationStatus:String;
			
			[Bindable]
			private var sendFilesStatus:String;
			
			[Bindable]
			private var controleStatus:String;
			
			private var firstGetData:Boolean = true
			
			private var importResult:String;
			private var import_id:Number = 0;
			
			
			private var setContents_currentFileIndex:int
			private var setContents_interval:Number;
			

			[Bindable]
			private var selected_import_item:Object;

			[Bindable]
			private var uploadFilesResult:XML = new XML();
			
			private var chiffrier_list_GUI:CTI_GridChart
			
			private var chiffrier_datagrid_node:XML = new XML(
					<DATAGRID title="Controle chiffrier" rowHeight="20" headerHeight="20">
						<ROWSTYLEx name="backgroundColor" value="0xFFFF99" condition="ROW.rsa_det != ROW.rsa_conso || ROW.ca_det != ROW.ca_conso" />
						<COLUMN dataField="finess" headerText="Finess" visible="true" type="Char" width="60" >
						</COLUMN>
						<COLUMN dataField="finess_texte" headerText="Etablissement" visible="true" type="Char" minWidth="150" largeSeparator="true" >
						</COLUMN>
						<COLUMN dataField="min_det" headerText="Début (E)" visible="true" width="80" type="Date" 
							inputFormat="AAAAMMJJ" outputFormat="MMM YYYY" >
						</COLUMN>		
						<COLUMN dataField="max_det" headerText="Fin (E)" visible="true" width="80" type="Date" 
							inputFormat="AAAAMMJJ" outputFormat="MMM YYYY" >
						</COLUMN>
						<COLUMN dataField="rsa_det" headerText="Séjours (E)" visible="true" width="80" type="Number" 
							outputFormat="#" >
						</COLUMN>
						<COLUMN dataField="ca_det" headerText="C.A. (E)" visible="true" width="100" type="Number" 
							outputFormat="#E" largeSeparator="true" >
						</COLUMN>
						<COLUMN dataField="min_conso" headerText="Début (C)" visible="true" width="80" type="Date" 
							inputFormat="AAAAMMJJ" outputFormat="MMM YYYY"  >
						</COLUMN>		
						<COLUMN dataField="max_conso" headerText="Fin (C)" visible="true" width="80" type="Date" 
							inputFormat="AAAAMMJJ" outputFormat="MMM YYYY" >
						</COLUMN>
						<COLUMN dataField="rsa_conso" headerText="Séjours (C)" visible="true" width="80" type="Number" 
							outputFormat="#" >
						</COLUMN>
						<COLUMN dataField="ca_conso" headerText="C.A. (C)" visible="true" width="100" type="Number" 
							outputFormat="#E" >
						</COLUMN>
					</DATAGRID>
					)


			
			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

				firstGetData = true
				
				// Pop up menu
				var item:Object = new Object()
				item.label = "RSS"
				item.data = "RSS"
				download_menu_items.addItem(item)
				item = new Object()
				item.label = "RSF"
				item.data = "RSF"
				download_menu_items.addItem(item)
				item = new Object()
				item.label = "Médecins"
				item.data = "MED"
				download_menu_items.addItem(item)
				item = new Object()
				item.label = "RSS - Médecins"
				item.data = "RSSMED"
				download_menu_items.addItem(item)
				item = new Object()
				item.label = "Noms RSS"
				item.data = "RSSNOM"
				download_menu_items.addItem(item)
				item = new Object()
				item.label = "Actes - Médecins"
				item.data = "ACTMED"
				download_menu_items.addItem(item)
				
				download_menu_GUI.dataProvider = download_menu_items;
				download_menu_GUI.selectedIndex = 0;
				download_menu_current_data_type = "RSS";		
				
				download_menu_GUI.addEventListener("itemClick", download_menu_itemClick);
				
				download_popup_GUI.label = "Télécharger fichier";
				
				
				
				create_chiffrier_datagrid()
				

				getData()
			}
			
			private function create_chiffrier_datagrid():void {
			
			
				chiffrier_list_GUI = new CTI_GridChart()
				var columns:Array = new Array();
				chiffrier_list_GUI.title = chiffrier_datagrid_node.@title.toString()
				chiffrier_list_GUI.parentView = this
				chiffrier_list_GUI.percentHeight = 100
				chiffrier_list_GUI.percentWidth = 100
				chiffrier_list_GUI.rowHeight = Number(chiffrier_datagrid_node.@rowHeight.toString())
				chiffrier_list_GUI.headerHeight = Number(chiffrier_datagrid_node.@headerHeight.toString())
				chiffrier_list_GUI.dataProvider = chiffrier_list
				
				for each (var columnNode:XML in chiffrier_datagrid_node.COLUMN) {
					columns.push(new CTI_DataGridColumn(columnNode));
				}

				chiffrier_list_GUI.columns = columns
				
				for each (var styleNode:XML in chiffrier_datagrid_node.ROWSTYLE) {
					var cellStyle:Object = new Object()
					cellStyle.name = styleNode.@name.toString()
					cellStyle.value = styleNode.@value.toString()
					cellStyle.condition = styleNode.@condition.toString()
					chiffrier_list_GUI.rowStyles.push(cellStyle)
				}
				
				chiffrier_box_GUI.addChild(chiffrier_list_GUI)

				
			
			}
			

			private function download_menu_itemClick(event:MenuEvent):void {

				download_popup_GUI.label = "Télécharger " + event.item.label
				download_menu_current_data_type = event.item.data;	
				download_popup_GUI.close();
				download_menu_GUI.selectedIndex = event.index;
				
				if (tabNavigator_GUI.selectedIndex == 0 ) {
					if (imports_list_GUI.selectedItem != null) {
						downloadFile();
					}
				}
				
				if (tabNavigator_GUI.selectedIndex == 1) {
					downloadFile();
				}
				
			}
			
			public function eventDispatcher(eventCode:String):Boolean {
				return true
			}
			
			public function getData():void {
			
				var urlVariables:URLVariables 
				
				imports_list.removeAll();
				chiffrier_list.removeAll();
				
				CTI_Utilitaires.startRequestHTTP("modules/pmsi/php/PMSI_getConsolidation.php",urlVariables,exec,true,this.parentApplication)
							
				function exec(resultatService:XML):void {
				
					var initDft:Boolean = false;
					
					jobsHistory_list = resultatService.JOB
					jobsHistory_length = resultatService.JOB.length()
					
					for each (var importNode:XML in resultatService.IMPORT) {

						var import_item:Object = new Object();
						import_item.oid = Number(importNode.@oid.toString())
						import_item.texte = importNode.@texte.toString()
						import_item.date_import = importNode.@date_import.toString()
						import_item.etat = importNode.@etat.toString()
						import_item.etat_en_cours = importNode.@etat_en_cours.toString()
						import_item.traitement_a_faire = importNode.@traitement_a_faire.toString()
						import_item.finess = importNode.@finess.toString()
						import_item.finess_texte = importNode.@finess_texte.toString()
						import_item.date_debut = importNode.@date_debut.toString()
						import_item.date_fin = importNode.@date_fin.toString()
						import_item.nb_rss = Number(importNode.@nb_rss.toString())
						import_item.nb_rum = Number(importNode.@nb_rum.toString())
						import_item.nb_rsf = Number(importNode.@nb_rsf.toString())
						import_item.nb_erreurs = Number(importNode.@nb_erreurs.toString())
						import_item.nb_avertissements = Number(importNode.@nb_avertissements.toString())
						import_item.dbname = importNode.@dbname.toString()
						import_item.dbuser = importNode.@dbuser.toString()
						import_item.dbpassword = importNode.@dbpassword.toString()
						
						import_item.etat_texte = "";
						if (import_item.etat == "I") {
							import_item.etat_texte = "Importé";
						}
						if (import_item.etat == "C") {
							import_item.etat_texte = "Controlé";
						}
						if (import_item.etat == "G") {
							import_item.etat_texte = "Généré";
						}
						
						if (import_item.traitement_a_faire == "") {
							if (import_item.etat_en_cours == "I") {
								import_item.etat_texte = import_item.etat_texte + " C";
							}
							if (import_item.etat_en_cours == "C") {
								import_item.etat_texte = import_item.etat_texte + " C";
							}
							if (import_item.etat_en_cours == "G") {
								import_item.etat_texte = import_item.etat_texte + " C";
							}
						}
						else {
							if (import_item.traitement_a_faire == "C") {
								import_item.etat_texte = import_item.etat_texte + " A";
							}
							if (import_item.traitement_a_faire == "G") {
								import_item.etat_texte = import_item.etat_texte + " A";
							}
						}
						
						imports_list.addItem(import_item);
					}
					
					for each (var chiffrierNode:XML in resultatService.CHIFFRIER) {

						var chiffrier_item:Object = new Object();
						chiffrier_item.finess = chiffrierNode.@finess.toString()
						chiffrier_item.finess_texte = chiffrierNode.@finess_texte.toString()
						chiffrier_item.min_det = chiffrierNode.@min_det.toString()
						chiffrier_item.max_det = chiffrierNode.@max_det.toString()
						chiffrier_item.rsa_det = Number(chiffrierNode.@rsa_det.toString())
						chiffrier_item.ca_det = Number(chiffrierNode.@ca_det.toString())
						chiffrier_item.min_conso = chiffrierNode.@min_conso.toString()
						chiffrier_item.max_conso = chiffrierNode.@max_conso.toString()
						chiffrier_item.rsa_conso = Number(chiffrierNode.@rsa_conso.toString())
						chiffrier_item.ca_conso = Number(chiffrierNode.@ca_conso.toString())
						
						chiffrier_item.rowType = 'd';
						
						
						chiffrier_list.addItem(chiffrier_item);
					}
					
					chiffrier_list_GUI.dataProvider = chiffrier_list
				
					firstGetData = false
										
					setTimeout(setBuildComplete, 1000);
										
				}
			
			
			}
			
			private function setBuildComplete():void {

				buildComplete = true;
			}
			
			
			
					
			private function resize():void {
				
				
			}
		
			
						
			
			public function displayRapport():void {

				if (imports_list_GUI.selectedItem != null) {
					var urlRequest:URLRequest = new URLRequest();
					urlRequest.url = "modules/pmsi/php/PMSI_getImportRapport"
					var urlVariables:URLVariables = new URLVariables()
					urlVariables.import_id = imports_list_GUI.selectedItem.oid
					urlVariables.dbname = imports_list_GUI.selectedItem.dbname
					urlVariables.dbuser = imports_list_GUI.selectedItem.dbuser
					urlVariables.dbpassword = imports_list_GUI.selectedItem.dbpassword
					urlRequest.data = urlVariables
					urlRequest.method = "POST";
					
					navigateToURL(urlRequest, "importRapport")
					return;
				}

				Alert.show("Aucun import à afficher")
			}
			
			
			
			
						
			
			public function downloadFile():void {

				if (imports_list_GUI.selectedItem != null) {
					var urlRequest:URLRequest = new URLRequest();
					urlRequest.url = "modules/pmsi/php/PMSI_getFile.php"
					var urlVariables:URLVariables = new URLVariables()
					urlVariables.import_id = imports_list_GUI.selectedItem.oid
					urlVariables.data_type = download_menu_current_data_type
					urlVariables.dbname = imports_list_GUI.selectedItem.dbname
					urlVariables.dbuser = imports_list_GUI.selectedItem.dbuser
					urlVariables.dbpassword = imports_list_GUI.selectedItem.dbpassword
					urlRequest.data = urlVariables
					urlRequest.method = "POST";
					
					navigateToURL(urlRequest, "getFile")
					
					return;
				}

				Alert.show("Aucun import sélectionné")

			}
			
			
			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 &amp;&amp; tabNavigator_GUI.selectedIndex != 2}" >
				<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" 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" width="140" label="Dernier rapport" toolTip="Afficher le rapport" icon="@Embed('../../base/swf/CTI/images/24/zoom-in.png')" labelPlacement="right" click="displayRapport()" enabled="{imports_list_GUI.selectedIndex >= 0}" />
					<mx:PopUpButton id="download_popup_GUI" enabled="{imports_list_GUI.selectedIndex >= 0}"
						height="28" width="200" toolTip="Télécharger un fichier" 
						popUp="{download_menu_GUI}" 
						icon="@Embed('../../base/swf/CTI/images/24/document-save.png')" labelPlacement="right" click="downloadFile()" />
					<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 == 2}" >
				<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 id="chiffrier_box_GUI" width="100%" height="100%" label="Controle détail / consolidé" backgroundColor="0xF0F0F0">
			
				
			
				
			
			</mx:VBox>
		
			<mx:VBox width="100%" height="100%" label="Etat des imports" backgroundColor="0xF0F0F0">
			
				<mx:DataGrid id="imports_list_GUI" rowHeight="20" headerHeight="40"
					rowCount="{imports_list.length + 1}" height="100%" width="100%"
					wordWrap="true" dataProvider="{imports_list}" 
					fontFamily="tahoma" editable="false" >
					<mx:columns>
						<mx:DataGridColumn dataField="finess" headerText="Finess" width="80" resizable="false"  editable="false" />
						<mx:DataGridColumn dataField="finess_texte" headerText="Etablissement" width="200" resizable="false"  editable="false" />
						<mx:DataGridColumn dataField="oid" headerText="N°" width="40"  resizable="false" editable="false" />
						<mx:DataGridColumn dataField="texte" headerText="Texte" width="200" resizable="false" editable="false" />
						<mx:DataGridColumn dataField="date_import" headerText="Date import" width="90" resizable="false"  editable="false" />
						<mx:DataGridColumn dataField="etat_texte" headerText="Etat" width="80" resizable="false"  editable="false" />
						<mx:DataGridColumn dataField="date_debut" headerText="Date début" width="90" resizable="false"  editable="false" />
						<mx:DataGridColumn dataField="date_fin" headerText="Date fin" width="90" resizable="false"  editable="false" />
						<mx:DataGridColumn dataField="nb_rss" headerText="RSS" width="60" resizable="false"  editable="false" />
						<mx:DataGridColumn dataField="nb_rum" headerText="RUM" width="60" resizable="false"  editable="false" />
						<mx:DataGridColumn dataField="nb_rsf" headerText="RSF" width="60" resizable="false"  editable="false" />
						<mx:DataGridColumn dataField="nb_erreurs" headerText="Erreurs" width="70" resizable="false"  editable="false" />
						<mx:DataGridColumn dataField="nb_avertissements" headerText="Avertissements" width="70" resizable="false"  editable="false" />
					</mx:columns>
				</mx:DataGrid>
			
			</mx:VBox>

			
			
			<mx:VBox width="100%" height="100%" label="Historique des travaux" backgroundColor="0xF0F0F0">
			
				<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>