"use strict";

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

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

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

var _qs = _interopRequireDefault(require("qs"));

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

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

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

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

var _loading = _interopRequireDefault(require("../narrative/loading"));

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

var _constants = require("../../util/constants");

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

var _fieldTripStatusIcon = _interopRequireDefault(require("./field-trip-status-icon"));

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

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

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

/**
 * Displays a searchable list of field trip requests in a draggable window.
 */
class FieldTripList extends _react.Component {
  constructor(props) {
    super(props);

    _defineProperty(this, "_onClickFieldTrip", request => {
      const {
        callTaker,
        fetchFieldTripDetails,
        setActiveFieldTrip
      } = this.props;

      if (request.id === callTaker.fieldTrip.activeId) {
        this._onCloseActiveFieldTrip();
      } else {
        setActiveFieldTrip(request.id);
        fetchFieldTripDetails(request.id);
      }
    });

    _defineProperty(this, "_onClickRefresh", () => this.props.fetchFieldTrips());

    _defineProperty(this, "_onCloseActiveFieldTrip", () => {
      this.props.setActiveFieldTrip(null);
    });

    _defineProperty(this, "_getReportUrl", () => {
      const {
        datastoreUrl
      } = this.props;
      const {
        date
      } = this.state;
      const [year, month, day] = date.split('-');
      const params = {
        day,
        month,
        year
      };
      return `${datastoreUrl}/fieldtrip/opsReport?${_qs.default.stringify(params)}`;
    });

    _defineProperty(this, "_handleSearchKeyUp", e => {
      const {
        callTaker,
        setFieldTripFilter
      } = this.props;
      const {
        search
      } = callTaker.fieldTrip.filter;
      const newSearch = e.target.value; // Update filter if Enter is pressed or search value is entirely cleared.

      const newSearchEntered = e.keyCode === 13 && newSearch !== search;
      const searchCleared = search && !newSearch;

      if (newSearchEntered || searchCleared) {
        setFieldTripFilter({
          search: newSearch
        });
      }
    });

    _defineProperty(this, "_onTabChange", e => {
      this.props.setFieldTripFilter({
        tab: e.currentTarget.name
      });
    });

    _defineProperty(this, "_updateReportDate", evt => this.setState({
      date: evt.target.value
    }));

    this.state = {
      date: (0, _moment.default)().startOf('day').format('YYYY-MM-DD')
    };
  }

  render() {
    const {
      callTaker,
      style,
      toggleFieldTrips,
      visibleRequests
    } = this.props;
    const {
      fieldTrip
    } = callTaker;
    const {
      activeId,
      filter
    } = fieldTrip;
    const {
      search
    } = filter;
    const {
      date
    } = this.state;
    return /*#__PURE__*/_react.default.createElement(_draggableWindow.default, {
      footer: /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("input", {
        className: "datetime-slim",
        onChange: this._updateReportDate,
        style: {
          border: 'none',
          fontSize: '14px',
          lineHeight: '1em',
          outline: 'none',
          width: '106px'
        },
        type: "date",
        value: date
      }), /*#__PURE__*/_react.default.createElement(_reactBootstrap.Button, {
        bsSize: "xsmall",
        href: this._getReportUrl(),
        target: "_blank"
      }, "View report")),
      header: /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_styled.WindowHeader, null, /*#__PURE__*/_react.default.createElement(_icon.default, {
        type: "graduation-cap"
      }), " Field Trip Requests", ' ', /*#__PURE__*/_react.default.createElement("span", {
        className: "pull-right"
      }, /*#__PURE__*/_react.default.createElement("button", {
        className: "clear-button-formatting",
        onClick: this._onClickRefresh,
        style: {
          marginRight: '5px',
          verticalAlign: 'bottom'
        }
      }, /*#__PURE__*/_react.default.createElement(_icon.default, {
        type: "refresh"
      })), /*#__PURE__*/_react.default.createElement("input", {
        defaultValue: search,
        onKeyUp: this._handleSearchKeyUp,
        placeholder: "Press Enter to Search",
        style: {
          fontSize: 'revert',
          fontWeight: 400,
          marginRight: '15px',
          width: '140px'
        }
      }))), _callTaker.TABS.map(tab => /*#__PURE__*/_react.default.createElement(Tab, {
        data: fieldTrip.requests.data,
        filter: filter,
        key: tab.id,
        onClick: this._onTabChange,
        tab: tab
      }))),
      onClickClose: toggleFieldTrips,
      style: style
    }, fieldTrip.requests.status === _constants.FETCH_STATUS.FETCHING ? /*#__PURE__*/_react.default.createElement(_loading.default, null) : visibleRequests.length > 0 ? visibleRequests.map((request, i) => /*#__PURE__*/_react.default.createElement(FieldTripRequestRecord, {
      active: activeId === request.id,
      key: request.id,
      onClick: this._onClickFieldTrip,
      request: request
    })) : /*#__PURE__*/_react.default.createElement("div", null, "No field trips found."));
  }

}

function Tab({
  data,
  filter,
  onClick,
  tab
}) {
  const count = _react.default.useMemo(() => data.filter(tab.filter).length, [data, tab]);

  const active = tab.id === filter.tab;
  const style = {
    backgroundColor: active ? 'navy' : undefined,
    borderRadius: 5,
    color: active ? 'white' : undefined,
    padding: '2px 3px'
  };
  return /*#__PURE__*/_react.default.createElement("button", {
    className: "clear-button-formatting",
    key: tab.id,
    name: tab.id,
    onClick: onClick,
    style: style
  }, tab.label, " ", /*#__PURE__*/_react.default.createElement(_reactBootstrap.Badge, null, count));
}

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

    _defineProperty(this, "_onClick", () => {
      const {
        onClick,
        request
      } = this.props;
      onClick(request);
    });
  }

  render() {
    const {
      active,
      request
    } = this.props;
    const style = {
      backgroundColor: active ? 'lightgrey' : undefined,
      borderBottom: '1px solid grey'
    };
    const {
      endLocation,
      id,
      inboundTripStatus,
      outboundTripStatus,
      schoolName,
      startLocation,
      teacherName,
      timeStamp,
      travelDate
    } = request;
    const formattedDate = travelDate.split(' ').splice(0, 3).join(' ');
    return /*#__PURE__*/_react.default.createElement("li", {
      className: "list-unstyled",
      style: style
    }, /*#__PURE__*/_react.default.createElement(_styled.FieldTripRecordButton, {
      className: "clear-button-formatting",
      name: id,
      onClick: this._onClick
    }, /*#__PURE__*/_react.default.createElement("span", {
      style: {
        display: 'inline-block',
        fontWeight: 600,
        width: '50%'
      }
    }, schoolName, " Trip (#", id, ")"), /*#__PURE__*/_react.default.createElement("span", {
      style: {
        display: 'inline-block',
        width: '50%'
      }
    }, /*#__PURE__*/_react.default.createElement("span", {
      style: {
        marginLeft: '10px'
      }
    }, /*#__PURE__*/_react.default.createElement(_fieldTripStatusIcon.default, {
      ok: Boolean(outboundTripStatus)
    }), " Outbound"), /*#__PURE__*/_react.default.createElement("span", {
      style: {
        marginLeft: '10px'
      }
    }, /*#__PURE__*/_react.default.createElement(_fieldTripStatusIcon.default, {
      ok: Boolean(inboundTripStatus)
    }), " Inbound")), /*#__PURE__*/_react.default.createElement("span", {
      style: {
        display: 'block',
        fontSize: '.9em'
      }
    }, "Submitted by ", teacherName, " on ", timeStamp), /*#__PURE__*/_react.default.createElement("span", {
      style: {
        display: 'block',
        fontSize: '.9em'
      }
    }, startLocation, " to ", endLocation, " on ", /*#__PURE__*/_react.default.createElement("strong", null, formattedDate))));
  }

}

const mapStateToProps = (state, ownProps) => {
  return {
    callTaker: state.callTaker,
    currentQuery: state.otp.currentQuery,
    datastoreUrl: state.otp.config.datastoreUrl,
    searches: state.otp.searches,
    visibleRequests: (0, _callTaker.getVisibleRequests)(state)
  };
};

const mapDispatchToProps = {
  fetchFieldTripDetails: fieldTripActions.fetchFieldTripDetails,
  fetchFieldTrips: fieldTripActions.fetchFieldTrips,
  setActiveFieldTrip: fieldTripActions.setActiveFieldTrip,
  setFieldTripFilter: fieldTripActions.setFieldTripFilter,
  toggleFieldTrips: fieldTripActions.toggleFieldTrips
};

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

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

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