/* eslint-disable no-nested-ternary */
import { locales } from '@pisell/utils';
import { useMount } from 'ahooks';
import { Button } from 'antd';
import React from 'react';
import { ModeEnum, ClientEnum } from "../../const";
import { getInitState } from "../../manufacturer";
import XClose from '@pisell/icon/es/XClose';
import { useDispatch, useContextData, updateState, updateComponent } from "../../store";
import Header from "../header";
export default (function (_ref) {
  var _ref$className = _ref.className,
    className = _ref$className === void 0 ? '' : _ref$className,
    isMobile = _ref.isMobile,
    onChange = _ref.onChange;
  var _useContextData = useContextData(),
    eftpos = _useContextData.eftpos,
    custom = _useContextData.custom,
    mode = _useContextData.mode,
    name = _useContextData.name,
    symbol = _useContextData.symbol,
    amount = _useContextData.amount,
    order_id = _useContextData.order_id,
    number = _useContextData.number,
    device = _useContextData.device,
    client = _useContextData.client;
  var failCustom = custom === null || custom === void 0 ? void 0 : custom.failCustom;
  var dispatch = useDispatch();

  /**
   * @title: 重新支付
   * @Author: hongbing.wang
   * @Date: 2024-08-20 11:30
   */
  var onRetry = function onRetry() {
    onChange('page', 'pay');
    dispatch(updateState(getInitState(eftpos, mode === ModeEnum.Query ? {
      mode: mode,
      name: name,
      symbol: symbol,
      amount: amount,
      order_id: order_id
    } : {
      mode: mode,
      name: name,
      symbol: symbol,
      amount: amount,
      order_id: order_id,
      number: ''
    })));
  };

  /**
   * @title: 修改金额信息重新支付
   * @Author: hongbing.wang
   * @Date: 2024-08-20 11:30
   */
  var onModify = function onModify() {
    dispatch(updateState(getInitState(eftpos, {
      mode: mode,
      name: name,
      symbol: symbol,
      amount: amount,
      order_id: order_id,
      number: ''
    })));
    onChange('page', 'amount');
  };

  /**
   * @title: 手动标记
   * @description: 前往手动标记
   * @return {*}
   * @Author: hongbing.wang
   * @Date: 2024-08-20 11:31
   */
  var onManually = function onManually() {
    dispatch(updateState({
      manual: {
        title: locales.getText('page.eftpos-pay.text.request.unknown.title')(mode),
        text: locales.getText('page.eftpos-pay.text.request.unknown.text')(mode)
      }
    }));
    dispatch(updateComponent('Manual'));
  };
  var onNotin = function onNotin() {
    onChange('print', [], 'print_on_timeout');
  };

  /**
   * @title: 关闭支付页面
   * @description: 交易关闭页面
   * @Author: hongbing.wang
   * @Date: 2024-08-20 11:32
   */
  var onClose = function onClose() {
    onChange('fail', {
      number: number,
      uniquePaymentNumber: number,
      device: device,
      order_id: order_id
    });
  };

  // 错误页 通知外界 开发关闭按钮
  useMount(function () {
    if ((failCustom === null || failCustom === void 0 ? void 0 : failCustom.status) === 'fail') {
      onChange('page', 'fail');
    }
  });
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-box ".concat(className)
  }, /*#__PURE__*/React.createElement(Header, {
    status: failCustom.status
  }, (failCustom === null || failCustom === void 0 ? void 0 : failCustom.status) === 'fail' && !isMobile ? /*#__PURE__*/React.createElement(XClose, {
    size: 20,
    style: {
      fontSize: '24px',
      position: 'absolute',
      top: 0,
      right: 0
    },
    onClick: onClose
  }) : null), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-fail-title"
  }, failCustom.title || locales.getText('page.eftpos-pay.text.failed')(mode)), (failCustom === null || failCustom === void 0 ? void 0 : failCustom.status) === 'fail' ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-fail-text pisell-lowcode__eft-pay-body"
  }, failCustom === null || failCustom === void 0 ? void 0 : failCustom.fail) : (failCustom === null || failCustom === void 0 ? void 0 : failCustom.status) === 'warn' ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-warn-text pisell-lowcode__eft-pay-body"
  }, failCustom === null || failCustom === void 0 ? void 0 : failCustom.warn) : null, failCustom !== null && failCustom !== void 0 && failCustom.isPosNetworkError ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-step-item-btns"
  }, client === ClientEnum.Merchant ? /*#__PURE__*/React.createElement(Button, {
    block: true,
    className: "pisell-lowcode__eft-pay-step-item-btn",
    onClick: onManually
  }, locales.getText('page.eftpos-pay.text.manual.marking')) : /*#__PURE__*/React.createElement(Button, {
    block: true,
    className: "pisell-lowcode__eft-pay-step-item-btn",
    onClick: onNotin
  }, locales.getText('page.eftpos-pay.text.manual.print_receipt'))) : /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-step-item-btns"
  }, /*#__PURE__*/React.createElement(Button, {
    block: true,
    className: "pisell-lowcode__eft-pay-step-item-btn",
    onClick: onClose
  }, locales.getText('page.eftpos-pay.text.cancel')), /*#__PURE__*/React.createElement(Button, {
    block: true,
    type: "primary",
    className: "pisell-lowcode__eft-pay-step-item-btn",
    onClick: onRetry
  }, locales.getText('page.eftpos-pay.text.retry'))));
});