"use strict";

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

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

var _printableItinerary = _interopRequireDefault(require("@opentripplanner/printable-itinerary"));

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

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

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

var callTakerActions = _interopRequireWildcard(require("../../actions/call-taker"));

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

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

var _contexts = require("../../util/contexts");

var _print = require("../../util/print");

var _printStyled = require("./print-styled");

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; }

/**
 * Component that renders the print version of field trip itineraries.
 */
class PrintFieldTripLayout extends _react.Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "_print", () => {
      window.print();
    });
  }

  componentDidMount() {
    const {
      initializeModules
    } = this.props; // Add print-view class to html tag to ensure that iOS scroll fix only applies
    // to non-print views.

    (0, _print.addPrintViewClassToRootHtml)(); // Load call-taker/field-trip functionality (performs a fetch).

    initializeModules();
  }

  componentDidUpdate(prevProps) {
    const {
      fetchFieldTripDetails,
      receivedFieldTrips,
      request,
      requestId,
      session
    } = this.props;

    if (!prevProps.session && session) {
      // When session is set,
      // create a placeholder in the calltaker redux state that has just one request
      // for fetching/receiving the details of the field trip per request id.
      receivedFieldTrips({
        fieldTrips: [{
          endTime: 0,
          id: requestId
        }]
      });
      fetchFieldTripDetails(requestId);
    }

    if (request && request !== prevProps.request) {
      // Set window title when request has fully loaded
      // (appears in print headings)
      const {
        endLocation,
        schoolName
      } = request;
      document.title = `Field Trip: ${schoolName} to ${endLocation}`;
    }
  }

  componentWillUnmount() {
    (0, _print.clearClassFromRootHtml)();
  }

  render() {
    const {
      config,
      request
    } = this.props;
    const {
      LegIcon
    } = this.context;
    if (!request) return null;
    const {
      address,
      classpassId,
      emailAddress,
      endLocation,
      faxNumber,
      grade,
      numChaperones,
      numFreeStudents,
      numStudents,
      phoneNumber,
      schoolName,
      teacherName,
      timeStamp
    } = request; // Outbound/inbound template

    const tripStructure = [{
      title: 'Outbound Trip (to Destination)',
      trip: (0, _callTaker2.getTripFromRequest)(request, true),
      tripAbsentMessage: 'No Outbound Trip Planned'
    }, {
      title: 'Inbound Trip (from Destination)',
      trip: (0, _callTaker2.getTripFromRequest)(request, false),
      tripAbsentMessage: 'No Inbound Trip Planned'
    }];
    return /*#__PURE__*/_react.default.createElement(_printStyled.PrintLayout, null, /*#__PURE__*/_react.default.createElement(_printStyled.Header, null, /*#__PURE__*/_react.default.createElement(_reactBootstrap.Button, {
      bsSize: "small",
      onClick: this._print,
      style: {
        float: 'right'
      }
    }, /*#__PURE__*/_react.default.createElement("i", {
      className: "fa fa-print"
    }), " Print"), /*#__PURE__*/_react.default.createElement(_printStyled.TripTitle, null, "Field Trip Plan: ", schoolName, " to ", endLocation)), /*#__PURE__*/_react.default.createElement(_printStyled.TripInfoList, null, /*#__PURE__*/_react.default.createElement("li", null, /*#__PURE__*/_react.default.createElement("b", null, "Teacher"), ": ", /*#__PURE__*/_react.default.createElement(_printStyled.Val, null, teacherName), " (", schoolName, ", Grade: ", /*#__PURE__*/_react.default.createElement(_printStyled.Val, null, grade), ")"), /*#__PURE__*/_react.default.createElement("li", null, /*#__PURE__*/_react.default.createElement("b", null, "Teacher Address"), ": ", /*#__PURE__*/_react.default.createElement(_printStyled.Val, null, address)), /*#__PURE__*/_react.default.createElement("li", null, /*#__PURE__*/_react.default.createElement("b", null, "Phone"), ": ", /*#__PURE__*/_react.default.createElement(_printStyled.Val, null, phoneNumber), " / ", /*#__PURE__*/_react.default.createElement("b", null, "Fax"), ": ", /*#__PURE__*/_react.default.createElement(_printStyled.Val, null, faxNumber)), /*#__PURE__*/_react.default.createElement("li", null, /*#__PURE__*/_react.default.createElement("b", null, "Email"), ": ", /*#__PURE__*/_react.default.createElement(_printStyled.Val, null, emailAddress)), /*#__PURE__*/_react.default.createElement("li", null, /*#__PURE__*/_react.default.createElement("b", null, "Students Age 7 and Over"), ": ", numStudents || 0), /*#__PURE__*/_react.default.createElement("li", null, /*#__PURE__*/_react.default.createElement("b", null, "Students Age 6 and Under"), ": ", numFreeStudents || 0), /*#__PURE__*/_react.default.createElement("li", null, /*#__PURE__*/_react.default.createElement("b", null, "Chaperones"), ": ", numChaperones || 0), classpassId && /*#__PURE__*/_react.default.createElement("li", null, /*#__PURE__*/_react.default.createElement("b", null, "Class Pass Hop Card #"), ": ", classpassId), /*#__PURE__*/_react.default.createElement("li", null, /*#__PURE__*/_react.default.createElement("i", null, "Request submitted: ", timeStamp))), tripStructure.map(({
      title,
      trip,
      tripAbsentMessage
    }, i) => {
      var _trip$groupItinerarie;

      return /*#__PURE__*/_react.default.createElement(_printStyled.TripContainer, {
        key: i
      }, /*#__PURE__*/_react.default.createElement("h2", null, title), trip ? (_trip$groupItinerarie = trip.groupItineraries) === null || _trip$groupItinerarie === void 0 ? void 0 : _trip$groupItinerarie.map((groupItin, i) => {
        const itinerary = JSON.parse((0, _callTaker2.lzwDecode)(groupItin.itinData));
        return /*#__PURE__*/_react.default.createElement(_printStyled.TripBody, {
          key: i
        }, /*#__PURE__*/_react.default.createElement(_printStyled.ItineraryContainer, null, /*#__PURE__*/_react.default.createElement("h3", null, groupItin.passengers, " passengers on following itinerary:"), /*#__PURE__*/_react.default.createElement(_printStyled.PrintableItineraryContainer, null, /*#__PURE__*/_react.default.createElement(_printableItinerary.default, {
          config: config,
          itinerary: itinerary,
          LegIcon: LegIcon
        }), /*#__PURE__*/_react.default.createElement(_printStyled.TripSummary, {
          itinerary: itinerary
        }))));
      }) : /*#__PURE__*/_react.default.createElement(_printStyled.TripBody, null, /*#__PURE__*/_react.default.createElement("i", null, tripAbsentMessage)));
    }));
  }

} // connect to the redux store


_defineProperty(PrintFieldTripLayout, "contextType", _contexts.ComponentContext);

const mapStateToProps = (state, ownProps) => {
  const requestId = parseInt(state.router.location.query.requestId);
  const {
    requests
  } = state.callTaker.fieldTrip;
  const request = requests.data.find(req => req.id === requestId);
  return {
    config: state.otp.config,
    request,
    requestId,
    session: state.callTaker.session
  };
};

const mapDispatchToProps = {
  fetchFieldTripDetails: fieldTripActions.fetchFieldTripDetails,
  initializeModules: callTakerActions.initializeModules,
  receivedFieldTrips: fieldTripActions.receivedFieldTrips
};

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

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

//# sourceMappingURL=print-field-trip-layout.js