"use strict";

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _extends2 = require("babel-runtime/helpers/extends");

var _extends3 = _interopRequireDefault(_extends2);

var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require("babel-runtime/helpers/createClass");

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require("babel-runtime/helpers/possibleConstructorReturn");

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require("babel-runtime/helpers/inherits");

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _lodash = require("lodash");

require("antd/lib/tag/style");

require("./index.less");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

var ShowType;
(function (ShowType) {
    ShowType[ShowType["IMG"] = 1] = "IMG";
    ShowType[ShowType["NAME"] = 2] = "NAME";
    ShowType[ShowType["IMG_NAME"] = 3] = "IMG_NAME";
})(ShowType || (ShowType = {}));

var SkuParamSelector = function (_React$Component) {
    (0, _inherits3["default"])(SkuParamSelector, _React$Component);

    function SkuParamSelector(props) {
        (0, _classCallCheck3["default"])(this, SkuParamSelector);

        var _this = (0, _possibleConstructorReturn3["default"])(this, (SkuParamSelector.__proto__ || Object.getPrototypeOf(SkuParamSelector)).call(this, props));

        _this.renderAttrName = function (attrValue) {
            var showType = attrValue.showType,
                imgUrl = attrValue.imgUrl,
                value = attrValue.value;

            switch (showType) {
                case ShowType.IMG:
                    return _react2["default"].createElement(
                        "a",
                        { href: "#", className: "itemA itemImg", style: { background: "url(" + imgUrl + ") center no-repeat" } },
                        _react2["default"].createElement(
                            "span",
                            { title: value },
                            value
                        )
                    );
                case ShowType.IMG_NAME:
                    return _react2["default"].createElement(
                        "a",
                        { href: "#", className: "itemA itemImgName", style: { background: "url(" + imgUrl + ") center no-repeat" } },
                        _react2["default"].createElement(
                            "span",
                            { title: value },
                            value
                        )
                    );
                case ShowType.NAME:
                    return _react2["default"].createElement(
                        "a",
                        { href: "#", className: "itemA itemName" },
                        _react2["default"].createElement(
                            "span",
                            { title: value },
                            value
                        )
                    );
            }
            return '';
        };
        _this.onSelectClick = function (attr, attrValue) {
            var attrSelectedMap = _this.state.attrSelectedMap;
            var onAfterSelect = _this.props.onAfterSelect;

            attrSelectedMap[attr.attrId] = attrValue.value;
            var newAttrSelectedMap = (0, _extends3["default"])({}, attrSelectedMap, { attrId: attr.attrId }, { selectable: attrValue.selectable });
            // newAttrSelectedMap[attr.attrId] = attrValue.value;
            // this.setState({attrSelectedMap: newAttrSelectedMap});
            onAfterSelect && onAfterSelect(newAttrSelectedMap);
        };
        var attrSelectedMap = {};
        var datas = _this.props.datas;

        if (datas && datas.length) {
            datas.forEach(function (p) {
                var f = (0, _lodash.find)(p.attrValues, function (p1) {
                    return p1.checked;
                });
                if (f) {
                    attrSelectedMap[p.attrId] = f.value;
                } else {
                    attrSelectedMap[p.attrId] = p.attrValues[0].value;
                }
            });
        }
        _this.state = {
            attrSelectedMap: attrSelectedMap
        };
        return _this;
    }

    (0, _createClass3["default"])(SkuParamSelector, [{
        key: "componentWillReceiveProps",
        value: function componentWillReceiveProps() {
            this.getCheckedData();
        }
    }, {
        key: "getCheckedData",
        value: function getCheckedData() {
            var attrSelectedMap = {};
            var datas = this.props.datas;

            if (datas && datas.length) {
                datas.forEach(function (p) {
                    var f = (0, _lodash.find)(p.attrValues, function (p1) {
                        return p1.checked;
                    });
                    if (f) {
                        attrSelectedMap[p.attrId] = f.value;
                    } else {
                        attrSelectedMap[p.attrId] = p.attrValues[0].value;
                    }
                });
            }
            this.setState({
                attrSelectedMap: attrSelectedMap
            });
        }
    }, {
        key: "render",
        value: function render() {
            var _this2 = this;

            var datas = this.props.datas;
            var attrSelectedMap = this.state.attrSelectedMap;

            return _react2["default"].createElement(
                "div",
                { className: "sku-param-selector" },
                datas && datas.length > 0 && datas.map(function (p) {
                    return _react2["default"].createElement(
                        "div",
                        { className: "attr-item", key: p.attrId },
                        _react2["default"].createElement(
                            "div",
                            { className: "itemTitle" },
                            p.attrName
                        ),
                        _react2["default"].createElement(
                            "div",
                            null,
                            _react2["default"].createElement(
                                "ul",
                                null,
                                p.attrValues && p.attrValues.length > 0 && p.attrValues.map(function (p2) {
                                    return _react2["default"].createElement(
                                        "li",
                                        { key: p.attrId + "_" + p2.value, className: (attrSelectedMap[p.attrId] === p2.value ? 'selected' : '') + " " + (attrSelectedMap[p.attrId] !== p2.value && !p2.selectable ? 'unselectable' : ''), onClick: function onClick() {
                                                _this2.onSelectClick(p, p2);
                                            } },
                                        _this2.renderAttrName(p2)
                                    );
                                })
                            )
                        )
                    );
                })
            );
        }
    }]);
    return SkuParamSelector;
}(_react2["default"].Component);

exports["default"] = SkuParamSelector;

SkuParamSelector.defaultProps = {
    datas: []
};
module.exports = exports['default'];