angular.module('myApp.directives').directive('gimsChoiQuestion', function (QuestionAssistant) {
    return {
        restrict: 'E',
        template:   "<ng-form name='row show-grid innerQuestionForm'>" +
                        "<div class='col-md-12'>"+
                            "<table>"+
                                "<tr>"+
                                "   <td class='text-center' ng-repeat='part in question.parts' ng-switch='part.name'>"+
                                "       <div ng-switch-when='Total'>National</div>"+
                                "       <div ng-switch-when='Urban'>Urban</div>"+
                                "       <div ng-switch-when='Rural'>Rural</div>"+
                                "   </td><td></td>"+
                                "</tr>"+
                                "<tr ng-repeat='choice in question.choices' >"+
                                "   <td class='text-center' ng-repeat='part in question.parts' >"+
                                "       <div ng-switch='question.isMultiple'>"+
                                "           <div ng-switch-when='true'>" +
                                "               <input type='checkbox' ng-disabled='saving' ng-model='index[question.id+\"-\"+choice.id+\"-\"+part.id].isCheckboxChecked' ng-click='save(question,choice,part)' name='{{part.id}}-{{choice.is}}' />"+
                                "           </div>"+
                                "           <div ng-switch-when='false'>" +
                                "               <input type='radio' ng-disabled='saving' ng-required='question.isCompulsory' ng-model='index[question.id+\"-\"+part.id].valueChoice.id' value='{{choice.id}}' ng-click='save(question,choice,part)' name='{{part.id}}-{{question.id}}'/>"+
                                "           </div>"+
                                "       </div>"+
                                "   </td>"+
                                "   <td><div style='padding-top:5px'>{{choice.name}}</div></td>"+
                                "</tr>"+
                            "</table>"+
                        "   <span ng-show='question.isCompulsory' class='badge' ng-class=\"{'badge-danger':question.statusCode==1, 'badge-success':question.statusCode==3}\">Required</span>"+
                        "   <span ng-show='!question.isCompulsory' class='badge' ng-class=\"{'badge-warning':question.statusCode==2, 'badge-success':question.statusCode==3}\">Optional</span>"+
                        "</div>"+
                    "</ng-form>",

        scope: {
            index: '=',
            question: '='
        },
        controller: function ($scope, $location, $resource, $routeParams, Restangular, Modal)
        {
            $scope.saving = false;
            $scope.save = function (question, choice, part)
            {
                $scope.saving = true;
                if ($scope.question.isMultiple) {
                    var identifier = question.id + "-" + choice.id + "-" + part.id;
                } else {
                    var identifier = question.id + "-" + part.id;
                }

                var newAnswer = $scope.index[identifier];
                newAnswer.valueChoice = choice;
                newAnswer.valuePercent = choice.value;


                // if id setted on radio button, update
                if (newAnswer.id && !$scope.question.isMultiple) {
                    newAnswer.put().then(function ()
                    {
                        $scope.saving = false;
                        QuestionAssistant.updateQuestion(question, $scope.index, false, true);
                    });

                    // if id is setted on checkbox element, that means that there already is a result and we want to remove it
                } else if (newAnswer.id && $scope.question.isMultiple) {
                    newAnswer.remove().then(function ()
                    {
                        $scope.index[identifier] = QuestionAssistant.getEmptyChoiceAnswer(question, part, choice);
                        $scope.saving = false;
                        QuestionAssistant.updateQuestion(question, $scope.index, false, true);
                    });

                    // if don't exists -> create
                } else if (!newAnswer.id) {
                    Restangular.all('answer').post(newAnswer).then(function (answer)
                    {
                        if ($scope.question.isMultiple) {
                            answer.isCheckboxChecked = true;
                        }
                        $scope.index[identifier] = answer;
                        $scope.saving = false;
                        QuestionAssistant.updateQuestion(question, $scope.index, false, true);
                    });
                }

            }


        }
    }
});

@@ -280,28 +280,46 @@
      %22%3Ctable
+ class='show-grid'
 %3E%22+%0A
-
             
@@ -2078,32 +2078,543 @@
        %22%3C/tr%3E%22+%0A
+                                %22%3Ctr ng-show='!question.isMultiple'%3E%22 +%0A                                %22   %3Ctd ng-repeat='part in question.parts' class='text-center'%3E%22 +%0A                                %22       %3Cspan class='btn btn-xs btn-danger' ng-click='index%5Bquestion.id+%5C%22-%5C%22+part.id%5D.valueChoice.id=null; save(question,null,part)'%3E%3Ci class='fa fa-trash-o'%3E%3C/i%3E%3C/span%3E%22 +%0A                                %22   %3C/td%3E%22 +%0A                                %22   %3Ctd%3E%3C/td%3E%22+%0A                                %22%3C/tr%3E%22+%0A
                 
@@ -3769,16 +3769,49 @@
 choice;%0A
+                if(choice) %7B%0A    
         
@@ -3857,16 +3857,33 @@
 .value;%0A
+                %7D
 %0A%0A      
@@ -4496,36 +4496,16 @@
 ction ()
-%0A                   
  %7B%0A     
@@ -4773,32 +4773,456 @@
            %7D);%0A%0A
+                %7D else if (newAnswer.id && !$scope.question.isMultiple && !choice ) %7B%0A                    newAnswer.remove().then(function () %7B%0A                        $scope.index%5Bidentifier%5D = QuestionAssistant.getEmptyChoiceAnswer(question, part, null);%0A                        $scope.saving = false;%0A                        QuestionAssistant.updateQuestion(question, $scope.index, false, true);%0A                    %7D);%0A%0A
                 
@@ -5785,33 +5785,32 @@
               %7D%0A
-%0A
             %7D%0A%0A%0A
@@ -5808,17 +5808,16 @@
      %7D%0A%0A
-%0A
         

Allow to remove answers on non-multiple question choices