////////////////////////////////
// Controller for admin panel //
////////////////////////////////

'use strict';

pay.controller('Admin', [
    '$scope',
    '$http',
    '$timeout',
    'PayAuth',
    'Event',
    'FormValidator',
    'Error',
    function ($scope, $http, $timeout, PayAuth, Event, FormValidator, Error) {
        if (!PayAuth.needUser()) { return; }

        $scope.isAdmin = PayAuth.etu.isAdmin === true;
        $scope.fundation = PayAuth.etu.fundation;

        // Shows events list
        Event.query(function (events) {
            var keptEvents = [];
            var keptEventsSeller = [];
            $http.get('/api/accounts/' + PayAuth.etu.id).then(function (data) {
                data.data.forEach(function (account) {
                    events.forEach(function (event) {
                        if (event.id === account.event) {
                            if (PayAuth.etu.isAdmin) {
                                keptEvents.push(event);
                                keptEventsSeller.push(event);
                            } else if (account.admin) {
                                keptEvents.push(event);
                            } else {
                                keptEventsSeller.push(event);
                            }
                        }
                    });
                });
            }, function () {
                Error('Erreur', 0);
            });

            $scope.events = keptEvents;
            $scope.eventsSeller = keptEventsSeller;
        });

        // Model for the new event
        $scope.newEvent = {};
        $scope.fileInfo = null;
        $scope.datePattern = /^\d{1,2}\/\d{1,2}\/\d{4} [0-2][0-9]?:[0-5]?[0-9]$/;

        /**
         * Called when the ng-if has been evaluted
         */
        this.activateDate = function () {
            // Activate the datepicker, and ng-validate when the date changes
            $('.date').datetimepicker().on('dp.change', function () {
                var $self = $(this).children().first();
                $timeout(function () {
                    $self.data().$ngModelController.$setViewValue($self.val());
                }, 0);
            });
        };

        /**
         * Called when the ng-if has been evaluated
         */
        this.activateFile = function () {
            // Updates the input text with the input file name
            $('input[type=file]').on('change', function (e) {
                if (this.files.length === 1) {
                    $(this).parent().parent().next().val(this.files[0].name);
                }
            });
        };

        /**
         * Creates a event
         */
        this.createEvent = function () {
            if (!FormValidator(newEventForm, 'file')) {
                return;
            }

            var file = newEventForm.file.files[0];

            // Image -> string
            var reader = new FileReader();
            reader.onload = function (e) {
                var result = e.currentTarget.result;
                // Duplicate event to preserve from changes made by the resource
                var newEventData = jQuery.extend(true, {}, $scope.newEvent);

                newEventData.image = result;
                newEventData.date = moment(newEventData.date, 'DD/MM/YYYY HH:mm').toDate();
                newEventData.fundationId = $scope.fundation.id;
                var newEvent = new Event(newEventData);
                newEvent.$save(function () {}, function (res) {
                    // Request entity too large => file size too large
                    if (res.status === 413) {
                        Error('Erreur', 16);
                    }
                    Error('Erreur', res.data.error);
                });
            };
            reader.readAsDataURL(file);
        };
    }
]);

@@ -483,15 +483,26 @@
    /
-/
+**%0A         *
  Show
-s
  eve
@@ -510,16 +510,81 @@
 ts list%0A
+         */%0A        function getEventsListsWithAccounts () %7B%0A    
         
@@ -611,24 +611,28 @@
  (events) %7B%0A
+    
             
@@ -656,32 +656,36 @@
 %5B%5D;%0A            
+    
 var keptEventsSe
@@ -691,24 +691,28 @@
 eller = %5B%5D;%0A
+    
             
@@ -795,16 +795,20 @@
         
+    
 data.dat
@@ -858,16 +858,20 @@
         
+  
+  
 events.f
@@ -888,32 +888,36 @@
 ction (event) %7B%0A
+    
                 
@@ -982,24 +982,28 @@
             
+    
 if (PayAuth.
@@ -1041,32 +1041,36 @@
                 
+   
+ 
 keptEvents.push(
@@ -1069,32 +1069,36 @@
 ts.push(event);%0A
+    
                 
@@ -1167,24 +1167,28 @@
             
+  
+  
 %7D else if (a
@@ -1227,32 +1227,36 @@
                 
+    
 keptEvents.push(
@@ -1287,24 +1287,28 @@
             
+ 
+   
 %7D else %7B%0A   
@@ -1304,16 +1304,20 @@
  else %7B%0A
+    
         
@@ -1390,34 +1390,41 @@
                 
-%7D%0A
+    %7D%0A   
                 
@@ -1423,34 +1423,39 @@
                 
+ 
 %7D%0A
+    
                 
@@ -1470,36 +1470,44 @@
                 
+  
+  
 %7D);%0A
+    
             %7D, f
@@ -1527,32 +1527,36 @@
                 
+ 
+   
 Error('Erreur', 
@@ -1555,24 +1555,28 @@
 rreur', 0);%0A
+    
             
@@ -1584,32 +1584,36 @@
 );%0A%0A            
+    
 $scope.events = 
@@ -1628,32 +1628,36 @@
 ts;%0A            
+  
+  
 $scope.eventsSel
@@ -1680,33 +1680,86 @@
 Seller;%0A        
-%7D
+    %7D);%0A        %7D%0A%0A        getEventsListsWithAccounts(
 );%0A%0A        // M
@@ -3700,16 +3700,83 @@
 ion () %7B
+%0A                    getEventsListsWithAccounts();%0A                
 %7D, funct

Fix the account bug