import PickupReceivableViewPage from '../oneos-web-finance/pages/05-提车应收款-查看.jsx';
import PickupReceivableInvoicePage from '../oneos-web-finance/pages/06-提车应收款-开票信息.jsx';
import PickupReceivableAuditPage from '../oneos-web-finance/pages/07-提车应收款-审核.jsx';
import PickupCollectEditPage from '../oneos-web-finance/pages/04-提车收款单-编辑.jsx';
import VehiclePickupReceivableList from './VehiclePickupReceivableList.jsx';
import VehiclePickupReceivableCollectPage from './VehiclePickupReceivableCollectPage.jsx';
import VehiclePickupReceivableContractView from './VehiclePickupReceivableContractView.jsx';
import {
  appendPickupCollectChild,
  buildPickupSubmitCcMessage,
  buildCollectHandleContext,
  buildContractViewContext,
  canShowHandle,
} from './pickup-receivable-bridge.js';

/** @typedef {'list' | 'collect' | 'contractView' | 'view' | 'edit' | 'invoice' | 'audit'} PickupView */

const Component = function (props) {
  var useState = React.useState;
  var useEffect = React.useEffect;
  var _view = useState('list');
  var view = _view[0];
  var setView = _view[1];

  useEffect(function () {
    if (props && props.onPageViewChange) {
      var pageId = view === 'collect'
        ? 'collect'
        : view === 'contractView'
          ? 'contractView'
          : 'list';
      props.onPageViewChange(pageId);
    }
  }, [view, props]);

  var listRefreshState = useState(0);
  var listRefreshToken = listRefreshState[0];
  var bumpListRefresh = listRefreshState[1];

  var handleContextState = useState(null);
  var handleContext = handleContextState[0];
  var setHandleContext = handleContextState[1];

  var viewContextState = useState(null);
  var viewContext = viewContextState[0];
  var setViewContext = viewContextState[1];

  var contractViewRecordState = useState(null);
  var contractViewRecord = contractViewRecordState[0];
  var setContractViewRecord = contractViewRecordState[1];

  useEffect(function () {
    window.__receivableBack = function () {
      setView('list');
      bumpListRefresh(function (n) {
        return n + 1;
      });
    };
    window.__receivableToCollect = function () {
      setView('collect');
    };
    window.__receivableToEdit = function () {
      setView('edit');
    };
    window.__receivableToView = function () {
      setView('view');
    };
    window.__receivableToInvoice = function () {
      setView('invoice');
    };
    window.__receivableToAudit = function () {
      setView('audit');
    };
    window.__receivableSubmit = function () {
      if (window.__receivableBack) window.__receivableBack();
    };
    window.__pickupBuildSubmitCcMessage = buildPickupSubmitCcMessage;
    window.__pickupHandleSubmit = function (payload) {
      var ctx = window.__pickupHandleContext;
      if (ctx && ctx.contractCode) {
        appendPickupCollectChild(ctx.contractCode, payload);
      }
    };
    return function () {
      delete window.__receivableBack;
      delete window.__receivableToCollect;
      delete window.__receivableToEdit;
      delete window.__receivableToView;
      delete window.__receivableToInvoice;
      delete window.__receivableToAudit;
      delete window.__receivableSubmit;
      delete window.__pickupHandleContext;
      delete window.__pickupViewContext;
      delete window.__pickupHandleSubmit;
      delete window.__pickupBuildSubmitCcMessage;
    };
  }, [setView, bumpListRefresh]);

  useEffect(
    function () {
      if (view === 'collect' && handleContext) {
        window.__pickupHandleContext = handleContext;
      }
      if (view === 'contractView' && viewContext) {
        window.__pickupViewContext = viewContext;
      }
    },
    [view, handleContext, viewContext],
  );

  if (view === 'collect') return React.createElement(VehiclePickupReceivableCollectPage);
  if (view === 'contractView') {
    return React.createElement(VehiclePickupReceivableContractView, {
      canHandle: contractViewRecord ? canShowHandle(contractViewRecord) : false,
      onGoHandle: function () {
        if (!contractViewRecord) return;
        var ctx = buildCollectHandleContext(contractViewRecord);
        setHandleContext(ctx);
        window.__pickupHandleContext = ctx;
        setView('collect');
      },
    });
  }
  if (view === 'view') return React.createElement(PickupReceivableViewPage);
  if (view === 'edit') return React.createElement(PickupCollectEditPage);
  if (view === 'invoice') return React.createElement(PickupReceivableInvoicePage);
  if (view === 'audit') return React.createElement(PickupReceivableAuditPage);

  return React.createElement(VehiclePickupReceivableList, {
    refreshToken: listRefreshToken,
    onGoHandle: function (record) {
      var ctx = buildCollectHandleContext(record);
      setHandleContext(ctx);
      window.__pickupHandleContext = ctx;
      setView('collect');
    },
    onGoContractView: function (record) {
      setContractViewRecord(record);
      var ctx = buildContractViewContext(record);
      setViewContext(ctx);
      window.__pickupViewContext = ctx;
      setView('contractView');
    },
    onGoView: function (record, parent) {
      if (parent) {
        window.__pickupViewContext = { mode: 'batch', batch: record, parent: parent };
      }
      setView('view');
    },
    onGoEdit: function () {
      setView('edit');
    },
  });
};

export default Component;
