///define(['app'], function (app) {
(function() {
  var app = angular.module('myApp');
  var cId = 'ReportViewer';
  var cVer = 1;
  ///app.register.controller(cId, [
  app.controller(cId, [
    '$scope', '$rootScope', '$http', '$window', '$location', 'VS4Library', 'myFactory',
    function($scope, $rootScope, $http, $window, $location, VS4Library, myFactory) {
      var self = this;

      $rootScope.controllerMsg = cId + ' : ' + myFactory.AppVersion() + '.' + cVer;

      $scope.reportServiceUrl = $rootScope.apiBase + '/api/ReportApi';
      $scope.path = $location.search().itemPath;
      $scope.mode = ej.ReportViewer.ProcessingMode.Local;
      $scope.printMode = true;
      $scope.reportQuery = $window.sessionStorage.reportQuery;
      $scope.reportConditions = $window.sessionStorage.reportConditions;
      $scope.printOption = ej.ReportViewer.PrintOptions.NewTab;

      $scope.onReportPrint = function(args) {
        args.printWind.printDelay = 3000;
      };

      // $scope.parameters: [{
      //   name: 'InvoiceID',
      //   labels: ['InvoiceID'],
      //   values: [10250],
      //   nullable: false
      // }]
      // $scope.toolbarSettings = {
      //   items: ej.ReportViewer.ToolbarItems.All & ~ej.ReportViewer.ToolbarItems.Parameters
      // };

			// if ($rootScope.ActiveUser = "admin"){$scope.showQuery = true;}else{$scope.showQuery = false;}

      $scope.showQuery = false;
      $scope.toggleQuery = function() {
        $scope.showQuery = !$scope.showQuery;
      };

      $scope.showCondition = false;
      $scope.toggleCondition = function() {
        $scope.showCondition = !$scope.showCondition;
      };


    }
  ]);


  var _params = null;
  var _sqlQuery = window.sessionStorage.reportQuery;
  var _conditions = window.sessionStorage.reportConditions;

  ej.ReportViewer.prototype._viewReportParamsClick = function(event) {
    var proxy = $('#container').data('ejReportViewer');

    var parameters = event.data.params;
    var reportParams = proxy._getParameterJson(parameters);
    proxy._refresh = true;
    $('#' + proxy._id + '_viewBlockContainer .e-reportviewer-viewerblockcontent table:first').attr('isviewclick', 'true');
    _params = reportParams;
    proxy._refreshReport();
  }

  ej.ReportViewer.prototype.doAjaxPost = function(type, url, jsondata, onSuccess) {
    var proxy = $('#container').data('ejReportViewer');
    var inVokemethod = onSuccess;

    $.ajax({
      type: type,
      url: url,
      crossDomain: true,
      contentType: 'application/json; charset=utf-8',
      dataType: 'json',
      data: jsondata,
      beforeSend: function(req) {
        if (inVokemethod == "_getDataSourceCredential" || inVokemethod == "_reportLoaded") {
          var _json = jQuery.parseJSON(this.data);
          if (_params != null) {
            _json["params"] = _params;
          }
          _json["sqlMain"] = _sqlQuery;
          _json["conditions"] = _conditions;
          this.data = JSON.stringify(_json);
        }

        if (inVokemethod == "_getPageModel" || inVokemethod == "_getPreviewModel") {
          if (!proxy._isToolbarClick) {
            proxy._showloadingIndicator(true);
            proxy._updateDatasource = true;
          } else {
            proxy._showNavigationIndicator(true);
          }
        }
        req.setRequestHeader('ejAuthenticationToken', proxy._authenticationToken);
      },
      success: function(data) {
        if (data && typeof(data.Data) != "undefined") {
          data = data.Data;
        }
        if (typeof(data) == "string") {
          if (data.indexOf("Sf_Exception") != -1) {
            proxy._renderExcpetion(inVokemethod + ":" + data);
            return;
          }
        }
        proxy[inVokemethod](data);
      },
    });
  }



  ///});
})();
