/*
table edit modal component , display modal form if the show property is true
displayOption:
{
    show: true/false,
}
or
    2015/3/11 weita : this directive is no longer responsible for create save/cancel button,
                      so I added a new feature that allows the caller pass bool var for display-option

    displayOption : true/false

passedForm : transclude html ngForm object, define in html. reference: https://docs.angularjs.org/api/ng/directive/form
*/
angular.module('View.Directive')
    .directive('tableEditModal', function (CONSTANTS) {
        return {
            restrict: 'E',
            scope: {
                displayOption: '=displayOption',
                passedForm: '='
            },
            transclude: true,
            controller: function ($scope) {
                $scope.close = function () {
                    if ($scope.displayOption == true) {
                        $scope.displayOption = false;
                    } else {
                        $scope.displayOption.show = false;
                    }
                }
            },
            link: function (scope, element, attrs) {

                // using $watch to find modal opening event
                scope.$watch('displayOption', function (newValue, oldValue) {

                    if (scope.displayOption && (scope.displayOption.show == true || scope.displayOption == true)) {

                        // add class 'modal-open' to <body> if a modal is opening
                        angular.element('body').addClass('modal-open');

                    } else if (scope.displayOption == false || (scope.displayOption && scope.displayOption.show == false)) {

                        // remove class 'modal-open' from <body> if a modal is colsing
                        angular.element('body').removeClass('modal-open');

                    }

                    // Reset form status by $setPristine 
                    if (scope.passedForm) {
                        scope.passedForm.$setPristine();
                    }

                }, true)
            },
            templateUrl: CONSTANTS.TB + 'table_edit'
        }
    })
    .directive('selectRow', function () {//
        return {
            controller: function ($scope) {
                $scope.selectRow = {};
                $scope.selectRow.toBeEdit = {};
                var _selected = null;
                var _selectedParent = null;
                var _selectedArray = null;
                $scope.selectRow.select = function (row, parent, inArray) {
                    if (row) {
                        _selected = row;
                    }
                    if (parent) {
                        _selectedParent = parent;
                    }
                    if (inArray) {
                        _selectedArray = inArray;
                    }
                }
                $scope.selectRow.getSelected = function () { return _selected; }
                $scope.selectRow.getParent = function () { return _selectedParent; }
                $scope.selectRow.getSelectedArray = function () { return _selectedArray; };
                $scope.selectRow.getNextObj = function () {
                    return null;
                }
                $scope.selectRow.unSelect = function () { _selected = null; }
            }
        };
    })