"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.mapStateToProps = exports.mapDispatchToProps = exports.TechBarComponent = exports.TechBar = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/esm/objectSpread2"));
var _react = _interopRequireDefault(require("react"));
var _store = require("../../../redux/store");
var _recompose = require("recompose");
var _bundleCommon = require("@kineticdata/bundle-common");
var _PageTitle = require("../../shared/PageTitle");
var _router = require("@reach/router");
var _moment = _interopRequireDefault(require("moment"));
var _appointments = require("../../../redux/modules/appointments");
var _react2 = require("@kineticdata/react");
var _TechBarDisplayMembers = require("./TechBarDisplayMembers");
var _constants = require("../../../constants");
var TechBarComponent = exports.TechBarComponent = function TechBarComponent(_ref) {
  var techBar = _ref.techBar,
    hasManagerAccess = _ref.hasManagerAccess,
    appointments = _ref.appointments,
    appointmentDate = _ref.appointmentDate,
    handlePreviousDay = _ref.handlePreviousDay,
    handleNextDay = _ref.handleNextDay,
    handleToday = _ref.handleToday,
    isToday = _ref.isToday,
    kapp = _ref.kapp;
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "page-container"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "page-panel"
  }, /*#__PURE__*/_react["default"].createElement(_PageTitle.PageTitle, {
    parts: [techBar && techBar.values['Name']],
    settings: true,
    breadcrumbs: [{
      label: 'Home',
      to: '/'
    }, {
      label: "".concat(kapp.name, " Settings"),
      to: '../..'
    }, {
      label: 'Tech Bars',
      to: '..'
    }],
    title: techBar && techBar.values['Name'],
    actions: [techBar && hasManagerAccess && {
      label: 'Edit Settings',
      icon: 'pencil',
      to: 'edit'
    }]
  }), techBar ? /*#__PURE__*/_react["default"].createElement("div", {
    className: "content-wrapper"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "form form-unstyled mb-5"
  }, /*#__PURE__*/_react["default"].createElement("h2", {
    className: "section__title"
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "title"
  }, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, "General Settings"))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "form-group"
  }, /*#__PURE__*/_react["default"].createElement("label", null, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, "Allow Walk-Ins")), /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, techBar.settings.allowWalkIns ? 'Yes' : 'No'))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "form-group"
  }, /*#__PURE__*/_react["default"].createElement("label", null, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, "Feedback Identification")), /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, techBar.settings.feedbackIdentitifcation)))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "mb-5"
  }, /*#__PURE__*/_react["default"].createElement("h2", {
    className: "section__title"
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "title"
  }, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, "Front Desk Users"))), /*#__PURE__*/_react["default"].createElement(_TechBarDisplayMembers.TechBarDisplayMembers, {
    techBar: techBar,
    hasManagerAccess: hasManagerAccess
  })), /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement("h2", {
    className: "section__title"
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "title"
  }, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, "Appointments"))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "appointments-control mb-3"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "btn-group"
  }, /*#__PURE__*/_react["default"].createElement("button", {
    className: "btn btn-inverse",
    onClick: handlePreviousDay
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "fa fa-chevron-left"
  })), /*#__PURE__*/_react["default"].createElement("button", {
    className: "btn btn-inverse",
    onClick: handleToday,
    disabled: isToday
  }, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, "Today")), /*#__PURE__*/_react["default"].createElement("button", {
    className: "btn btn-inverse",
    onClick: handleNextDay
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "fa fa-chevron-right"
  }))), /*#__PURE__*/_react["default"].createElement("span", {
    className: "ml-3"
  }, /*#__PURE__*/_react["default"].createElement(_react2.Moment, {
    timestamp: appointmentDate,
    format: _react2.Moment.formats.dateWithDay
  }))), appointments ? /*#__PURE__*/_react["default"].createElement(_bundleCommon.Table, {
    props: {
      "class": 'table--settings table-hover',
      name: 'appointments-table',
      id: 'appointments-table'
    },
    data: appointments.map(function (a) {
      return (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, a.values), {}, {
        _id: a.id,
        _coreState: a.coreState
      });
    }).toJS(),
    columns: [{
      value: 'Summary',
      title: 'Summary',
      renderBodyCell: function renderBodyCell(_ref2) {
        var content = _ref2.content,
          row = _ref2.row;
        return /*#__PURE__*/_react["default"].createElement("td", null, /*#__PURE__*/_react["default"].createElement(_router.Link, {
          to: "appointment/".concat(row._id)
        }, content));
      }
    }, {
      value: 'Requested For Display Name',
      title: 'User'
    }, {
      value: 'Event Time',
      title: 'Time',
      renderBodyCell: function renderBodyCell(_ref3) {
        var content = _ref3.content,
          row = _ref3.row;
        var start = _moment["default"].utc(row['Event Time'], _constants.TIME_FORMAT);
        var end = start.clone().add(row['Event Duration'], 'minute');
        return /*#__PURE__*/_react["default"].createElement("td", null, /*#__PURE__*/_react["default"].createElement(_react2.Moment, {
          timestamp: start,
          format: _react2.Moment.formats.time
        }), " - ", /*#__PURE__*/_react["default"].createElement(_react2.Moment, {
          timestamp: end,
          format: _react2.Moment.formats.time
        }));
      }
    }, {
      value: 'Event Type',
      title: 'Event Type'
    }, {
      value: 'Status',
      title: 'Status',
      width: '1%',
      renderBodyCell: function renderBodyCell(_ref4) {
        var content = _ref4.content,
          row = _ref4.row;
        return /*#__PURE__*/_react["default"].createElement("td", null, /*#__PURE__*/_react["default"].createElement("span", {
          className: "badge ".concat(row._coreState === 'Closed' ? 'badge-dark' : 'badge-success')
        }, /*#__PURE__*/_react["default"].createElement(_react2.I18n, null, content)));
      }
    }],
    emptyMessage: "There are no appointments for the selected date.",
    filtering: false,
    pagination: false,
    sortOrder: 2,
    render: function render(_ref5) {
      var table = _ref5.table;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "table-wrapper"
      }, table);
    }
  }) : /*#__PURE__*/_react["default"].createElement(_bundleCommon.LoadingMessage, null))) : /*#__PURE__*/_react["default"].createElement(_bundleCommon.ErrorNotFound, null)));
};
var mapStateToProps = exports.mapStateToProps = function mapStateToProps(state, props) {
  var isSchedulerAdmin = (0, _bundleCommon.selectHasRoleSchedulerAdmin)(state.app.profile);
  var techBar = state.techBarApp.schedulers.find(function (scheduler) {
    return scheduler.id === props.techBarId && (isSchedulerAdmin || _bundleCommon.Utils.isMemberOf(state.app.profile, "Role::Scheduler::".concat(scheduler.values['Name'])) || _bundleCommon.Utils.isMemberOf(state.app.profile, "Scheduler::".concat(scheduler.values['Name'])));
  });
  return {
    kapp: state.app.kapp,
    techBar: techBar,
    hasManagerAccess: isSchedulerAdmin || techBar && _bundleCommon.Utils.isMemberOf(state.app.profile, "Role::Scheduler::".concat(techBar.values['Name'])),
    appointmentError: state.appointments.error,
    appointments: state.appointments.list,
    appointmentDate: state.appointments.listDate
  };
};
var mapDispatchToProps = exports.mapDispatchToProps = {
  fetchAppointmentsListRequest: _appointments.actions.fetchAppointmentsListRequest,
  setAppointmentsListDate: _appointments.actions.setAppointmentsListDate
};
var TechBar = exports.TechBar = (0, _recompose.compose)((0, _store.connect)(mapStateToProps, mapDispatchToProps), (0, _recompose.withProps)(function (_ref6) {
  var appointmentDate = _ref6.appointmentDate;
  return {
    isToday: appointmentDate.format('YYYY-MM-DD') === (0, _moment["default"])().format('YYYY-MM-DD')
  };
}), (0, _recompose.withHandlers)({
  handlePreviousDay: function handlePreviousDay(_ref7) {
    var appointmentDate = _ref7.appointmentDate,
      setAppointmentsListDate = _ref7.setAppointmentsListDate,
      techBar = _ref7.techBar;
    return function () {
      setAppointmentsListDate({
        date: appointmentDate.add(-1, 'day'),
        schedulerId: techBar.values['Id']
      });
    };
  },
  handleNextDay: function handleNextDay(_ref8) {
    var appointmentDate = _ref8.appointmentDate,
      setAppointmentsListDate = _ref8.setAppointmentsListDate,
      techBar = _ref8.techBar;
    return function () {
      setAppointmentsListDate({
        date: appointmentDate.add(1, 'day'),
        schedulerId: techBar.values['Id']
      });
    };
  },
  handleToday: function handleToday(_ref9) {
    var setAppointmentsListDate = _ref9.setAppointmentsListDate,
      techBar = _ref9.techBar;
    return function () {
      setAppointmentsListDate({
        date: (0, _moment["default"])(),
        schedulerId: techBar.values['Id']
      });
    };
  }
}), (0, _recompose.lifecycle)({
  componentDidMount: function componentDidMount() {
    if (this.props.techBar) {
      this.props.fetchAppointmentsListRequest({
        schedulerId: this.props.techBar.values['Id']
      });
    }
  }
}))(TechBarComponent);