"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.clear = exports.getUnitHtml = void 0;

require("antd-mobile/lib/modal/style");

var _modal = _interopRequireDefault(require("antd-mobile/lib/modal"));

var _react = _interopRequireDefault(require("react"));

var _uniq = _interopRequireDefault(require("lodash/uniq"));

var _indexOf = _interopRequireDefault(require("lodash/indexOf"));

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

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

var selfState = {
  clickNum: 0,
  row: '',
  col: '',
  clickObj: {} // 保存点击数据

};

var getSelectStyle = function getSelectStyle(type) {
  var css = {};

  if (Number(type) === 1) {
    css = {
      backgroundColor: '#F58F3E',
      color: 'white',
      originalColor: '#F58F3E'
    };
  } else {
    css = {
      backgroundColor: '#FFFFFF',
      color: '#8F8F95',
      originalColor: '#8F8F95',
      priceColor: '#F58F3E'
    };
  }

  return css;
};

var clear = function clear() {
  selfState = {
    clickNum: 0,
    row: '',
    col: '',
    clickObj: {}
  };
};

exports.clear = clear;

var clearSelect = function clearSelect(array, col) {
  array.forEach(function (e, i) {
    // 清除所选
    if (e["params".concat(col)]) {
      e["params".concat(col)].select = undefined;
    }
  });
};

var getTotalPrice = function getTotalPrice(clickObj) {
  var price = 0;
  var rulesId = [];
  var countsArray = [];

  for (var key in clickObj) {
    if (_typeof(clickObj[key]) === 'object') {
      price = price + clickObj[key].price || 0;
      rulesId = rulesId.concat(clickObj[key].rulesId);
      countsArray = countsArray.concat(clickObj[key].countsArray);
    }
  }

  rulesId = (0, _uniq["default"])(rulesId);
  return {
    price: price,
    rulesId: rulesId,
    countsArray: countsArray
  };
};

var checkCloseUnit = function checkCloseUnit(obj, row, col, array) {
  var selectArray = array.slice(Math.min(Number(obj.startRow), Number(row)), Math.max(Number(obj.startRow), Number(row)) + 1); // 选择连续的单元格
  // 不可预订的单元格数组

  var hasCloseUnit = selectArray.filter(function (ele) {
    return Number(ele["params".concat(col)] && ele["params".concat(col)].status) === 1 || !!ele.dayTime || !ele["params".concat(String(col))];
  });
  return !!hasCloseUnit.length; // true代表有中间有关闭格子
};

var doSelect = function doSelect(_ref, _ref2, multiTimeInterval) {
  var e = _ref.e,
      ele = _ref.ele,
      row = _ref.row,
      col = _ref.col,
      data = _ref.data,
      array = _ref.array,
      clickObj = _ref.clickObj,
      headerContent = _ref.headerContent,
      ruleDate = _ref.ruleDate;
  var setParentState = _ref2.setParentState,
      configState = _ref2.configState;
  var _window$$$RESOURCECON = window.$$RESOURCECONTEXT,
      priceType = _window$$$RESOURCECON.priceType,
      rentalType = _window$$$RESOURCECON.rentalType,
      multiUnit = _window$$$RESOURCECON.multiUnit,
      autoAssign = _window$$$RESOURCECON.autoAssign;
  var _data$dataDetail = data.dataDetail,
      dataDetail = _data$dataDetail === void 0 ? {} : _data$dataDetail;
  var rentalDate = '';
  var price = 0;
  var rulesId = [];
  var countsArray = [];

  if (multiTimeInterval === 0) {
    clickObj[String(col)] = {
      startRow: row,
      price: ele.price,
      rulesId: [ele.id],
      countsArray: [ele.counts]
    };
    ele.select = 1;
    array[row]["params".concat(col)] = _objectSpread({}, ele);
  } else if (!clickObj[String(col)]) {
    // 首次点击某列 确定start的头 startRow
    clickObj[String(col)] = {
      startRow: row,
      price: ele.price,
      rulesId: [ele.id],
      countsArray: [ele.counts]
    };
    ele.select = 1;
    array[row]["params".concat(col)] = _objectSpread({}, ele);
  } else if (checkCloseUnit(clickObj[String(col)], row, col, array, ele)) {
    clearSelect(array, col);
    clickObj[String(col)] = {
      startRow: row,
      price: ele.price,
      rulesId: [ele.id],
      countsArray: [ele.counts]
    };
    ele.select = 1;
    array[row]["params".concat(col)] = _objectSpread({}, ele);
  } else if (!clickObj[String(col)].endRow && clickObj[String(col)].endRow !== 0) {
    // 确认尾巴 endRow
    clickObj[String(col)].endRow = row;

    if (row > clickObj[String(col)].startRow) {
      clickObj[String(col)].derection = 1; // derection 1 为正方向
    } else if (row < clickObj[String(col)].startRow) {
      clickObj[String(col)].derection = -1; // derection -1 为反方向
    } else {
      // 相等则清空
      clickObj[String(col)] = undefined;
    }
  } else if (clickObj[String(col)].derection) {
    // 头尾都有
    if (row === clickObj[String(col)].startRow) {
      // 点击头
      clickObj[String(col)] = undefined;
    } else if (row === clickObj[String(col)].endRow) {
      // 点击尾巴
      if (clickObj[String(col)].derection === -1) {
        clickObj[String(col)].endRow = row + 1;
      } else {
        clickObj[String(col)].endRow = row - 1;
      }
    } else if (clickObj[String(col)].derection === 1) {
      // 下次点击 在头之前 方向调换
      if (row < clickObj[String(col)].startRow) {
        clickObj[String(col)].derection = 0 - clickObj[String(col)].derection;
        clickObj[String(col)].startRow = clickObj[String(col)].endRow;
        clickObj[String(col)].endRow = row;
      } else if (row > clickObj[String(col)].endRow) {
        // 下次点击 在尾巴以后
        clickObj[String(col)].endRow = row;
      } else {
        // 点击中间
        clickObj[String(col)].endRow = row - 1;
      }
    } else if (clickObj[String(col)].derection === -1) {
      // 下次点击 在头之后 方向调换
      if (row > clickObj[String(col)].startRow) {
        clickObj[String(col)].derection = 0 - clickObj[String(col)].derection;
        clickObj[String(col)].startRow = clickObj[String(col)].endRow;
        clickObj[String(col)].endRow = row;
      } else if (row < clickObj[String(col)].endRow) {
        clickObj[String(col)].endRow = row;
      } else {
        // 点击中间
        clickObj[String(col)].endRow = row + 1;
      }
    } else {
      // 点击中间
      clickObj[String(col)].endRow = row;
    }
  }

  if (clickObj[String(col)]) {
    var minValue = Math.min(Number(clickObj[String(col)].startRow), Number(clickObj[String(col)].endRow));
    var maxValue = Math.max(Number(clickObj[String(col)].startRow), Number(clickObj[String(col)].endRow));

    if (!isNaN(minValue) && !isNaN(maxValue)) {
      clickObj[String(col)].price = 0;
      clickObj[String(col)].rulesId = [];

      for (var i = minValue; i < maxValue + 1; i++) {
        array[i]["params".concat(col)].select = 1;

        if (clickObj[String(col)].rulesId.indexOf(array[i]["params".concat(col)].id) === -1) {
          clickObj[String(col)].rulesId.push(array[i]["params".concat(col)].id);
        }

        clickObj[String(col)].countsArray.push(array[i]["params".concat(col)].counts);

        if (Number(priceType) === 1) {
          if (i === minValue) {
            clickObj[String(col)].price = array[i]["params".concat(col)].price;
          } else {
            clickObj[String(col)].price = clickObj[String(col)].price + array[i]["params".concat(col)].initiatePrice;
          }
        } else {
          clickObj[String(col)].price = clickObj[String(col)].price + array[i]["params".concat(col)].price;
        }
      }
    }
  }

  price = getTotalPrice(clickObj).price;
  rulesId = (0, _uniq["default"])(getTotalPrice(clickObj).rulesId);

  if (Number(priceType) === 1 && rulesId.length > 1) {
    price = ele.price + (rulesId.length - 1) * ele.initiatePrice;
  }

  if (Number(multiUnit) === 1 && Number(autoAssign) === 0) {
    countsArray = getTotalPrice(clickObj).countsArray;
  } else {
    countsArray = [1];
  }

  rentalDate = headerContent[col].rentalDate;
  var totalRuleDate = [];

  if (String(rentalType) === '1' || String(rentalType) === '3') {
    // 处理满减
    var checkRuleDate = {
      params0: []
    };
    var storeRuleDate = {
      params0: []
    };

    if (dataDetail.siteNames) {
      dataDetail.siteNames.map(function (ele, index) {
        checkRuleDate["params".concat(index)] = [];
        storeRuleDate["params".concat(index)] = [];
      });
    }

    array.map(function (ele, index) {
      for (var key in ele) {
        if (ele[key] && Number(ele[key].select === 1)) {
          var indexOfDate = storeRuleDate[key].indexOf(ele[key].ruleDate);

          if (String(rentalType) === '1') {
            if (indexOfDate > -1) {
              checkRuleDate[key].push(ele[key].ruleDate);
            }

            storeRuleDate[key].push(ele[key].ruleDate);
          } else if (String(rentalType) === '3') {
            if (indexOfDate > -1) {
              var secondIndexOf = (0, _indexOf["default"])(storeRuleDate[key], ele[key].ruleDate, indexOfDate + 1);

              if (secondIndexOf > -1) {
                checkRuleDate[key].push(ele[key].ruleDate);
              }
            }

            storeRuleDate[key].push(ele[key].ruleDate);
          }
        }
      }
    });

    for (var key in checkRuleDate) {
      totalRuleDate = totalRuleDate.concat(checkRuleDate[key]);
    }
  }

  var totalPrice = Number(price.toFixed(2));

  if (String(dataDetail.discountType) === '1') {
    if (Number(price.toFixed(2)) >= dataDetail.fullPrice) {
      totalPrice = Number(price.toFixed(2)) - Number(dataDetail.cutPrice);
    }
  } else if (String(dataDetail.discountType) === '2') {
    totalPrice = Number(price.toFixed(2)) - Number(dataDetail.cutPrice) * totalRuleDate.length;
  }

  configState({
    bodyArray: array
  });
  setParentState({
    bottomState: {
      // 父组件的state，为了让ConfirmBottom组件使用
      price: Number(totalPrice.toFixed(2)),
      originalPrice: Number(price.toFixed(2)),
      rulesId: rulesId,
      rentalDate: rentalDate || ruleDate,
      counts: Math.min.apply(null, countsArray) && Math.min.apply(null, countsArray) !== Infinity ? Math.min.apply(null, countsArray) : ''
    }
  });
};

var onSelect = function onSelect(_ref3, _ref4) {
  var e = _ref3.e,
      ele = _ref3.ele,
      row = _ref3.row,
      col = _ref3.col,
      data = _ref3.data;
  var setParentState = _ref4.setParentState,
      configState = _ref4.configState;
  var _window$$$RESOURCECON2 = window.$$RESOURCECONTEXT,
      allowRent = _window$$$RESOURCECON2.allowRent,
      sceneToken = _window$$$RESOURCECON2.sceneToken,
      sceneType = _window$$$RESOURCECON2.sceneType,
      namespaceId = _window$$$RESOURCECON2.namespaceId,
      resourceType = _window$$$RESOURCECON2.resourceType;
  var _window$location = window.location,
      host = _window$location.host,
      protocol = _window$location.protocol;
  var baseUrl = "".concat(protocol, "//").concat(host);

  if (resourceType !== 'station_booking' && sceneType === 'park_tourist' && String(allowRent) !== '1') {
    window.alertModal = _modal["default"].alert('提示', '用户认证通过后才能预订', [{
      text: '继续浏览'
    }, {
      text: '马上认证',
      onPress: function onPress() {
        if (window.ehopenapi) {
          window.ehopenapi.router({
            route: 'zl://address/list'
          });
          return false;
        }

        window.location.href = "".concat(baseUrl, "/user-identification/build/index.html#/myAddress?namespaceId=").concat(namespaceId, "&sceneToken=").concat(sceneToken);
      }
    }]);
    return false;
  } // row 第几行
  // col 第几列


  var _window$$$RESOURCECON3 = window.$$RESOURCECONTEXT,
      multiUnit = _window$$$RESOURCECON3.multiUnit,
      autoAssign = _window$$$RESOURCECON3.autoAssign;
  var _data$bodyArray = data.bodyArray,
      bodyArray = _data$bodyArray === void 0 ? [] : _data$bodyArray,
      _data$dataDetail2 = data.dataDetail,
      dataDetail = _data$dataDetail2 === void 0 ? {} : _data$dataDetail2,
      _data$headerContent = data.headerContent,
      headerContent = _data$headerContent === void 0 ? [] : _data$headerContent,
      ruleDate = data.ruleDate;
  var array = bodyArray.concat();
  var _selfState = selfState,
      clickObj = _selfState.clickObj;

  if (Number(dataDetail.multiTimeInterval) === 1) {
    // 可以选多个时间段
    if (Number(multiUnit) === 1 && Number(autoAssign) === 1) {
      // 是有编号的多选
      clearSelect(array, col);
      doSelect({
        e: e,
        ele: ele,
        row: row,
        col: col,
        data: data,
        array: array,
        clickObj: clickObj,
        headerContent: headerContent,
        ruleDate: ruleDate
      }, {
        setParentState: setParentState,
        configState: configState
      }, Number(dataDetail.multiTimeInterval));
    } else {
      // 无编号的 多选和单选
      if (!clickObj[String(col)]) {
        // 这里就是跨列的意思
        array.forEach(function (e) {
          // 清除所选
          for (var key in e) {
            if (_typeof(e[key]) === 'object') {
              e[key].select = undefined;
              clickObj[String(key).slice(6)] = undefined;
            }
          }
        });
      } else {
        clearSelect(array, col);
      }

      doSelect({
        e: e,
        ele: ele,
        row: row,
        col: col,
        data: data,
        array: array,
        clickObj: clickObj,
        headerContent: headerContent,
        ruleDate: ruleDate
      }, {
        setParentState: setParentState,
        configState: configState
      }, Number(dataDetail.multiTimeInterval));
    }
  } else {
    // 不能选多个时间段
    if (Number(multiUnit) === 1 && Number(autoAssign) === 1) {
      // 是有编号的多选，隔列可以选
      clearSelect(array, col);
      doSelect({
        e: e,
        ele: ele,
        row: row,
        col: col,
        data: data,
        array: array,
        clickObj: clickObj,
        headerContent: headerContent,
        ruleDate: ruleDate
      }, {
        setParentState: setParentState,
        configState: configState
      }, Number(dataDetail.multiTimeInterval));
    } else {
      // 无编号的 多选和单选
      if (!clickObj[String(col)]) {
        // 这里就是跨列的意思
        array.forEach(function (e, i) {
          // 清除所选
          for (var key in e) {
            if (_typeof(e[key]) === 'object') {
              e[key].select = undefined;
              clickObj[String(key).slice(6)] = undefined;
            }
          }
        });
        clearSelect(array, col);
      } else {
        clearSelect(array, col);
      }

      doSelect({
        e: e,
        ele: ele,
        row: row,
        col: col,
        data: data,
        array: array,
        clickObj: clickObj,
        headerContent: headerContent,
        ruleDate: ruleDate
      }, {
        setParentState: setParentState,
        configState: configState
      }, Number(dataDetail.multiTimeInterval));
    }
  }
};

var getUnitHtml = function getUnitHtml(_ref5, _ref6) {
  var ele = _ref5.ele,
      row = _ref5.row,
      col = _ref5.col,
      unitWitdh = _ref5.unitWitdh,
      data = _ref5.data;
  var setParentState = _ref6.setParentState,
      configState = _ref6.configState;
  var _window$$$RESOURCECON4 = window.$$RESOURCECONTEXT,
      needPay = _window$$$RESOURCECON4.needPay,
      priceType = _window$$$RESOURCECON4.priceType;

  if (ele && Number(ele.status) === 1) {
    return _react["default"].createElement("div", {
      className: "inner"
    }, _react["default"].createElement("div", {
      className: "hasRental",
      style: {
        width: unitWitdh
      }
    }, "\u5DF2\u9884\u8BA2"));
  }

  if (ele && Number(ele.status) === 0) {
    if (Number(needPay) === 0) {
      // 不需要支付
      return _react["default"].createElement("div", {
        className: "inner"
      }, _react["default"].createElement("div", {
        className: "canRental",
        "data-ruleId": ele.id,
        onClick: function onClick(e) {
          return onSelect({
            e: e,
            ele: ele,
            row: row,
            col: col,
            data: data
          }, {
            setParentState: setParentState,
            configState: configState
          });
        },
        style: {
          backgroundColor: getSelectStyle(ele.select).backgroundColor,
          color: getSelectStyle(ele.select).color,
          width: unitWitdh
        }
      }, "\u53EF\u9884\u8BA2"));
    }

    if (Number(needPay) === 1) {
      // 需要支付 分为有原价  没有原价
      if (Number(priceType) === 1) {
        return _react["default"].createElement("div", {
          className: "inner"
        }, _react["default"].createElement("div", {
          className: "canRental",
          "data-ruleId": ele.id,
          onClick: function onClick(e) {
            return onSelect({
              e: e,
              ele: ele,
              row: row,
              col: col,
              data: data
            }, {
              setParentState: setParentState,
              configState: configState
            });
          },
          style: {
            backgroundColor: getSelectStyle(ele.select).backgroundColor,
            color: getSelectStyle(ele.select).color,
            width: unitWitdh
          }
        }, "\u53EF\u9884\u8BA2"));
      }

      return _react["default"].createElement("div", {
        className: "inner"
      }, _react["default"].createElement("div", {
        className: "canRental",
        onClick: function onClick(e) {
          return onSelect({
            e: e,
            ele: ele,
            row: row,
            col: col,
            data: data
          }, {
            setParentState: setParentState,
            configState: configState
          });
        },
        style: {
          backgroundColor: getSelectStyle(ele.select).backgroundColor,
          color: getSelectStyle(ele.select).color,
          width: unitWitdh
        }
      }, _react["default"].createElement("div", {
        className: "price-discount",
        style: ele.price && ele.originalPrice ? {
          color: getSelectStyle(ele.select).priceColor
        } : {
          color: getSelectStyle(ele.select).color
        }
      }, ele && String(ele.price) === '0' ? '免费' : "\uFFE5".concat(ele.price)), ele.originalPrice ? _react["default"].createElement("div", {
        className: "price-discount-del",
        style: {
          width: "".concat(unitWitdh - 'px' - 15, "px"),
          paddingRight: '15px'
        }
      }, _react["default"].createElement("del", {
        style: {
          color: getSelectStyle(ele.select).originalColor
        }
      }, ele && Number(ele.originalPrice) === 0 ? '免费' : "\uFFE5".concat(ele.originalPrice))) : ''));
    }
  } else {
    return _react["default"].createElement("div", {
      className: "inner"
    }, _react["default"].createElement("div", {
      className: "cannotRental",
      style: {
        width: unitWitdh
      }
    }, "\u4E0D\u53EF\u9884\u8BA2"));
  }
};

exports.getUnitHtml = getUnitHtml;