"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        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 routes_1 = require("../../../apiClient/routes");
var ProductDetailFooter_1 = require("./products/ProductDetailFooter");
var NewCarousel_1 = require("../../common/NewCarousel");
var httpService_1 = require("../../../services/httpService");
var html_react_parser_1 = require("html-react-parser");
var ProductDetail = /** @class */ (function (_super) {
    __extends(ProductDetail, _super);
    function ProductDetail(props) {
        var _this = _super.call(this, props) || this;
        _this.increaseDecreaseAmount = function (newAmount) {
            _this.setState({ amount: newAmount });
        };
        _this.handleAmountChange = function (e) {
            _this.setState({ amount: ~~e.currentTarget.value });
        };
        _this.handleAddToCartClick = function () {
            var _a = _this.state, amount = _a.amount, product = _a.product;
            var errors = [];
            if (amount <= 0 || amount > product.availableQuantity) {
                errors.push("Zvolen\u00E9 mno\u017Estv\u00ED: <strong>".concat(amount, "</strong> kus\u016F nen\u00ED na sklad\u011B! Aktu\u00E1ln\u011B dostupn\u00E9 mno\u017Estv\u00ED je: <strong>").concat(_this.state.product.availableQuantity, "</strong> kus\u016F."));
                _this.setState({ errors: errors });
            }
            else {
                _this.setState({ errors: errors });
                _this.props.onAddToCartClick(product.id, amount);
            }
        };
        _this.state = {
            product: {
                id: 0,
                name: '',
                price: 0,
                availableQuantity: 0,
                reducedPrice: 0,
                description: '',
                productCategoryID: 0,
                productCategoryName: '',
                productImageIDs: []
            },
            amount: 1,
            outOfStock: false,
            errors: []
        };
        return _this;
    }
    ProductDetail.prototype.componentDidMount = function () {
        var _this = this;
        document.title = "Detail produktu";
        var productId = this.props.match.params.id;
        httpService_1.default.get((0, routes_1.ProductController_GetProduct)(productId)).then(function (products) {
            var product = products.data;
            _this.setState({ product: product, outOfStock: (product.availableQuantity <= 0) });
        })
            .catch(function (e) {
            console.log(e);
            // pri chybe (nenalezeni produktu apod.), presmerovani na vypis produktu
            _this.props.history.push("/");
        });
    };
    ProductDetail.prototype.render = function () {
        var _this = this;
        var _a = this.state, amount = _a.amount, outOfStock = _a.outOfStock, product = _a.product, errors = _a.errors;
        return (React.createElement("section", { className: "product-details" }, product.id !== 0
            ? React.createElement("div", { className: "container" },
                React.createElement("div", { className: "row" },
                    React.createElement("div", { className: "col-lg-6 col-md-6" },
                        React.createElement("div", { className: "product__details__pic" },
                            React.createElement(NewCarousel_1.NewCarousel, { imageIds: product.productImageIDs }))),
                    React.createElement("div", { className: "col-lg-6 col-md-6" },
                        React.createElement("div", { className: "product__details__text" },
                            React.createElement("h3", null, product.name),
                            React.createElement("div", { className: "product__details__rating" },
                                React.createElement("span", null,
                                    "Kategorie: ",
                                    product.productCategoryName)),
                            React.createElement("div", { className: "product__details__price" },
                                product.reducedPrice !== undefined && product.reducedPrice > 0 && product.reducedPrice < product.price
                                    ? product.reducedPrice.toFixed(2)
                                    : product.price.toFixed(2),
                                " K\u010D"),
                            outOfStock
                                ? ''
                                : React.createElement("div", null,
                                    React.createElement("div", { className: "product__details__quantity" },
                                        React.createElement("small", null, "Mno\u017Estv\u00ED:"),
                                        React.createElement("div", { className: "quantity" },
                                            React.createElement("div", { className: errors.length ? "pro-qty input__error" : "pro-qty" },
                                                React.createElement("i", { onClick: function () { return _this.increaseDecreaseAmount(amount - 1); }, className: "fas fa-minus qtybtn" }),
                                                React.createElement("input", { type: "text", onChange: function (e) { return _this.handleAmountChange(e); }, value: amount, pattern: "[1-9]*" }),
                                                React.createElement("i", { onClick: function () { return _this.increaseDecreaseAmount(amount + 1); }, className: "fas fa-plus qtybtn" })))),
                                    React.createElement("button", { onClick: this.handleAddToCartClick, className: "primary-btn", disabled: outOfStock }, "DO KO\u0160\u00CDKU"),
                                    React.createElement("br", null),
                                    errors.length
                                        ? React.createElement("span", { className: "error-message" }, (0, html_react_parser_1.default)(errors[0]))
                                        : null,
                                    React.createElement("br", null),
                                    outOfStock
                                        ? React.createElement("span", { className: "error-message" }, "Omlouv\u00E1me se, ale tento produkt nen\u00ED moment\u00E1ln\u011B skladem!")
                                        : null),
                            React.createElement("ul", null,
                                React.createElement("li", null,
                                    React.createElement("strong", null, "Mno\u017Estv\u00ED na sklad\u011B: "),
                                    product.availableQuantity <= 0
                                        ? React.createElement("span", { className: "text-danger font-weight-bold" }, " Nedostupn\u00E9")
                                        : (product.availableQuantity > 0 && product.availableQuantity <= 10)
                                            ? React.createElement(React.Fragment, null, product.availableQuantity)
                                            : React.createElement(React.Fragment, null,
                                                "v\u00EDce ne\u017E ",
                                                React.createElement("b", null, "10"),
                                                " kus\u016F")),
                                React.createElement("li", null,
                                    React.createElement("strong", null, "Dodac\u00ED lh\u016Fta:"),
                                    "obvykle do 5 pracovn\u00EDch dn\u016F")))),
                    React.createElement("div", { className: "col-lg-12" },
                        React.createElement(ProductDetailFooter_1.default, { productDescription: product.description }))))
            : ''));
    };
    return ProductDetail;
}(React.Component));
exports.default = ProductDetail;
//# sourceMappingURL=productDetail.js.map