//
// This is our "Enter OPAT" flow controller
// 
controllers.controller(
    'OPATReferralCtrl',
    function($scope, $modalInstance, $modal, $rootScope,
             schema, options,
             Episode){
        
        $scope.model = {
            hospital_number : null
        }
        $scope.patient = null;

        //
        // When we've created an episode with this flow, tag it to the correct
        // teams and then kill the modal.
        // 
        $scope.tag_and_close = function(episode){
            if(!episode.newItem){
                episode = new Episode(episode, schema);
            };
            if(!episode.tagging[0].makeCopy){
                episode.tagging[0] = episode.newItem('tagging');
            }
            var teams = episode.tagging[0].makeCopy();
            var location = episode.location[0].makeCopy();
            teams.opat = true;
            teams.opat_referrals = true;
            location.category = 'OPAT';

            //
            // Pre fill some tests:
            //
            var mrsa = episode.newItem('microbiology_test');
            var vte = episode.newItem('microbiology_test');

            // TODO - these are promises - the API is nicer than this !
            episode.tagging[0].save(teams).then(function(){
                episode.location[0].save(location).then(function(){
                    mrsa.save({test: 'MRSA PCR'}).then(function(mrsa_test){
                        vte.save({test: 'VTE Assessment'}).then(function(){
                            episode.active = true;
                            $modalInstance.close(episode);
                        });
                    });
                })
            });
        };

        // 
        // We have an initial hospital number - we can now figure out whether to
        // Add or pull over.
        // 
        $scope.find_by_hospital_number = function(){
            Episode.findByHospitalNumber(
                $scope.model.hospital_number,
                {
                    newPatient: $scope.new_patient,
                    newForPatient: $scope.new_for_patient,
                    error: function(){
			            // This shouldn't happen, but we should probably handle it better
			            alert('ERROR: More than one patient found with hospital number');
                        $modalInstance.close(null)
                    }
                }
            );
        };

        // 
        // Create a new patient
        // 
        $scope.new_patient = function(result){
			// There is no patient with this hospital number
			// Show user the form for creating a new episode,
            // with the hospital number pre-populated
			modal = $modal.open({
				templateUrl: '/templates/modals/opat/add_episode.html/',
				controller: 'AddEpisodeCtrl',
				resolve: {
					schema: function() { return schema; },
					options: function() { return options; },
					demographics: function() {
						return { hospital_number: $scope.hospital_number }
					}
				}
			}).result.then(function(result) {
				// The user has created the episode, or cancelled
                if(result){ // We made an episode!
                    $scope.tag_and_close(result);
                }else{
				    $modalInstance.close(result);
                }
			});
        };

        // 
        // Create a new episode for an existing patient
        // 
        $scope.new_for_patient = function(patient){
            if(patient.active_episode_id && _.keys(patient.episodes).length > 0){
                // Offer to import the data from this episode.
				for (var eix in patient.episodes) {
					patient.episodes[eix] = new Episode(patient.episodes[eix], schema);
				};
				modal = $modal.open({
					templateUrl: '/templates/modals/copy_to_category.html/',
					controller: 'CopyToCategoryCtrl',
					resolve: {
                        category: function() { return 'OPAT' },
						patient: function() { return patient; },
					}
				}).result.then(
                    function(result) {
                        if(!_.isString(result)){
                            $scope.tag_and_close(result);
                            return
                        };
					    if (result == 'open-new') {
						    // User has chosen to open a new episode
                            $scope.add_for_patient(patient);
					    } else {
						    // User has chosen to reopen an episode, or cancelled
						    $modalInstance.close(result);
					    };
				    },
                    function(result){ $modalInstance.close(result); });
            }else{
                $scope.add_for_patient(patient);
            };
        };
        
        // 
        // Add a new episode for an existing patient. Pre-fill the relevant demographics
        // 
        $scope.add_for_patient = function(patient){
            var demographics = patient.demographics[0];
            if(demographics.date_of_birth){
                var dob = moment(demographics.date_of_birth, 'YYYY-MM-DD')
                    .format('DD/MM/YYYY');
				demographics.date_of_birth = dob;
            }
			modal = $modal.open({
				templateUrl: '/templates/modals/opat/add_episode.html/',
				controller: 'AddEpisodeCtrl',
				resolve: {
					schema: function() { return schema; },
					options: function() { return options; },
					demographics: function() { return demographics; }
				}
			}).result.then(function(result) {
				// The user has created the episode, or cancelled
                if(result){ // We made an episode!
                    $scope.tag_and_close(result);
                }else{
				    $modalInstance.close(result);
                }
			});                
        };

        // Let's have a nice way to kill the modal.
        $scope.cancel = function() {
	        $modalInstance.close('cancel');
        };        
    }
);

@@ -148,16 +148,20 @@
 otScope,
+ $q,
 %0A       
@@ -966,16 +966,63 @@
  'OPAT';
+%0A            location.opat_referral = moment();
 %0A%0A      
@@ -1219,68 +1219,21 @@
     
-// TODO - these are promises - the API is nicer than this !%0A
+$q.all(%5B%0A    
     
@@ -1270,33 +1270,17 @@
 e(teams)
-.then(function()%7B
+,
 %0A       
@@ -1322,38 +1322,18 @@
 ocation)
-.then(function()%7B%0A    
+,%0A
         
@@ -1373,43 +1373,10 @@
 R'%7D)
-.then(function(mrsa_test)%7B%0A        
+,%0A
     
@@ -1421,16 +1421,31 @@
 sment'%7D)
+%0A            %5D)
 .then(fu
@@ -1454,28 +1454,16 @@
 tion()%7B%0A
-            
         
@@ -1505,36 +1505,24 @@
             
-   
-         
 $modalInstan
@@ -1548,36 +1548,24 @@
             
-     
-       
 %7D);%0A        
@@ -1580,46 +1580,12 @@
     
-%7D);%0A                %7D)%0A            %7D);
+    
 %0A   

Set OPAT referral to date of referral. Refactor OPAT referral saving to use promise.all() API