export const CostsReportsDetailComponent = {
  bindings: {
    transition: '<'
  },
  template: require('./costs-reports-detail.html'),
  controller: CostsReportsDetailController
};

function CostsReportsDetailController($mdDialog, $state, $q, CostsReports, Projects, roleCheckerService, logger) {
  'ngInject';

  const ctrl = this;

  const id = ctrl.transition.params().id;

  ctrl.loading = true;
  ctrl.isAdmin = false;
  ctrl.report = null;
  ctrl.excludedProjects = [];

  ctrl.deleteReport = deleteReport;

  init();

  function init() {
    loadAdminStatus()
      .then(loadCostReport);
  }

  function loadAdminStatus() {
    return roleCheckerService
      .hasHubRole('admin')
      .then(hasRole => {
        ctrl.isAdmin = hasRole;
      });
  }

  function loadCostReport() {
    return CostsReports
      .get(id)
      .then(report => {
        ctrl.report = report;

        return prepareExcludedProjects(report.config);
      })
      .finally(() => {
        ctrl.loading = false;
      });
  }

  function prepareExcludedProjects(config) {
    if (config.excluded_projects) {
      const promises = config.excluded_projects.map(Projects.get);
      return $q
        .all(promises)
        .then(projects => {
          angular.copy(projects, ctrl.excludedProjects);
        });
    }
    return $q.when([]);
  }

  function deleteReport(targetEvent) {
    if (!ctrl.isAdmin) {
      return;
    }

    const confirm = $mdDialog.confirm()
      .title('Are you sure?')
      .textContent(`This will delete the costs report '${id}'. You can still recreate it later as long as the source data is still available.`)
      .ariaLabel('Confirm deletion of costs report')
      .targetEvent(targetEvent)
      .ok('Do it')
      .cancel('Cancel');

    $mdDialog
      .show(confirm)
      .then(() => {
        ctrl.loading = true;

        CostsReports
          .delete(ctrl.report.id)
          .then(() => {
            logger.success('Costs report deleted');
            $state.go('costs-reports.list');
          })
          .finally(() => {
            ctrl.loading = false;
          });
      });
  }
}

@@ -236,12 +236,8 @@
 ate,
- $q,
  Cos
@@ -287,16 +287,16 @@
 gger) %7B%0A
+
   'ngInj
@@ -439,38 +439,8 @@
 ull;
-%0A  ctrl.excludedProjects = %5B%5D;
 %0A%0A  
@@ -847,64 +847,8 @@
 ort;
-%0A%0A        return prepareExcludedProjects(report.config);
 %0A   
@@ -902,24 +902,24 @@
 ng = false;%0A
+
       %7D);%0A  
@@ -925,327 +925,8 @@
  %7D%0A%0A
-  function prepareExcludedProjects(config) %7B%0A    if (config.excluded_projects) %7B%0A      const promises = config.excluded_projects.map(Projects.get);%0A      return $q%0A        .all(promises)%0A        .then(projects =%3E %7B%0A          angular.copy(projects, ctrl.excludedProjects);%0A        %7D);%0A    %7D%0A    return $q.when(%5B%5D);%0A  %7D%0A%0A
   fu

remove unused code in costs report detail view