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 PickupReceivableCollectPage from '../oneos-web-finance/pages/08-提车应收款-提车收款单.jsx';
import PickupCollectEditPage from '../oneos-web-finance/pages/04-提车收款单-编辑.jsx';
import VehiclePickupReceivableList from './VehiclePickupReceivableList.jsx';
import {
  toCollectPageVehicles,
  appendPickupCollectChild,
  buildPickupSubmitCcMessage,
  applyPickupDateToVehicles,
  buildBatchPickupScheduleSummary,
  describePickupRule,
} from './pickup-receivable-bridge.js';

var DEFAULT_PICKUP_DATE = '2026-03-12';

/** @typedef {'list' | 'collect' | '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' : '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];

  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.__pickupCalc = {
      applyPickupDateToVehicles: applyPickupDateToVehicles,
      buildBatchPickupScheduleSummary: buildBatchPickupScheduleSummary,
      describePickupRule: describePickupRule,
    };
    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.__pickupHandleSubmit;
      delete window.__pickupBuildSubmitCcMessage;
      delete window.__pickupCalc;
    };
  }, [setView, bumpListRefresh]);

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

  if (view === 'collect') return React.createElement(PickupReceivableCollectPage);
  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 months = record.paymentCycleMonths || 1;
      var pickupDate = record.lastPickupDate || DEFAULT_PICKUP_DATE;
      var ctx = {
        contractCode: record.contractCode,
        contractType: record.contractType,
        projectName: record.projectName,
        customerName: record.customerName,
        paymentMethod: record.paymentMethod || '预付',
        paymentCycle: months + '个月',
        paymentCycleMonths: months,
        contractStart: record.contractEffectiveDate,
        contractEnd: record.contractEndDate || '',
        businessDept: record.businessDept,
        businessPerson: record.businessPerson,
        pickupDate: pickupDate,
        vehicles: toCollectPageVehicles(record, pickupDate),
      };
      setHandleContext(ctx);
      window.__pickupHandleContext = ctx;
      setView('collect');
    },
    onGoView: function () {
      setView('view');
    },
    onGoEdit: function () {
      setView('edit');
    },
  });
};

export default Component;
