'use strict';
/**
 * @ngdoc directive
 * @name eventDelete
 * @description
 *  system-overview 화면에서 scheduled event 삭제
 */
app.directive('eventDelete', function ($http, $templateCache, $state, $modal,  toastr, Systems, Rooms) {

  return {
    restrict: 'EA',
    replace: true,
    scope: {
      system: '=',
      room: '=',
      event: '='
    },

    link: function (scope, element, attrs) {
      var modal = $modal({
        scope: scope,
        title: 'Delete this event',
        template: 'delete-event.html',
        container: '#system-overview',
        show: false
      });

      element.on('click', function(event) {
        modal.$promise.then(modal.show);
      });

      scope.deleteEvent = function(id) {
        var jsonData = JSON.stringify({ id: id });

        // scheduled event 삭제
        Rooms.deleteEvent(scope.system.id, scope.room.id, jsonData)
          .then(function(data) {
            Systems.updateSystem(scope.system.id).then(function () {
              $state.transitionTo($state.current, null, { reload: true, inherit: true, notify: true  });
            });
          })
          .catch(toastr.error);
      };
    }
  };
});




















