/* Controllers */

var ecAppControllers = angular.module('ecAppControllers', ['restangular']);


ecAppControllers.controller('readyCtrl', 
  ['$scope', 'Restangular', '$q',
  function($scope, Restangular, $q) {
    'use strict';

    Restangular.all('list_types/').getList().then(function(types) {
      $scope.types = types;
    });
  }]);


ecAppControllers.controller('callCtrl', 
  ['$scope', '$routeParams', 'Restangular', '$q',
  function($scope, $routeParams, Restangular, $q) {
    'use strict';

    Restangular.one('list_types/' + $routeParams.callCat + '/').get().then(
      function(callType) {
        $scope.callType = callType;
    });

    $scope.getNextRecord = function() {
      var deferred = $q.defer();

      var record = Restangular.one('call_records/' + $routeParams.callCat + '/next/');
      record.get().then(
        function(demographics) {
          $scope.demographics = demographics;
          deferred.resolve();
        }, function (response) {
          console.log("Error with status code", response.status);
          deferred.reject();
        });

      return deferred.promise;
    };

    $scope.getUserNotes = function() {
      Restangular.one('call_records/' + $scope.demographics.pk + '/notes/')
        .get().then(
          function(usernotes) {
            $scope.usernotes = usernotes;
      });
    };

    $scope.getSystemNotes = function() {
      Restangular.one('call_records/' + $scope.demographics.pk + '/sysnotes/')
        .get().then(
          function(systemnotes) {
            $scope.systemnotes = systemnotes;
      });
    };

    $scope.getExtraInfo = function() {
      Restangular.one('call_records/' + $scope.demographics.pk + '/extra/')
        .get().then(
          function(extra) {
            $scope.extra = extra;
      });
    };

    $scope.createNote = function(text) {
      var recordId = $scope.demographics.pk;
      var notes = Restangular.one('call_records', recordId).all('notes/');
      
      var newNote = {text: text};
      notes.post(newNote, {}, {"X-CSRFToken": csrf_token}).then(
        function (response) {
          console.log("Note created.");
          $scope.getUserNotes();
        }, function (response) {
          console.log("Error with status code", response.status);
        });
    };

    $scope.deleteNote = function(id) {
      var note = Restangular.one('user_notes/' + id + '/');
      
      note.remove({}, {"X-CSRFToken": csrf_token}).then(
        function (response) {
          console.log("Note deleted.");
          $scope.getUserNotes();
        }, function (response) {
          console.log("Error with status code", response.status);
        });
    };

    $scope.updateNote = function(id, text) {
      var note = Restangular.one('user_notes/' + id + '/');
      note.get().then(
        function(data) {
          data.text = text;
          data.put({}, {"X-CSRFToken": csrf_token}).then(
            function (response) {
              console.log("Note updated.");
              $scope.getUserNotes();
            }, function (response) {
              console.log("Error with status code", response.status);
            });
        });
    };

    $scope.updateSysNotes = function(data) {
      var id = data.call_record;
      var notes = Restangular.one('call_records/' + id + '/sysnotes/');
      notes.get().then(
        function(recnotes) {
          recnotes.note1 = data.note1;
          recnotes.note2 = data.note2;
          recnotes.note3 = data.note3;
          recnotes.put({}, {"X-CSRFToken": csrf_token}).then(
            function (response) {
              console.log("System Notes updated.");
              $scope.getSystemNotes();
            }, function (response) {
              console.log("Error with status code", response.status);
            });
        });
    };

    $scope.next = function() {
      $scope.getNextRecord()
        .then( function() {
          $scope.getUserNotes();
          $scope.getExtraInfo();
        });
    };
    $scope.next();

  }]);
@@ -1796,32 +1796,228 @@
     %7D);%0A    %7D;%0A%0A
+    $scope.getCall = function() %7B%0A      Restangular.one('call/' + $scope.demographics.call + '/')%0A        .get().then(%0A          function(call) %7B%0A            $scope.call = call;%0A      %7D);%0A    %7D;%0A%0A
     $scope.creat
@@ -4013,32 +4013,969 @@
     %7D);%0A    %7D;%0A%0A
+    $scope.updateCall = function(data, button) %7B%0A      var id = data.pk;%0A      var call = Restangular.one('call/' + id + '/');%0A      call.get().then(%0A        function(callinfo) %7B%0A          callinfo.data1 = data.data1;%0A          callinfo.data2 = data.data2;%0A          callinfo.data3 = data.data3;%0A          callinfo.data4 = data.data4;%0A          callinfo.data5 = data.data5;%0A          callinfo.data6 = data.data6;%0A          callinfo.data7 = data.data7;%0A          callinfo.data8 = data.data8;%0A          // callinfo.???? = button;%0A%0A          // FIXME:  the API does not support PUT yet%0A          %0A          // callinfo.put(%7B%7D, %7B%22X-CSRFToken%22: csrf_token%7D).then(%0A          //   function (response) %7B%0A          //     console.log(%22Call updated.%22);%0A          //     $scope.getSystemNotes();%0A          //   %7D, function (response) %7B%0A          //     console.log(%22Error with status code%22, response.status);%0A          //   %7D);%0A        %7D);%0A    %7D;%0A%0A
     $scope.next 
@@ -5108,24 +5108,87 @@
 xtraInfo();%0A
+          $scope.getSystemNotes();%0A          $scope.getCall();%0A
         %7D);%0A

Add Call related functions to the controller.