<?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:cti_components="CTI.technique.components.*"
				xmlns="*"
				creationComplete="creationComplete()"
				resize="resize()"
				styleName="CTIView"
				paddingTop="0"
				paddingBottom="0"
				paddingRight="0"
				paddingLeft="0"
				creationPolicy="all">

	<mx:Script>
		<![CDATA[
			import CTI.technique.CTI_ExcelUtilitaires;
			import CTI.technique.CTI_Utilitaires;
			import mx.collections.ArrayCollection;
			import mx.collections.XMLListCollection;
			import mx.controls.Alert;
			import mx.controls.TextInput;
			import mx.events.CloseEvent;
			import mx.events.DataGridEvent;
			import mx.events.DataGridEventReason;
			
			
			public var buildComplete:Boolean = false;
			
			[Embed(source="../../base/swf/CTI/images/32/dialog-question.png")]
			public static var question_class:Class;
			
			[Bindable]
			public var title:String = "Gestion du budget";
			
			[Bindable]
			public var hasHelp:Boolean = false;
			
			[Bindable]
			private var BUDGETS_XML:XMLListCollection;
			
			[Bindable]
			public var budgetUpdated:Boolean = false;
			
			private var currentBudgetItem:XML = null;
			
			[Bindable]
			private var BUDGET_XML:XML;
			
			public function creationComplete():void
			{
				
				if (application.currentHelpFile != "")
				{
					hasHelp = true;
				}
				
				if (application.name == "CTI")
				{
				}
				if (application.name == "CTI_1")
				{
					GUI_titleBar.height = 0
				}
				
				getData()
			}
			
			public function eventDispatcher(eventCode:String):Boolean
			{
				return true
			}
			
			public function getData():void
			{
				
				var urlVariables:URLVariables = new URLVariables()
				urlVariables.softCode = application.currentSoftCode;
				
				CTI_Utilitaires.startRequestHTTP("modules/activite/php/httpService_ACTI_budgetManager_getBudgets.php", urlVariables, exec, true, this.parentApplication)
				
				function exec(resultatService:XML):void
				{
					
					BUDGETS_XML = new XMLListCollection(resultatService.BUDGET);
					
					setTimeout(setBuildComplete, 1000);
				
				}
			
			}
			
			private function setBuildComplete():void
			{
				buildComplete = true;
			}
			
			private function resize():void
			{
			
			}
			
			public function getBudget():void
			{
				if (budgetUpdated == true)
				{
					return;
				}
				
				if (list_GUI.selectedIndex < 0)
				{
					return;
				}
				
				currentBudgetItem = list_GUI.selectedItem as XML
				
				showDetailBudget()
			}
			
			public function createBudget():void
			{
				if (budgetUpdated == true)
				{
					return;
				}
				
				currentBudgetItem =  <BUDGET oid="-1" code="" texte="Nouveau budget"  />
				
				showDetailBudget()
			}
			
			public function showDetailBudget():void
			{
				var i:Number
				
				if (currentBudgetItem == null)
				{
					return;
				}
				
				oid_GUI.text = currentBudgetItem.@oid.toString();
				code_GUI.text = currentBudgetItem.@code.toString();
				texte_GUI.text = currentBudgetItem.@texte.toString();
				
				var urlVariables:URLVariables = new URLVariables()
				urlVariables.budget_id = currentBudgetItem.@oid.toString();
				
				CTI_Utilitaires.startRequestHTTP("modules/activite/php/httpService_ACTI_budgetManager_getBudget.php", urlVariables, exec, true, this.parentApplication)
				
				function exec(resultatService:XML):void
				{
					
					BUDGET_XML = resultatService
					
					detailBox.percentWidth = 100
					listBox.width = 0
				
				}
			
			}
			
			public function setUpdated():void
			{
				budgetUpdated = true
			}
			
			private function askCancelBudget():void
			{
				
				if (budgetUpdated == false)
				{
					hideBudget()
					return;
				}
				
				Alert.yesLabel = "OUI";
				Alert.noLabel = "NON";
				var alert:Alert = Alert.show("Les données ont été changées, désirez vous les annuler ?", "Gestion budgets", 3, this, alertClickHandler, question_class);
				
				function alertClickHandler(event:CloseEvent):void
				{
					if (event.detail == Alert.YES)
					{
						hideBudget()
					}
					else
					{
						
					}
				}
			
			}
			
			private function hideBudget():void
			{
				
				detailBox.width = 0
				listBox.percentWidth = 100
				currentBudgetItem = null;
				budgetUpdated = false;
			}
			
			private function askDeleteBudget():void
			{
				
				Alert.yesLabel = "OUI";
				Alert.noLabel = "NON";
				var alert:Alert = Alert.show("Désirez vous vraiment supprimer le budget " + texte_GUI.text + " ?", "Gestion budgets", 3, this, alertClickHandler, question_class);
				
				function alertClickHandler(event:CloseEvent):void
				{
					if (event.detail == Alert.YES)
					{
						deleteBudget()
					}
					else
					{
						
					}
				}
			
			}
			
			public function deleteBudget():void
			{
				
				updateBudget("delete")
			
			}
			
			public function copyBudget():void
			{
				
				updateBudget("copy")
			
			}
			
			public function saveBudget():void
			{
				
				updateBudget("save")
			
			}
			
			public function updateBudget(action:String):void
			{
				
				trace(BUDGET_XML.toXMLString())
				
				var urlVariables:URLVariables = new URLVariables()
				urlVariables.action = action
				
				BUDGET_XML.@oid = oid_GUI.text
				BUDGET_XML.@code = code_GUI.text
				BUDGET_XML.@texte = texte_GUI.text
				
				urlVariables.xmldef = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + BUDGET_XML.toXMLString();
				
				CTI_Utilitaires.startRequestHTTP("modules/activite/php/httpService_ACTI_budgetManager_setBudget.php", urlVariables, exec, true, this.parentApplication)
				
				function exec(resultatService:XML):void
				{
					
					getData()
					
					detailBox.width = 0
					listBox.percentWidth = 100
					currentBudgetItem = null;
					budgetUpdated = false;
				}
			
			}
		]]>
	</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="100%">

			<mx:VBox id="listBox"
					 width="100%"
					 height="100%"
					 backgroundColor="0xFFFFFF"
					 verticalScrollPolicy="off">
				<mx:Canvas width="100%"
						   height="28">
					<mx:HBox id="GUI_buttonBar"
							 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="Créer un nouveau budget"
								   icon="@Embed('../../base/swf/CTI/images/24/list-add.png')"
								   labelPlacement="right"
								   click="createBudget()"
								   visible="{budgetUpdated == false &amp;&amp; budgetUpdated == true}" />
						<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:Spacer width="100%" />
					</mx:HBox>
				</mx:Canvas>
				<mx:DataGrid id="list_GUI"
							 rowHeight="20"
							 rowCount="{BUDGETS_XML.length + 1}"
							 height="100%"
							 width="100%"
							 wordWrap="false"
							 dataProvider="{BUDGETS_XML}"
							 fontFamily="tahoma"
							 itemClick="getBudget()"
							 selectable="{!budgetUpdated}">
					<mx:columns>
						<mx:DataGridColumn dataField="@texte"
										   headerText="Nom"
										   width="400"
										   editable="false" />
						<mx:DataGridColumn dataField="@code"
										   headerText="Code"
										   width="100"
										   editable="false" />
					</mx:columns>
				</mx:DataGrid>
			</mx:VBox>


			<mx:VBox id="detailBox"
					 width="0"
					 height="100%"
					 backgroundColor="0xFFFFFF"
					 verticalScrollPolicy="off">
				<mx:HBox>
					<mx:Canvas>
						<mx:Button height="28"
								   width="28"
								   toolTip="Annuler les modifications"
								   icon="@Embed('../../base/swf/CTI/images/24/window-close.png')"
								   labelPlacement="right"
								   click="askCancelBudget()"
								   visible="{budgetUpdated == true}" />
						<mx:Button height="28"
								   width="28"
								   toolTip="Retour à la liste"
								   icon="@Embed('../../base/swf/CTI/images/24/go-previous.png')"
								   labelPlacement="right"
								   click="hideBudget()"
								   visible="{budgetUpdated == false}" />
					</mx:Canvas>
					<mx:Canvas>
						<mx:Button height="28"
								   width="28"
								   toolTip="Enregistrer les modifications"
								   icon="@Embed('../../base/swf/CTI/images/24/dialog-ok.png')"
								   labelPlacement="right"
								   click="saveBudget()"
								   visible="{budgetUpdated == true}" />
						<mx:HBox visible="false">
							<mx:Button height="28"
									   width="28"
									   toolTip="Copier le budget"
									   icon="@Embed('../../base/swf/CTI/images/24/edit-copy.png')"
									   labelPlacement="right"
									   click="copyBudget()"
									   visible="{budgetUpdated == false &amp;&amp; oid_GUI.text != '-1'}" />
							<mx:Button height="28"
									   width="28"
									   toolTip="Supprimer le budget"
									   icon="@Embed('../../base/swf/CTI/images/24/user-trash.png')"
									   labelPlacement="right"
									   click="askDeleteBudget()"
									   visible="{budgetUpdated == false &amp;&amp; oid_GUI.text != '-1'}" />
						</mx:HBox>
					</mx:Canvas>
				</mx:HBox>

				<mx:HBox visible="false"
						 height="0">
					<mx:Label width="150"
							  text="Oid" />
					<mx:TextArea width="300"
								 height="18"
								 id="oid_GUI"
								 change="budgetUpdated = true"
								 editable="false" />
				</mx:HBox>
				<mx:HBox>
					<mx:Label width="100"
							  text="Texte" />
					<mx:TextArea width="300"
								 height="18"
								 id="texte_GUI"
								 change="budgetUpdated = true" />
					<mx:Label text="Code" />
					<mx:TextArea width="100"
								 height="18"
								 id="code_GUI"
								 change="budgetUpdated = true" />
					<mx:Spacer width="100%" />
				</mx:HBox>


				<mx:List id="mylist"
						 labelField="@code"
						 columnCount="1"
						 dataProvider="{BUDGET_XML.PARENT}"
						 width="100%"
						 height="100%"
						 variableRowHeight="true">
					<mx:itemRenderer>
						<mx:Component>
							<cti_components:CTI_ACTI_BudgetManager_saisie width="100%">
							</cti_components:CTI_ACTI_BudgetManager_saisie>
						</mx:Component>
					</mx:itemRenderer>
				</mx:List>



			</mx:VBox>



		</mx:Canvas>



	</mx:VBox>


</mx:Application>
