<?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;
            
            private var localPHP:String = "modules/pmsi/php/PMSI_simulation.php";
            
            [Bindable]
            public var title:String = "Groupage FG2023";

            [Bindable]
            public var FG_YEAR:String = "2023";
            
            [Bindable]
            public var hasHelp:Boolean = false;
            
            [Bindable]
            public var files_list:XMLList;
            [Bindable]
            public var files_len:int;
            
            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 {
                
                GUI_status.height = 20
                GUI_status.text = "Chargement état groupage depuis serveur";
                GUI_warning.text = ""
                GUI_warning.height = 0
                
                var urlVariables:URLVariables = new URLVariables()
                urlVariables.operation = "get_list";
                urlVariables.FG_YEAR = FG_YEAR;
                CTI_Utilitaires.startRequestHTTP(localPHP, urlVariables, exec, true, this.parentApplication)
                function exec(resultatService:XML):void {
                    if (resultatService.RETURN[0].@warning.toString() != "") {
                        GUI_warning.text = resultatService.RETURN[0].@warning.toString()
                        GUI_warning.height = 120
                    }
                    
                    GUI_status.text = "";
                    GUI_status.height = 0
                    if (resultatService.RETURN[0].@code.toString() == "KO") {
                        if (resultatService.RETURN[0].@complement.toString() == "") {
                            CTI_Utilitaires.alert_error("Groupage FG2023", resultatService.RETURN[0].@message.toString());
                        } else {
                            GUI_status.text = "Chargement état groupage depuis PC";
                            GUI_status.height = 20
                            var urlVariables:URLVariables = new URLVariables()
                            urlVariables.header = resultatService.RETURN[0].@complement_v1.toString()
                            urlVariables.operation = resultatService.RETURN[0].@complement_v3.toString()
                            CTI_Utilitaires.startRequestHTTP(resultatService.RETURN[0].@complement.toString(), urlVariables, execLocal, true, this.parentApplication)
                        }
                    } else {
                        files_list = resultatService.FILE
                        files_len = files_list.length
                    }
                    
                    setTimeout(setBuildComplete, 1000);
                
                }
                function execLocal(resultatService:XML):void {
                    GUI_status.text = "";
                    GUI_status.height = 0
                    if (resultatService.@returnCode.toString() == "OK") {
                        files_list = resultatService.FILE
                        files_len = files_list.length
                    } else {
                        CTI_Utilitaires.alert_error("Groupage FG2023", resultatService.@returnMessage.toString());
                    }
                }
            }
            
            private function setBuildComplete():void {
                
                buildComplete = true;
            }
            
            private function resize():void {
            
            }
            
            public function kill_to_cti():void {
                
                var urlVariables:URLVariables = new URLVariables()
                urlVariables.operation = "kill_all";
                urlVariables.FG_YEAR = FG_YEAR;
                CTI_Utilitaires.startRequestHTTP(localPHP, urlVariables, exec, true, this.parentApplication)
                function exec(resultatService:XML):void {
                    if (resultatService.RETURN[0].@code.toString() == "KO") {
                        CTI_Utilitaires.alert_error("Suppression des téléversements", resultatService.RETURN[0].@message.toString());
                    } else {
                        CTI_Utilitaires.alert_info("Suppression des téléversements", resultatService.RETURN[0].@message.toString());
                        getData();
                    }
                
                }
            
            }
            
            public function send_file():void {
                
                GUI_status.text = "Téléversement Séjours non groupés vers serveur";
                GUI_status.height = 20
                var urlVariables:URLVariables = new URLVariables()
                urlVariables.operation = "send_file";
                urlVariables.FG_YEAR = FG_YEAR;                
                CTI_Utilitaires.startRequestHTTP(localPHP, urlVariables, exec, true, this.parentApplication)
                function exec(resultatService:XML):void {
                    GUI_status.text = ""
                    GUI_status.height = 0
                    if (resultatService.RETURN[0].@code.toString() == "KO") {
                        if (resultatService.RETURN[0].@complement.toString() == "") {
                            CTI_Utilitaires.alert_error("Téléversement des Séjours non groupés", resultatService.RETURN[0].@message.toString());
                        } else {
                            GUI_status.text = "Téléversement séjours non groupés depuis PC";
                            GUI_status.height = 20
                            
                            var urlVariables:URLVariables = new URLVariables()
                            urlVariables.file = resultatService.RETURN[0].@complement_v1.toString()
                            urlVariables.data = resultatService.RETURN[0].@complement_v2.toString()
                            urlVariables.operation = resultatService.RETURN[0].@complement_v3.toString()
                            CTI_Utilitaires.startRequestHTTP(resultatService.RETURN[0].@complement.toString(), urlVariables, execLocal, true, this.parentApplication)
                        }
                    } else {
                        CTI_Utilitaires.alert_info("Téléversement des séjours non groupés", resultatService.RETURN[0].@message.toString());
                        getData();
                    }
                }
                function execLocal(resultatService:XML):void {
                    GUI_status.text = ""
                    GUI_status.height = 0
                    if (resultatService.@returnCode.toString() == "OK") {
                        CTI_Utilitaires.alert_info("Téléversement des séjours non groupés", resultatService.@returnMessage.toString());
                    } else {
                        CTI_Utilitaires.alert_error("Groupage FG2023", resultatService.@returnMessage.toString());
                    }
                    getData();
                }
            }
            
            public function get_file():void {
                
                GUI_status.text = "Réception Séjours groupés depuis serveur";
                GUI_status.height = 20
                
                var file:String = files_list_GUI.selectedItem.@fg1_name.toString()
                
                var urlVariables:URLVariables = new URLVariables()
                urlVariables.operation = "get_file"
                urlVariables.file = file;
                urlVariables.FG_YEAR = FG_YEAR;             
                CTI_Utilitaires.startRequestHTTP(localPHP, urlVariables, exec, true, this.parentApplication)
                function exec(resultatService:XML):void {
                    if (resultatService.RETURN[0].@code.toString() == "KO") {
                        if (resultatService.RETURN[0].@complement.toString() == "") {
                            CTI_Utilitaires.alert_error("Téléchargement Séjours groupés", resultatService.RETURN[0].@message.toString());
                        } else {
                            GUI_status.text = "Réception Séjours groupés depuis PC";
                            GUI_status.height = 20
                            
                            var urlVariables:URLVariables = new URLVariables()
                            urlVariables.file = resultatService.RETURN[0].@complement_v1.toString()
                            urlVariables.operation = resultatService.RETURN[0].@complement_v3.toString()
                            CTI_Utilitaires.startRequestHTTP(resultatService.RETURN[0].@complement.toString(), urlVariables, execLocal, true, this.parentApplication)
                        }
                    } else {
                        trt_file(file);
                    }
                
                }
                
                function execLocal(resultatService:XML):void {
                    GUI_status.text = ""
                    GUI_status.height = 0
                    if (resultatService.@returnCode.toString() == "OK") {
                        trt_file(file, resultatService.@returnData.toString());
                    } else {
                        CTI_Utilitaires.alert_error("Groupage FG2023", resultatService.@returnMessage.toString());
                    }
                }
            }
            
            public function trt_file(file:String, dataFG13_11d:String = ""):void {
                
                GUI_status.text = "Traitement Séjours groupés sur serveur";
                GUI_status.height = 20
                
                var urlVariables:URLVariables = new URLVariables()
                urlVariables.operation = "trt_file"
                urlVariables.dataFG13_11d = dataFG13_11d
                urlVariables.file = file;
                urlVariables.FG_YEAR = FG_YEAR;
                CTI_Utilitaires.startRequestHTTP(localPHP, urlVariables, exec, true, this.parentApplication)
                function exec(resultatService:XML):void {
                    GUI_status.text = "";
                    GUI_status.height = 0
                    if (resultatService.RETURN[0].@code.toString() == "KO") {
                        CTI_Utilitaires.alert_error("Téléchargement Séjours groupés", resultatService.RETURN[0].@message.toString());
                    } else {
                        CTI_Utilitaires.alert_info("Téléchargement Séjours groupés", resultatService.RETURN[0].@message.toString());
                        getData();
                    }
                
                }
            
            }
            
            
        ]]>
    </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="48">
            <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:Spacer width="10" />
                <mx:Button height="28"
                           label="Téléverser Séjours vers Web CTI"
                           toolTip="Téléverse des séjours vers CTI Santé pour groupage"
                           icon="@Embed('../../base/swf/CTI/images/24/go-up.png')"
                           labelPlacement="right"
                           click="send_file()" />
                <mx:Button height="28"
                           visible="{files_list_GUI.selectedItem != null &amp;&amp; files_list_GUI.selectedItem.@fg1_remote_date != ''}"
                           label="Télécharger séjours groupés depuis Web CTI"
                           toolTip="Télécharge les séjours groupés depuis CTI Santé pour simulation"
                           icon="@Embed('../../base/swf/CTI/images/24/document-save.png')"
                           labelPlacement="right"
                           click="get_file()" />
                <mx:Spacer width="20" />
                <mx:Button height="28"
                           visible="false"
                           label="Suppression Téléversement"
                           toolTip="Supprime des séjours téléversés chez CTI Santé pour groupage"
                           icon="@Embed('../../base/swf/CTI/images/24/user-trash.png')"
                           labelPlacement="right"
                           click="kill_to_cti()" />
            </mx:HBox>
        </mx:Canvas>


        <mx:VBox width="100%" height="100%">
            <mx:DataGrid id="files_list_GUI"
                         rowHeight="20"
                         headerHeight="40"
                         rowCount="{files_len + 1}"
                         height="100%"
                         width="100%"
                         wordWrap="true"
                         dataProvider="{files_list}"
                         fontFamily="tahoma"
                         editable="false">
                <mx:columns>
                    <mx:DataGridColumn dataField="@fg0_name"
                                       headerText="Séjours téléversés"
                                       width="400"
                                       resizable="false"
                                       editable="false" />
                    <mx:DataGridColumn dataField="@fg0_remote_date"
                                       headerText="Date téléversement"
                                       width="100"
                                       resizable="false"
                                       editable="false" />
                    <mx:DataGridColumn dataField="@fg0_size"
                                       headerText="Taille"
                                       width="100"
                                       resizable="false"
                                       editable="false" />
                    <mx:DataGridColumn dataField="@fg1_remote_date"
                                       headerText="Groupé en FG2023 par CTI"
                                       width="100"
                                       resizable="false"
                                       editable="false" />
                </mx:columns>
            </mx:DataGrid>
        </mx:VBox>
    </mx:VBox>
    <mx:VBox width="100%">
        <mx:TextArea id="GUI_warning"
                     text=""
                     width="100%"
                     height="0"
                     fontSize="12"
                     fontWeight="bold"
                     textAlign="center"
                     backgroundColor="0xFF9900"
                     visible="{GUI_warning.text != ''}" />
        <mx:TextArea id="GUI_status"
                     text=""
                     width="100%"
                     height="0"
                     fontSize="12"
                     fontWeight="bold"
                     textAlign="center" />
    </mx:VBox>
</mx:Application>
