poconsole.controller('notesCtrl', function($scope, Page, Note) {
	Page.setTitle('Notes');
    Page.setPage('notes');

    $scope.loadNotesPageData = function(page_number){
        var getData = Note.getNotesPage(page_number);
        getData.then(function(d){
            for(var j = 0; j < d.objects.length; j++){
                // console.log({'Date Before':d.objects[j].created_on});
                d.objects[j].created_on = Date.create(d.objects[j].created_on + "Z", { fromUTC: true });
                // console.log({'Date After':d.objects[j].created_on});
            }
            $scope.notes_data = d.objects;
            $scope.num_pages = d.total_pages;
            $scope.page_list = [];
            $scope.page_num = d.page;
			$scope.total = d.total;
        });
    };

    $scope.loadNotesPageData(1);

    $scope.startCreateNote = function(){
        $scope.newNote = {
            'note_text': "SAMPLE TEXT",
            'author': "Mark Twain",
            'created_on': Date.create()
        };
    };

    $scope.submitNewNote = function(){
        var temp_date = Date.create($scope.newNote.created_on);
        $scope.newNote.created_on = temp_date.utc().format('{yyyy}-{MM}-{dd}T{HH}:{mm}:00.00000');
        var createNote = Note.createNote($scope.newNote);
        createNote.then(function(response_data){
            console.log("Response from API");
            console.log(response_data);
            $scope.loadNotesPageData(1);
        });
    };

    $scope.startEditNote = function(id){
        var getEditNoteData = Note.getNote(id);
        getEditNoteData.then(function(d){
            $scope.editNote = d;
            $scope.editNote.created_on = Date.utc.create($scope.editNote.created_on);
        });
    };

    $scope.submitEditNote = function(){
        var temp_date = Date.create($scope.editNote.created_on);
        $scope.editNote.created_on = temp_date.utc().format('{yyyy}-{MM}-{dd}T{HH}:{mm}:00.00000');
        var updateNote = Note.updateNote($scope.editNote);
        updateNote.then(function(response_data){
            console.log("Response from API");
            console.log(response_data);
            $scope.loadNotesPageData(1);
        });
    };

    $scope.openDeleteNote = function(id){
        var getDeleteNoteData = Note.getNote(id);
        getDeleteNoteData.then(function(d){
            $scope.deleteNote = d;
        });
    };

    $scope.submitDeleteNote = function(){
        var deleteSelectedNote = Note.deleteNote($scope.deleteNote._id);
        deleteSelectedNote.then(function(data){
            $scope.loadNotesPageData(1);
        });
    };




});
