"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
var _react = _interopRequireWildcard(require("react"));
require("./good-item.scss");
var _subtract = _interopRequireDefault(require("../../../assets/icon/shop/subtract.png"));
var _add = _interopRequireDefault(require("../../../assets/icon/shop/add.png"));
var _checkbox = _interopRequireDefault(require("../../../assets/icon/shop/checkbox.png"));
var _excluded = ["item", "isEdit", "onUpdateItem", "onCheckChange", "onAddOrSubtract"];
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
/**
 * 数据
 */

var GoodItem = function GoodItem(_ref) {
  var item = _ref.item,
    isEdit = _ref.isEdit,
    onUpdateItem = _ref.onUpdateItem,
    onCheckChange = _ref.onCheckChange,
    onAddOrSubtract = _ref.onAddOrSubtract,
    otherProps = (0, _objectWithoutPropertiesLoose2["default"])(_ref, _excluded);
  var _otherProps = otherProps || {};
  var _useState = (0, _react.useState)(item),
    localItem = _useState[0],
    setLocalItem = _useState[1];
  (0, _react.useEffect)(function () {
    console.log('修改', localItem.id, localItem.checked);
  }, [item.checked]);
  var isUndercarriage = (0, _react.useMemo)(function () {
    return localItem.productGrounding === '已下架' || localItem.productGrounding === '1';
  }, [localItem]);

  // const localCover = useMemo(() => {
  //   return localItem.imgUrl || 'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg';
  // }, [localItem]);

  var handleSubtract = (0, _react.useCallback)(function () {
    if (localItem.amount <= 1) return;
    var prevItem = (0, _extends2["default"])({}, localItem, {
      amount: localItem.amount - 1,
      isUpdate: true
    });
    setLocalItem(prevItem);
    onUpdateItem((0, _extends2["default"])({}, localItem, {
      amount: localItem.amount - 1,
      isUpdate: true
    }));
    onAddOrSubtract('subtract', prevItem);
  }, [localItem, onUpdateItem]);
  var handleAdd = (0, _react.useCallback)(function () {
    var prevItem = (0, _extends2["default"])({}, localItem, {
      amount: localItem.amount + 1,
      isUpdate: true
    });
    setLocalItem(prevItem);
    onUpdateItem(prevItem);
    onAddOrSubtract('add', prevItem);
  }, [localItem, onUpdateItem]);

  // const handleCheckChange = useCallback((e: CheckboxChangeEvent) => {
  //   const prevItem = {...localItem, checked: e.target.checked}
  //   setLocalItem(prevItem)
  //   onCheckChange(prevItem);
  // }, [onCheckChange]);

  var handleCheckChange = (0, _react.useCallback)(function () {
    var prevItem = (0, _extends2["default"])({}, localItem, {
      checked: !localItem.checked
    });
    setLocalItem(prevItem);
    onCheckChange(prevItem);
  }, [onCheckChange]);
  return /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({
    className: "view"
  }, _otherProps), /*#__PURE__*/_react["default"].createElement("div", {
    className: "left"
  }, item.checked ? /*#__PURE__*/_react["default"].createElement("div", {
    className: "checkbox-img"
  }, /*#__PURE__*/_react["default"].createElement("img", {
    className: "checkbox-img",
    src: _checkbox["default"],
    onClick: handleCheckChange
  })) : /*#__PURE__*/_react["default"].createElement("div", {
    className: "checkbox2",
    onClick: handleCheckChange
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "cover-box"
  }, isUndercarriage && /*#__PURE__*/_react["default"].createElement("div", {
    className: "overlay"
  }, /*#__PURE__*/_react["default"].createElement("span", null, "\u5DF2\u4E0B\u67B6")), /*#__PURE__*/_react["default"].createElement("img", {
    className: "cover",
    src: localItem.imgUrl,
    alt: "cover",
    style: {
      objectFit: 'cover'
    }
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "info"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "title"
  }, localItem.productName), /*#__PURE__*/_react["default"].createElement("div", {
    className: "good-item-footer"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "price"
  }, "\xA5", /*#__PURE__*/_react["default"].createElement("span", null, localItem.productPromotionPrice || localItem.productPrice), localItem.productPromotionPrice && /*#__PURE__*/_react["default"].createElement("span", {
    className: "line-through"
  }, "\xA5", localItem.productPrice)), !isUndercarriage && /*#__PURE__*/_react["default"].createElement("div", {
    className: "actions"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "subtract",
    onClick: handleSubtract
  }, /*#__PURE__*/_react["default"].createElement("img", {
    className: "icon",
    src: _subtract["default"],
    alt: "subtract"
  })), /*#__PURE__*/_react["default"].createElement("div", {
    className: "text"
  }, localItem.amount), /*#__PURE__*/_react["default"].createElement("div", {
    className: "add",
    onClick: handleAdd
  }, /*#__PURE__*/_react["default"].createElement("img", {
    className: "icon",
    src: _add["default"],
    alt: "add"
  }))))));
};
GoodItem.displayName = 'GoodItem';
var _default = exports["default"] = GoodItem;