"use strict";

require("core-js/modules/web.dom.iterable");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _time = require("@opentripplanner/core-utils/lib/time");

var _moment = _interopRequireDefault(require("moment"));

var _react = _interopRequireWildcard(require("react"));

var _reactBootstrap = require("react-bootstrap");

var _reactRedux = require("react-redux");

var _styledComponents = _interopRequireDefault(require("styled-components"));

var fieldTripActions = _interopRequireWildcard(require("../../actions/field-trip"));

var _callTaker = require("../../util/call-taker");

var _icon = _interopRequireDefault(require("../util/icon"));

var _draggableWindow = _interopRequireDefault(require("./draggable-window"));

var _editableSection = _interopRequireDefault(require("./editable-section"));

var _fieldTripNotes = _interopRequireDefault(require("./field-trip-notes"));

var _styled = require("./styled");

var _tripStatus = _interopRequireDefault(require("./trip-status"));

var _updatable = _interopRequireDefault(require("./updatable"));

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const WindowHeader = (0, _styledComponents.default)(_styled.WindowHeader)`
  margin-bottom: 0px;
`;
/**
 * Shows the details for the active Field Trip Request.
 */

class FieldTripDetails extends _react.Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "_editSubmitterNotes", val => this.props.editSubmitterNotes(this.props.request, val));

    _defineProperty(this, "_getRequestLink", (path, isPublic = false) => `${this.props.datastoreUrl}/${isPublic ? 'public/' : ''}fieldtrip/${path}?requestId=${this.props.request.id}`);

    _defineProperty(this, "_onToggleStatus", () => {
      const {
        request,
        setRequestStatus
      } = this.props;

      if (request.status !== 'cancelled') {
        if (confirm('Are you sure you want to cancel this request? Any associated trips will be deleted.')) {
          setRequestStatus(request, 'cancelled');
        }
      } else {
        setRequestStatus(request, 'active');
      }
    });

    _defineProperty(this, "_renderFooter", () => {
      const {
        request,
        sessionId
      } = this.props;
      const cancelled = request.status === 'cancelled';
      const printFieldTripLink = `/#/printFieldTrip/?requestId=${request.id}&sessionId=${sessionId}`;
      return /*#__PURE__*/_react.default.createElement("div", {
        style: {
          padding: '5px 10px 0px 10px'
        }
      }, /*#__PURE__*/_react.default.createElement(_reactBootstrap.DropdownButton, {
        "aria-label": "Field Trip Menu",
        bsSize: "xsmall",
        dropup: true,
        id: "field-trip-menu",
        title: "View"
      }, /*#__PURE__*/_react.default.createElement(_reactBootstrap.MenuItem, {
        href: this._getRequestLink('feedbackForm', true),
        target: "_blank"
      }, /*#__PURE__*/_react.default.createElement(_icon.default, {
        type: "commenting-o"
      }), " Feedback link"), /*#__PURE__*/_react.default.createElement(_reactBootstrap.MenuItem, {
        href: this._getRequestLink('receipt'),
        target: "_blank"
      }, /*#__PURE__*/_react.default.createElement(_icon.default, {
        type: "file-text-o"
      }), " Receipt link"), /*#__PURE__*/_react.default.createElement(_reactBootstrap.MenuItem, {
        href: printFieldTripLink,
        target: "_blank"
      }, /*#__PURE__*/_react.default.createElement(_icon.default, {
        type: "print"
      }), " Printable trip plan")), /*#__PURE__*/_react.default.createElement(_styled.Button, {
        bsSize: "xsmall",
        bsStyle: cancelled ? undefined : 'danger',
        className: "pull-right",
        onClick: this._onToggleStatus
      }, cancelled ? 'Reactivate' : 'Cancel', " request"));
    });

    _defineProperty(this, "_renderHeader", () => {
      const {
        dateFormat,
        request
      } = this.props;
      const {
        id,
        schoolName,
        travelDate
      } = request;
      const travelDateAsMoment = (0, _moment.default)(travelDate);
      return /*#__PURE__*/_react.default.createElement(WindowHeader, null, /*#__PURE__*/_react.default.createElement(_icon.default, {
        type: "graduation-cap"
      }), " ", schoolName, " Trip (#", id, ")", /*#__PURE__*/_react.default.createElement("div", {
        style: {
          marginLeft: '28px'
        }
      }, /*#__PURE__*/_react.default.createElement("small", null, "Travel date: ", travelDateAsMoment.format(dateFormat), ' ', "(", travelDateAsMoment.fromNow(), ")")));
    });
  }

  render() {
    const {
      addFieldTripNote,
      clearActiveFieldTrip,
      deleteFieldTripNote,
      request,
      setRequestGroupSize,
      setRequestPaymentInfo,
      style
    } = this.props;
    if (!request) return null;
    const {
      invoiceRequired,
      notes,
      schoolName,
      submitterNotes,
      teacherName,
      ticketType
    } = request;
    const internalNotes = [];
    const operationalNotes = [];
    notes && notes.forEach(note => {
      if (note.type === 'internal') internalNotes.push(note);else operationalNotes.push(note);
    });
    return /*#__PURE__*/_react.default.createElement(_draggableWindow.default, {
      footer: this._renderFooter(),
      header: this._renderHeader(),
      height: "375px",
      onClickClose: clearActiveFieldTrip,
      style: style
    }, /*#__PURE__*/_react.default.createElement(_styled.Container, null, /*#__PURE__*/_react.default.createElement(_styled.Header, null, "Group Information"), /*#__PURE__*/_react.default.createElement(_styled.Half, null, /*#__PURE__*/_react.default.createElement(_styled.Para, null, /*#__PURE__*/_react.default.createElement(_styled.Bold, null, schoolName)), /*#__PURE__*/_react.default.createElement(_styled.Para, null, "Teacher: ", teacherName), /*#__PURE__*/_react.default.createElement(_styled.Para, null, "Ticket type: ", /*#__PURE__*/_react.default.createElement(_styled.Val, null, _callTaker.TICKET_TYPES[ticketType])), /*#__PURE__*/_react.default.createElement(_styled.Para, null, "Invoice required: ", /*#__PURE__*/_react.default.createElement(_styled.Val, null, invoiceRequired ? 'Yes' : 'No')), /*#__PURE__*/_react.default.createElement(_styled.Para, null, /*#__PURE__*/_react.default.createElement(_updatable.default, {
      fieldName: "Teacher notes",
      label: /*#__PURE__*/_react.default.createElement(_icon.default, {
        title: "Teacher notes",
        type: "sticky-note-o"
      }),
      onUpdate: this._editSubmitterNotes,
      value: submitterNotes
    }))), /*#__PURE__*/_react.default.createElement(_styled.Half, null, /*#__PURE__*/_react.default.createElement(_editableSection.default, {
      children: /*#__PURE__*/_react.default.createElement(_styled.Text, null, /*#__PURE__*/_react.default.createElement(_styled.Bold, null, (0, _callTaker.getGroupSize)(request)), " total"),
      fields: _callTaker.GROUP_FIELDS,
      inputStyle: {
        lineHeight: '0.8em',
        padding: '0px',
        width: '50px'
      },
      onChange: setRequestGroupSize,
      request: request,
      valueFirst: true
    })), /*#__PURE__*/_react.default.createElement(_tripStatus.default, {
      outbound: true,
      request: request
    }), /*#__PURE__*/_react.default.createElement(_tripStatus.default, {
      request: request
    }), /*#__PURE__*/_react.default.createElement(_styled.FullWithMargin, null, /*#__PURE__*/_react.default.createElement(_editableSection.default // Use inline header so that 'Change' button appears on top line.
    , {
      children: /*#__PURE__*/_react.default.createElement(_styled.InlineHeader, null, /*#__PURE__*/_react.default.createElement(_icon.default, {
        type: "credit-card"
      }), " Payment information"),
      fields: _callTaker.PAYMENT_FIELDS,
      inputStyle: {
        lineHeight: '0.8em',
        padding: '0px',
        width: '100px'
      },
      onChange: setRequestPaymentInfo,
      request: request
    })), /*#__PURE__*/_react.default.createElement(_fieldTripNotes.default, {
      addFieldTripNote: addFieldTripNote,
      deleteFieldTripNote: deleteFieldTripNote,
      request: request
    })));
  }

}

const mapStateToProps = (state, ownProps) => {
  return {
    currentQuery: state.otp.currentQuery,
    datastoreUrl: state.otp.config.datastoreUrl,
    dateFormat: (0, _time.getDateFormat)(state.otp.config),
    request: (0, _callTaker.getActiveFieldTripRequest)(state),
    sessionId: state.callTaker.session.sessionId
  };
};

const mapDispatchToProps = {
  addFieldTripNote: fieldTripActions.addFieldTripNote,
  clearActiveFieldTrip: fieldTripActions.clearActiveFieldTrip,
  deleteFieldTripNote: fieldTripActions.deleteFieldTripNote,
  editSubmitterNotes: fieldTripActions.editSubmitterNotes,
  fetchQueries: fieldTripActions.fetchQueries,
  setActiveFieldTrip: fieldTripActions.setActiveFieldTrip,
  setRequestGroupSize: fieldTripActions.setRequestGroupSize,
  setRequestPaymentInfo: fieldTripActions.setRequestPaymentInfo,
  setRequestStatus: fieldTripActions.setRequestStatus,
  toggleFieldTrips: fieldTripActions.toggleFieldTrips
};

var _default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(FieldTripDetails);

exports.default = _default;
module.exports = exports.default;

//# sourceMappingURL=field-trip-details.js