'use strict';

/* global _, moment, CartUtility */
module.exports = function($scope, $location, $modalInstance, $dataService) {
    CartUtility.log('CartBlogListModalCtrl');

    $scope.moment = moment;

    $scope.dataRows = [];
    $scope.rootUrl = CartUtility.getPureRootUrlFromLocation($location);
    $scope.encodeURIComponent = encodeURIComponent;

    // pagination
    $scope.dataOnPage = []; // data used to be displayed on page
    $scope.paginationTotalItems = 0;
    $scope.paginationCurrentPage = 1;
    $scope.itemsPerPage = 10;
    $scope.paginationMaxSize = 5;
    $scope.loadPageData = function() {
        var startPos = ($scope.paginationCurrentPage - 1) * $scope.itemsPerPage;
        var endPos = startPos + $scope.itemsPerPage;
        $scope.dataOnPage = $scope.dataRows.slice(startPos, endPos);
    };

    $scope.uploadTmpPost = function(uuid) {
        $dataService.postGetTmp(uuid).then(function(tmpPostData) {
            $dataService.postUpload(tmpPostData);
        }, function(err) {
            if (err.status === 404) {
                // 404 not found error would not be notified in $dataService class
                CartUtility.notify('Error!', 'Target tmp post data not found, uuid: ' + uuid, 'error');
            }
        });
    };

    $scope.deleteTmpPost = function(uuid) {
        $dataService.postRemoveTmp(uuid).then(function(result) {
            if (result === true) {
                CartUtility.notify('Done!', 'Tmp post data deleted!', 'success');
            } else {
                CartUtility.notify('Error!', 'Error in deleting tmp post data!', 'error');
            }
        });
    };

    $scope.deleteAllTmpPost = function() {
        $dataService.postRemoveAllTmp().then(function(result) {
            if (result === true) {
                CartUtility.notify('Done!', 'All tmp post data deleted!', 'success');
            } else {
                CartUtility.notify('Error!', 'Error in deleting all tmp post data!', 'error');
            }
        });
    };

    $dataService.postGetAllTmp().then(function(result) {
        _.forEach(result, function(post) {
            post.published = _.isNull($dataService.postSearchById(post._id)) ? false : true;
        });
        $scope.dataRows = _.sortBy(result, function(post) { return post.updated; }).reverse();
        $scope.paginationTotalItems = result.length;
        $scope.paginationCurrentPage = 1;
        $scope.loadPageData();
    });

    $scope.close = function() {
        $modalInstance.close();
    };
};
@@ -1303,32 +1303,64 @@
 unction(uuid) %7B%0A
+        $modalInstance.close();%0A
         $dataSer
@@ -1402,57 +1402,12 @@
 ion(
-result
 ) %7B%0A
-            if (result === true) %7B%0A    
     
@@ -1484,34 +1484,37 @@
 ');%0A        
-    %7D else
+%7D, function()
  %7B%0A         
@@ -1508,36 +1508,32 @@
 ) %7B%0A            
-  
-  
 CartUtility.noti
@@ -1587,38 +1587,24 @@
 , 'error');%0A
-            %7D%0A
         %7D);%0A
@@ -1712,57 +1712,12 @@
 ion(
-result) %7B%0A            if (result === true) %7B%0A    
+) %7B%0A
     
@@ -1806,25 +1806,24 @@
     
-    %7D else %7B%0A    
+%7D, function() %7B%0A
     
@@ -1905,38 +1905,24 @@
 , 'error');%0A
-            %7D%0A
         %7D);%0A

Fix future response handling of several front end js functions.