"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var FontAwesome = require("react-fontawesome");
var react_bootstrap_1 = require("react-bootstrap");
var react_jsonschema_form_1 = require("react-jsonschema-form");
var EntityList = /** @class */ (function (_super) {
    __extends(EntityList, _super);
    function EntityList(props) {
        var _this = _super.call(this, props) || this;
        _this.state = { showAddModal: false, showDeleteModal: false, selectedEntity: undefined, entityList: [], editing: false };
        return _this;
    }
    EntityList.prototype.componentDidMount = function () {
        this.getAll();
    };
    EntityList.prototype.render = function () {
        var _this = this;
        var log = function (type) { return console.log.bind(console, type); };
        var formData = this.state.editing ? this.state.selectedEntity : undefined;
        return (React.createElement(react_bootstrap_1.Panel, { header: (React.createElement(react_bootstrap_1.Row, null,
                React.createElement(react_bootstrap_1.Col, { md: 10, xs: 6 },
                    this.props.displayConfig.name,
                    "s"),
                React.createElement(react_bootstrap_1.Col, { md: 2, xs: 6 },
                    React.createElement(react_bootstrap_1.Button, { bsStyle: "primary", onClick: function () { return _this.setState({ showAddModal: true }); } },
                        React.createElement(FontAwesome, { name: "plus" }))))) },
            React.createElement(react_bootstrap_1.Table, { striped: true, bordered: true, condensed: true, hover: true, responsive: true },
                React.createElement("thead", null,
                    React.createElement("tr", null,
                        this.props.displayConfig.columns.map(function (col, idx) { return (React.createElement("th", { key: idx }, col.name)); }),
                        React.createElement("th", null))),
                React.createElement("tbody", null, this.state.entityList.map(function (field, idx) {
                    return (React.createElement("tr", { key: idx },
                        _this.props.displayConfig.columns.map(function (col, iidx) { return (React.createElement("td", { key: iidx }, col.renderer ? col.renderer(field[col.name]) : field[col.name])); }),
                        React.createElement("td", null,
                            React.createElement(react_bootstrap_1.ButtonGroup, null,
                                React.createElement(react_bootstrap_1.Button, { bsStyle: "primary", onClick: function () { return _this.startEditing(field); } },
                                    React.createElement(FontAwesome, { name: "edit" })),
                                React.createElement(react_bootstrap_1.Button, { bsStyle: "danger", onClick: function () { return _this.startDeleting(field); } },
                                    React.createElement(FontAwesome, { name: "remove" }))))));
                }))),
            React.createElement(react_bootstrap_1.Modal, { show: this.state.showAddModal, onHide: this.close.bind(this) },
                React.createElement(react_bootstrap_1.Modal.Header, { closeButton: true },
                    React.createElement(react_bootstrap_1.Modal.Title, null,
                        this.state.editing ? "Edit " : "Add new ",
                        this.props.displayConfig.name)),
                React.createElement(react_bootstrap_1.Modal.Body, null,
                    React.createElement(react_jsonschema_form_1.default, { schema: this.props.displayConfig.schema, uiSchema: this.props.displayConfig.uiSchema, onSubmit: this.add.bind(this), formData: formData, widgets: this.props.displayConfig.widgets, onError: log("errors") })),
                React.createElement(react_bootstrap_1.Modal.Footer, null)),
            React.createElement(react_bootstrap_1.Modal, { show: this.state.showDeleteModal, onHide: this.close.bind(this) },
                React.createElement(react_bootstrap_1.Modal.Header, { closeButton: true },
                    React.createElement(react_bootstrap_1.Modal.Title, null,
                        "Delete ",
                        this.props.displayConfig.name)),
                React.createElement(react_bootstrap_1.Modal.Body, null, "Are you sure you want to delete?"),
                React.createElement(react_bootstrap_1.Modal.Footer, null,
                    React.createElement(react_bootstrap_1.Button, { onClick: this.close.bind(this) }, "Cancel"),
                    React.createElement(react_bootstrap_1.Button, { onClick: this.delete.bind(this), bsStyle: "primary" }, "Delete")))));
    };
    EntityList.prototype.getAll = function () {
        var _this = this;
        this.props.entityService.fetchAll().then(function (entityList) { return _this.setState({ entityList: entityList }); });
    };
    EntityList.prototype.startEditing = function (field) {
        if (this.props.entityService.beforeEditing) {
            field = this.props.entityService.beforeEditing(field);
        }
        this.setState({ showAddModal: true, selectedEntity: field, editing: true });
    };
    EntityList.prototype.startDeleting = function (field) {
        this.setState({ showDeleteModal: true, selectedEntity: field });
    };
    EntityList.prototype.close = function () {
        this.getAll();
        this.setState({ showAddModal: false, selectedEntity: undefined, editing: false, showDeleteModal: false });
    };
    EntityList.prototype.add = function (type) {
        var _this = this;
        var formData = type.formData;
        if (this.props.entityService.beforeSave) {
            formData = this.props.entityService.beforeSave(formData);
        }
        if (this.state.editing) {
            this.props.entityService.update(formData).then(function () { return _this.close(); });
        }
        else {
            this.props.entityService.add(formData).then(function () { return _this.close(); });
        }
    };
    EntityList.prototype.delete = function () {
        var _this = this;
        this.props.entityService.delete(this.state.selectedEntity).then(function () { return _this.close(); });
    };
    return EntityList;
}(React.Component));
exports.EntityList = EntityList;
var ColumnConfig = /** @class */ (function () {
    function ColumnConfig(name, renderer) {
        this.name = name;
        this.renderer = renderer;
    }
    return ColumnConfig;
}());
exports.ColumnConfig = ColumnConfig;
var DisplayConfig = /** @class */ (function () {
    function DisplayConfig(name, schema, uiSchema, columns, widgets) {
        this.name = name;
        this.schema = schema;
        this.uiSchema = uiSchema;
        this.columns = columns;
        this.widgets = widgets;
    }
    return DisplayConfig;
}());
exports.DisplayConfig = DisplayConfig;
//# sourceMappingURL=EntityList.js.map