var DataManagement = angular.module('DataManagement',["xeditable"]);
DataManagement.run(function(editableOptions) {
  editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default'
});
DataManagement.controller('MainController', function ($scope,$http) {

  $scope.create_data = new Object();
  $scope.update_data = new Object();
  $scope.delete_data = new Object();
  
  $scope.read_data = new Array();

  $scope.query_data = new Object();

  $scope.data_services =
  [
    {
      name:"MySQL",
      value:"mysql"
    },
    {
      name:"MongoDB",
      value:"mongodb"
    }
  ];

  $scope.data_service = {
    name:"MongoDB",
    value:"mongodb"
  };

  $scope.export_to_csv = function() {
    location.href = "api/v1/example/" + $scope.data_service.value.toLowerCase()  + "/json_to_csv";
  };

  $scope.download_config = function() {
    $http({method: 'GET', url:"api/v1/example/" + $scope.data_service.value.toLowerCase()  + "/config", data:$scope.config, headers: {'Content-Type': 'application/json'}  }).success(function(data)
    {
        $scope.config = data;
    });
  };
  $scope.upload_config = function() {
    $http({method: 'POST', url:"api/v1/example/" + $scope.data_service.value.toLowerCase() + "/config", data:$scope.config, headers: {'Content-Type': 'application/json'}  }).success(function(data)
    {

    });
  };
  $scope.download_config();

  $scope.read = function() {  
    $http({method: 'GET', url:"api/v1/example/" + $scope.data_service.value.toLowerCase()  + "?ad_id=" + $scope.query_data.ad_id, headers: {'Content-Type': 'application/json'}  }).success(function(data)
    {
      $scope.read_data = data;
    });
  };
  $scope.create = function() {  
    $http({method: 'POST', url:"api/v1/example/" + $scope.data_service.value.toLowerCase()  + "", data:$scope.create_data, headers: {'Content-Type': 'application/json'} }).success(function(data)
    {
      $scope.refresh();
    });
  };
  $scope.update = function(id) {  
    $scope.update_data = $scope.read_data[id];
    $http({method: 'PUT', url:"api/v1/example/" + $scope.data_service.value.toLowerCase()  + "", data:$scope.update_data, headers: {'Content-Type': 'application/json'} }).success(function(data)
    {
      $scope.refresh();
    });
  };
  $scope.delete = function(id) {  
    $scope.delete_data = $scope.read_data[id];
    $http({method: 'DELETE', url:"api/v1/example/" + $scope.data_service.value.toLowerCase()  + "", data:$scope.delete_data, headers: {'Content-Type': 'application/json'} }).success(function(data)
    {
      $scope.refresh();
    });
  };
  $scope.refresh = function() {
    $scope.read();
  }
});

