'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

var _reactIntl = require('react-intl');

var _ReportModal = require('./ReportModal');

var _ReportModal2 = _interopRequireDefault(_ReportModal);

var _permissionHelpers = require('../../helpers/permissionHelpers');

var _reportHelpers = require('../../helpers/reportHelpers');

var _recordDataHelpers = require('../../helpers/recordDataHelpers');

var _SearchPanelContainer = require('../../containers/search/SearchPanelContainer');

var _SearchPanelContainer2 = _interopRequireDefault(_SearchPanelContainer);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /* global window */

var messages = (0, _reactIntl.defineMessages)({
  title: {
    'id': 'recordReportPanel.title',
    'defaultMessage': 'Reports'
  }
});

var getSearchDescriptor = function getSearchDescriptor(objectName) {
  return _immutable2.default.fromJS({
    recordType: 'report',
    searchQuery: {
      p: 0,
      size: 5,
      doctype: objectName
    }
  });
};

var propTypes = {
  color: _propTypes2.default.string,
  config: _propTypes2.default.object,
  csid: _propTypes2.default.string,
  perms: _propTypes2.default.instanceOf(_immutable2.default.Map),
  recordData: _propTypes2.default.instanceOf(_immutable2.default.Map),
  recordType: _propTypes2.default.string
};

var RecordReportPanel = function (_Component) {
  _inherits(RecordReportPanel, _Component);

  function RecordReportPanel(props) {
    _classCallCheck(this, RecordReportPanel);

    var _this = _possibleConstructorReturn(this, (RecordReportPanel.__proto__ || Object.getPrototypeOf(RecordReportPanel)).call(this, props));

    _this.handleItemClick = _this.handleItemClick.bind(_this);
    _this.handleModalCancelButtonClick = _this.handleModalCancelButtonClick.bind(_this);
    _this.handleModalCloseButtonClick = _this.handleModalCloseButtonClick.bind(_this);
    _this.handleModalRunButtonClick = _this.handleModalRunButtonClick.bind(_this);
    _this.handleSearchDescriptorChange = _this.handleSearchDescriptorChange.bind(_this);

    var _this$props = _this.props,
        config = _this$props.config,
        recordType = _this$props.recordType;


    var objectName = (0, _get2.default)(config, ['recordTypes', recordType, 'serviceConfig', 'objectName']);

    _this.state = {
      searchDescriptor: getSearchDescriptor(objectName)
    };
    return _this;
  }

  _createClass(RecordReportPanel, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      var recordType = this.props.recordType;
      var config = nextProps.config,
          nextRecordType = nextProps.recordType;


      if (nextRecordType !== recordType) {
        var nextObjectName = (0, _get2.default)(config, ['recordTypes', nextRecordType, 'serviceConfig', 'objectName']);

        this.setState({
          searchDescriptor: getSearchDescriptor(nextObjectName)
        });
      }
    }
  }, {
    key: 'handleItemClick',
    value: function handleItemClick(item) {
      this.setState({
        isModalOpen: true,
        selectedItem: item
      });
    }
  }, {
    key: 'handleModalCancelButtonClick',
    value: function handleModalCancelButtonClick() {
      this.setState({
        isModalOpen: false
      });
    }
  }, {
    key: 'handleModalCloseButtonClick',
    value: function handleModalCloseButtonClick() {
      this.setState({
        isModalOpen: false
      });
    }
  }, {
    key: 'handleModalRunButtonClick',
    value: function handleModalRunButtonClick() {
      var _props = this.props,
          config = _props.config,
          csid = _props.csid,
          recordType = _props.recordType;
      var selectedItem = this.state.selectedItem;


      var viewerPath = (0, _reportHelpers.getReportViewerPath)(selectedItem, config, recordType, csid);

      window.open(viewerPath, _reportHelpers.VIEWER_WINDOW_NAME);

      this.setState({
        isModalOpen: false
      });
    }
  }, {
    key: 'handleSearchDescriptorChange',
    value: function handleSearchDescriptorChange(searchDescriptor) {
      this.setState({
        searchDescriptor: searchDescriptor
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          color = _props2.color,
          config = _props2.config,
          csid = _props2.csid,
          perms = _props2.perms,
          recordData = _props2.recordData,
          recordType = _props2.recordType;
      var _state = this.state,
          isModalOpen = _state.isModalOpen,
          searchDescriptor = _state.searchDescriptor,
          selectedItem = _state.selectedItem;


      if (!(0, _recordDataHelpers.isExistingRecord)(recordData)) {
        // Don't render until after the record has loaded.

        return null;
      }

      if (!(0, _permissionHelpers.canList)('report', perms)) {
        return null;
      }

      var canRun = (0, _permissionHelpers.canCreate)('report', perms);

      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(_SearchPanelContainer2.default, {
          collapsed: true,
          color: color,
          config: config,
          csid: csid,
          linkItems: false,
          name: 'recordReportPanel',
          searchDescriptor: searchDescriptor,
          recordType: recordType,
          showSearchButton: false,
          title: _react2.default.createElement(_reactIntl.FormattedMessage, messages.title),
          onItemClick: canRun ? this.handleItemClick : undefined,
          onSearchDescriptorChange: this.handleSearchDescriptorChange
        }),
        _react2.default.createElement(_ReportModal2.default, {
          isOpen: isModalOpen,
          reportItem: selectedItem,
          onCancelButtonClick: this.handleModalCancelButtonClick,
          onCloseButtonClick: this.handleModalCloseButtonClick,
          onRunButtonClick: this.handleModalRunButtonClick
        })
      );
    }
  }]);

  return RecordReportPanel;
}(_react.Component);

exports.default = RecordReportPanel;


RecordReportPanel.propTypes = propTypes;