angular.module("mediaModal")
.controller("uploadController", ['$scope', '$http', 'upload_url', function ($scope, $http, upload_url) {
    $scope.upload_url = upload_url;
    $scope.options = {
        url: upload_url
    };
    $scope.loadingFiles = true;
    $http.get(upload_url)
        .then(
            function (response) {
                $scope.loadingFiles = false;
                $scope.filequeue = response.data.files || [];
            },
            function () {
                $scope.loadingFiles = false;
            }
        );

    $scope.selectImage = function(image){

        //console.log(image);

        image.selected = !image.selected;
        image.thumbnail = image.url;
        if ($scope.selectedImages){
            if (image.selected){
                $scope.selectedImages.push(image);
                return;
            }
            var imageIndex = $scope.selectedImages.indexOf(image);
            if (imageIndex != -1){
                $scope.selectedImages.splice(imageIndex, 1);
            }
        }
    };
    $scope.$on('fileuploaddone', function(e, data){
        // Your code here
        $.each(data.result.files, function (index, file) {
            $scope.selectImage(file);
        });
    });
}
]);