import _regeneratorRuntime from "@babel/runtime/regenerator";

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? Object(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 asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

import { message, Modal, Table } from '@whalecloud/fdx';
import React, { useEffect, useState } from 'react';
import { getCouponCollectTime, getCouponVaildity } from '../../../utils/businessUtil';
import { fishFetch } from '../../../utils/commonUtil';
import { getCouponDiscountDesc } from '../../../utils/currencyUtil';
export default function CouponListModal(props) {
  var visible = props.visible,
      onOk = props.onOk,
      onCancel = props.onCancel,
      initCoupons = props.coupons;

  var _useState = useState([]),
      _useState2 = _slicedToArray(_useState, 2),
      coupons = _useState2[0],
      setCoupons = _useState2[1];

  var _useState3 = useState(initCoupons),
      _useState4 = _slicedToArray(_useState3, 2),
      selectedCoupons = _useState4[0],
      setSelectedCoupons = _useState4[1];

  var _useState5 = useState({
    pageSize: 10,
    pageIndex: 1,
    total: 0
  }),
      _useState6 = _slicedToArray(_useState5, 2),
      paginationParams = _useState6[0],
      setPaginationParams = _useState6[1];

  var columns = [{
    title: 'Coupon Name',
    dataIndex: 'couponName',
    width: 180
  }, {
    title: 'Coupon Value',
    dataIndex: 'discountAmount',
    width: 150,
    render: function render(text, record) {
      return getCouponDiscountDesc(record);
    }
  }, {
    title: 'Period of Vaildity',
    dataIndex: 'date',
    width: 250,
    render: function render(text, record) {
      return getCouponVaildity(record);
    }
  }, {
    title: 'Collection Time',
    dataIndex: 'date',
    width: 300,
    render: function render(text, record) {
      return getCouponCollectTime(record);
    }
  }];

  var onRowSelect = function onRowSelect(record, selected) {
    if (selected) {
      var newSelectedCoupon = [].concat(_toConsumableArray(selectedCoupons), [record]);
      setSelectedCoupons(newSelectedCoupon);
    } else {
      setSelectedCoupons(selectedCoupons.filter(function (coupon) {
        return coupon.couponId !== record.couponId;
      }));
    }
  };

  var onModalOk = function onModalOk() {
    if (selectedCoupons.length === 0) {
      message.info('Please select a record');
      return;
    }

    onOk(selectedCoupons);
  };

  var getCoupons = /*#__PURE__*/function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      var retData;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return fishFetch("".concat(window.MParams.MBaseUrl, "/coupons/couponList/decorate"), {
                pageIndex: paginationParams.pageIndex,
                pageSize: paginationParams.pageSize
              });

            case 2:
              retData = _context.sent;
              setCoupons(retData.list || []);
              setPaginationParams(_objectSpread({}, setPaginationParams, {
                total: retData.total
              }));

            case 5:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));

    return function getCoupons() {
      return _ref.apply(this, arguments);
    };
  }();

  useEffect(function () {
    getCoupons();
  }, [paginationParams.pageIndex]);
  return /*#__PURE__*/React.createElement("div", {
    className: "coupon-modal-wrap"
  }, /*#__PURE__*/React.createElement(Modal, {
    title: "Coupon List",
    visible: visible,
    onCancel: onCancel,
    onOk: onModalOk,
    width: 1000,
    bodyStyle: {
      height: '700px'
    }
  }, /*#__PURE__*/React.createElement(Table, {
    rowSelection: {
      selectedRowKeys: selectedCoupons.map(function (coupon) {
        return coupon.couponId;
      }),
      onSelect: onRowSelect
    },
    rowKey: "couponId",
    columns: columns,
    dataSource: coupons,
    bordered: false,
    pagination: _objectSpread({}, paginationParams, {
      onChange: function onChange(page) {
        paginationParams.pageIndex = page;
        setPaginationParams(_objectSpread({}, setPaginationParams, {
          pageIndex: page
        }));
      }
    }),
    scroll: {
      x: 880
    }
  })));
}